malloc 期间内核发生了什么?

发布于 2024-11-01 23:29:42 字数 270 浏览 0 评论 0原文

我在接受采访时被问到这个问题。他们想知道的是,当用户调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

通知家属抬走 2024-11-08 23:29:42

当用户空间应用程序调用 malloc() 时,该调用不会在内核中实现。相反,它是一个库调用(实现 glibc 或类似的)。

简而言之,glibc 中的 malloc 实现要么从 brk()/sbrk() 系统调用获取内存,要么通过 匿名内存代码>mmap()。这为 glibc 提供了一个大的连续(关于虚拟内存地址)内存块,malloc 实现进一步将其分割成更小的块并分发给您的应用程序。

这里是一个小的 malloc 实现,它将为您提供想法,以及很多很多链接。

请注意,目前还没有任何东西关心物理内存——当进程数据段通过 brk()/sbrk()更改时,由内核虚拟内存系统处理>mmap(),以及当引用内存时(通过对内存的读取或写入)。

总结一下:

  1. malloc() 将搜索其托管内存块,看看是否有一块未使用的内存满足分配要求。
  2. 如果失败,malloc() 将尝试扩展进程数据段(通过 sbrk()/brk() 或在某些情况下mmap())。 sbrk() 最终进入内核。
  3. 内核中的brk()/sbrk()调用会调整进程的struct mm_struct中的一些偏移量,因此进程数据细分市场将会更大。首先,不会有物理内存映射到扩展数据段所提供的附加虚拟地址。
  4. 当第一次触及未映射的内存时(可能是通过 malloc 实现进行读/写),错误处理程序将启动并捕获到内核,内核将物理内存分配给未映射的内存。

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 the brk()/sbrk() system call or anonymous memory via mmap(). This gives glibc a big contiguous (regarding virtual memory addresses) chunk of memory, which the malloc 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() or mmap(), and when the memory is referenced (by a read or write to the memory).

To summarize:

  1. malloc() will search its managed pieces of memory to see if there's a piece of unused memory that satisfy the allocation requirements.
  2. Failing that, malloc() will try to extend the process data segment(via sbrk()/brk() or in some cases mmap()). sbrk() ends up in the kernel.
  3. The brk()/sbrk() calls in the kernel adjust some of the offsets in the struct 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.
  4. When that unmapped memory is first touched (likely a read/write by the 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.
百变从容 2024-11-08 23:29:42

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, and malloc manipulates the free list according to its implemention (check glibc for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文