malloc 期间内核发生了什么?
我在接受采访时被问到这个问题。他们想知道的是,当用户调用 malloc(4) 分配 4 字节内存时,操作系统(Linux)如何响应?哪个子系统响应该系统调用?
我告诉他 malloc() 将由内存管理子系统提供服务。 malloc() 实现将遍历空闲内存(物理内存)列表,我们将其称为空闲列表,并找到大于或等于 4 字节的合适块。一旦找到这样的块,它将从空闲列表中删除并添加到已使用列表中。然后该物理内存将被映射到进程堆 vma 结构。他似乎对这个答案不太满意。好友系统是如何融入其中的呢?任何帮助将不胜感激。
I was asked this question during an interview. What they wanted to know was when the user calls malloc(4) to allocate 4 bytes of memory how does the operating system (Linux) respond? Which subsystem responds to this system call?
I told him that malloc() will be serviced by the memory management subsystem. The malloc() implementation will go through the list of free memory(physical memory), we will call it free list, and find an appropriate chunk that is greater than or equal to 4 Bytes. Once it finds such a chunk, it will be deleted from free list and added to a used list. Then that physical memory will be mapped to the process heap vma struct. He didn't seem to be quite satisfied with this answer.How does the buddy system fit into this? Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当用户空间应用程序调用
malloc()
时,该调用不会在内核中实现。相反,它是一个库调用(实现 glibc 或类似的)。简而言之,glibc 中的
malloc
实现要么从brk()
/sbrk()
系统调用获取内存,要么通过匿名内存代码>mmap()。这为 glibc 提供了一个大的连续(关于虚拟内存地址)内存块,
malloc
实现进一步将其分割成更小的块并分发给您的应用程序。这里是一个小的
malloc
实现,它将为您提供想法,以及很多很多链接。请注意,目前还没有任何东西关心物理内存——当进程数据段通过
brk()
/sbrk()
或更改时,由内核虚拟内存系统处理>mmap()
,以及当引用内存时(通过对内存的读取或写入)。总结一下:
malloc()
将搜索其托管内存块,看看是否有一块未使用的内存满足分配要求。malloc()
将尝试扩展进程数据段(通过sbrk()
/brk()
或在某些情况下mmap()
)。sbrk()
最终进入内核。brk()
/sbrk()
调用会调整进程的struct mm_struct
中的一些偏移量,因此进程数据细分市场将会更大。首先,不会有物理内存映射到扩展数据段所提供的附加虚拟地址。When user space applications call
malloc()
, that call isn't implemented in the kernel. Instead, it's a library call (implemented glibc or similar).The short version is that the
malloc
implementation in glibc either obtains memory from thebrk()
/sbrk()
system call or anonymous memory viammap()
. This gives glibc a big contiguous (regarding virtual memory addresses) chunk of memory, which themalloc
implementation further slices and dices in smaller chunks and hands out to your application.Here's a small
malloc
implementation that'll give you the idea, along with many, many links.Note that nothing cares about physical memory yet -- that's handled by the kernel virtual memory system when the process data segment is altered via
brk()
/sbrk()
ormmap()
, and when the memory is referenced (by a read or write to the memory).To summarize:
malloc()
will search its managed pieces of memory to see if there's a piece of unused memory that satisfy the allocation requirements.malloc()
will try to extend the process data segment(viasbrk()
/brk()
or in some casesmmap()
).sbrk()
ends up in the kernel.brk()
/sbrk()
calls in the kernel adjust some of the offsets in thestruct mm_struct
of the process, so the process data segment will be larger. At first, there will be no physical memory mapped to the additional virtual addresses which extending the data segment gave.malloc
implementation) a fault handler will kick in and trap down to the kernel, where the kernel will assign physical memory to the unmapped memory.malloc
不直接处理物理内存。它涉及 分页虚拟内存 - 尽管我不确定这是否适用于每个那里的建筑。当您的程序尝试分配内存并且空闲列表不包含大小等于或大于请求大小的块时,则会分配整个新页面。页面大小取决于体系结构(x86 上为 4096 字节)。页面分配只有内核才能执行,因此
malloc
调用可能会导致系统调用。然后,新地址被添加到空闲列表中,malloc
根据其实现来操作空闲列表(例如检查 glibc)。malloc
does not deal with physical memory directly. It deals with paged virtual memory - although I'm not certain if it's true for every architecture out there.When your program tries to allocate memory and the free list doesn't contain a chunk of equal or larger size than the requested size, an entire new page is allocated. The page size is architecture dependent (4096 bytes on x86). Page allocation is something only the kernel can perform, thus a
malloc
call may cause a system call. The new address is then added to the free list, andmalloc
manipulates the free list according to its implemention (check glibc for example).