3
$\begingroup$

In the Silberschatz's book "operating systems" the author talks about the allocation of memory via system buddy and slab. My first question is: in the book, both memory allocation methods are described as allocation method that the kernel use to allocate memory only for kernel's process, not for user's process. Is it true?

My second question, as the title suggest, regards the allocation method that is implemented inside the Linux kernel. Looking the website kernel.org I've seen that there is a chapter dedicated to buddy system and a page dedicated to slab. So, I imagine that both are present inside the kernel, but what is one method for and what is the other for?

$\endgroup$

1 Answer 1

4
$\begingroup$

A modern operating system manages physical memory as page frames. A page frame can be allocated to a user process or for the kernel to use for its own purposes, such as to allocate its own data structures.

A kernel data object (e.g. a structure representing an "open file" or something) is typically less than the size of a page. So it makes sense to have a two-level allocation hierarchy: one to manage page frames, and one to allocate data structures inside allocated page frames.

Linux uses a buddy allocator to allocate page frames, and a slab allocator to allocate kernel data structures. When the slab allocator needs more memory, it obtains it from the buddy allocator.

This approach works well for Linux, since it supports different page sizes; x86-64 CPUs support 4kB, 2M, and sometimes 2GB pages, and a binary buddy allocator can support this with very little modification.

Windows NT, by comparison, uses a simple free list to implement page frame allocation. Actually, that's not quite accurate; it uses multiple free lists to implement cache colouring, but that's a story for another time. Again, it layers another allocator on top to handle kernel data structures.

It's a similar story with Mach, the microkernel underneath macOS and iOS. Kernel data structures are implemented with a zone allocator, which plays the same role as the slab allocator in Linux. It, again, operates as a layer above the physical page frame allocator.

$\endgroup$
2
  • $\begingroup$ First of all, thank you! One question: can we say that the slab allocator is used specifically for the kernel's processes and that the buddy allocator is used to allocate frame to both user's processes and kernel's processes? $\endgroup$
    – Bender
    Commented Jun 12, 2022 at 8:09
  • 1
    $\begingroup$ I guess? It's a bit more complicated than that, partly because the kernel isn't all "processes", and partly because there is memory shared between the kernel and user space. When a user process memory maps a file, for example, that has the effect of sharing memory between the user process and buffer cache. $\endgroup$
    – Pseudonym
    Commented Jun 12, 2022 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.