If all these questions related to memory allocation are on your mind, then this blog post is an ideal read for you.

Buddy systems in OS and slab systems are two efficient techniques used to allocate free memory in the kernel processes. As Kernel is touted to offer physical contiguous memory, it often needs memory areas in order to allocate memory or minimize external fragmentation.

This is when the concepts of buddy systems and slab systems come into being!

Allocating the kernel memory

Firstly, we should consider why there is a need to allocate kernel memory in the first place. When a process runs, it may require some additional memory. Although the pages will be allocated from the list of the page frames which are maintained by the kernel, they are mostly free pages.

This kind of memory is usually allocated from the memory pool to satisfy the ordinary mode process because:

Therefore, there is a need to manage the memory involved in the kernel processes. That’s why we usually follow the two strategies which are the buddy system in the OS and the slab system.

Buddy system in the OS

This is an ideal memory allocation concept that divides your memory into the power of two. It tries to satisfy your memory request by partitioning the memory and using it in equal halves to see where it fits the best.

Assume your memory is of the size 2m. The size of this process is denoted by P.

In the case  2m-1 < P<= 2m, you can calculate the memory in your process. Otherwise, we can allocate the memory into halves. If it does not satisfy the given condition, repeat the steps till the above condition is satisfied.

Example:

We are given the buddy system with the physical address, 256 KB. Then, we need to find the partition size of 36 KB.

As 36 kb is greater than 32 kb but less than the available 64 kb. Therefore, 64 kb of the memory is suitable to store the process.

The Importance of the buddy system in the OS

There is an essential need for the buddy system in your OS because of the following reasons:

Types of buddy systems

There are various types of buddy systems that you should take into consideration.

They are:

Slab System

The second strategy which is usually implemented for kernel memory allocation is the slab system. A slab consists of one or more than one contiguous page. For example; there will be a separate cache for data structures, file objects, semaphores, and so on.

Take another example of two kernels of 3 KB with three objects of size 7 KB. The slab allocation uses catches for storing the object. These are marked as free and will be used to allocate the objects.

A 12 kb slab in this case will store at least 6 2kb objects, with all of them marked as free. The new objects will be marked for the needed data structure. The kernel request in the slab allocator describes the process description. In a Linux system, the process descriptor will be of the size type struct with a maximum of 1.7 kb memory. This will create some new tasks for the objects of that cache.

This cache would be able to fulfill objects that are allocated in the slab so as to mark them free.

In Linux, the given slab will be of three states:

The basic aim of a slab allocator will be to satisfy requests given in the partial slab. If none of it exists, the free object will get assigned to your empty slab.

Add-on learning: Belady's anomaly in OS

Another essential concept that you should know if you are involved with the operating system is Belady's anomaly in os. It is seen as the phenomenon which helps to increase the number of page frames to increase the number of page faults in your memory.

Wrapping Up

With this guide, I hope you’ve gained an in-depth understanding of the buddy system in OS and the slab system for allocating kernel memory. Stick to this blog and equip your knowledge bank.