Introduction Why Buddy System About Buddy System Types of Buddy System Implementation in Linux Pros and Cons Conclusion
According to Donald Knuth, the buddy system was invented in 1963 by Harry Markowitz, who won the 1990 Nobel Memorial Prize in Economics. It was first described by Kenneth C. Knowlton(published 1965). Now a days Linux uses the buddy system to manage allocation of memory, possibly because it is allocating many structures which are already powers of two, like frames.
The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes use of splitting memory into halves to try to give a best-fit. Compared to the more complex memory allocation techniques that some modern operating systems use, buddy memory allocation is relatively easy to implement. It supports limited but efficient splitting and coalescing of memory blocks.
A fixed partitioning scheme limits the number of active processes and may use space inefficiently if there is a poor match between available partition size and process size A dynamic partitioning scheme is more complex to maintain and includes the overhead of compaction. An interesting compromise of fixed and dynamic partitioning is the buddy system.
The buddy system(binary) allows a single allocation block to be split, to form two blocks half the size of the parent block. These two blocks are known as buddies. Part of the definition of a buddy is that the buddy of block B must be the same size as B, and must be adjacent in memory (so that it is possible to merge them later). The other important property of buddies, stems from the fact that in the buddy system, every block is at an address in memory which is exactly divisible by its size. So all the 16-byte blocks are at addresses which are multiples of 16; all the 64K blocks are at addresses which are multiples of 64K... and so on.
There are number of buddy systems, proposed by researcher, which are capable of reducing execution time and increase memory utilization.Four Types of Buddy System Binary buddy system Fibonacci buddy system Weighted buddy system Tertiary buddy system
These three Buddy Systems are similar in the design of the algorithm, the major difference is the sizes of the memory blocks. It also differs in memory utilization and execution time. In some situations, one buddy system looks good, may not be good in other situation. It simply lies on the requests for memory which causes external and internal fragmentation higher at some situations.
In binary buddy system the memory block of 2m is into two equal parts of 2m-1. It satisfies the following recurrence relation Li = Li-1+ Li-1 8 4 4 2 2 2 2
The memory consists of a collection of blocks of consecutive memory, each of which is a power of two in size. Each block is marked either occupied or free, depending on whether it is allocated to the user. For each block we also know its size . The system provides two operations for supporting dynamic memory allocation: 1. Allocate (2k): Finds a free block of size 2k, marks it as occupied, and returns a pointer to it. 2. Deallocate (B): Marks the previously allocated block B as free and may merge it with others to form a larger free block.
The buddy system maintains a list of the free blocks of each size (called a free list), so that it is easy to find a block of the desired size, if one is available. If no block of the requested size is available, Allocate searches for the first nonempty list for blocks of at least the size requested. In either case, a block is removed from the free list. This process of finding a large enough free block will indeed be the most difficult operation for us to perform quickly.
If the found block is larger than the requested size, say 2k instead of the desired 2i, then the block is split in half, making two blocks of size 2k−1. If this is still too large (k − 1 > i),then one of the blocks of size 2k−1 is split in half. This process is repeated until we have blocks of size 2k−1, 2k−2, . . . , 2i+1, 2i, and 2i. Then one of the blocks of size 2i is marked as occupied and returned to the user. The others are added to the appropriate free lists. Each block B1 was created by splitting another block into two halves, call them B1 (Buddy of B2) and B2(Buddy of B1).
Now when a block is deallocated, the buddy system checks whether the block can be merged with any others or more precisely whether we can undo any splits that were performed to make this block. The merging process checks whether the buddy of a deallocated block is also free, in which case the two blocks are merged; then it checks whether the buddy of the resulting block is also free, in which case they are merged; and so on.
Thus it is crucial for performance purposes to know, given a block address, the size of the block and whether it is occupied. This is usually done by storing a block header in the first few bits of the block. More precisely, we use headers in which the first bit is the occupied bit , and the remaining bits specify the size of the block. Eg) To determine whether the buddy of a block is free, we compute the buddy’s address, look at the first bit at this address, and also check that the two sizes match.
Example: Let us consider 1-Mbyte of memory is allocated usingBuddy System. Show the Binary tree form and list form for thefollowing : Request 100k(A) Request 240k(B) Request 64k(C) Request 256k(D) Release B Release A Request 75k Release C Release E Release D.
Unused memory Used memoryA=128 C=64 64 256 D=256 256
Hirschberg taking Knuths suggestion has designed a Fibonacci buddy system with block sizes which are Fibonacci numbers. It satisfies the following recurrence relation : Li=Li-1 + Li-2. 0, 1,1, 2, 3, 5, 8,13, 21, 34, 55, 144, 233,377, 610, 987, 1597, 2582… 610 377 233 233 144 144 89
The address calculation for the binary and weighted buddy systems is straight forward, but the original procedure for the Fibonacci buddy system was either limited to a small, fixed number of block sizes or a time consuming computation.Problem: Show the results of the following sequence in a figure using Fibonacci Buddy system: Request 60 (A); Request 135 (B); Request 70 (C); Return A; Request 20 (D);Return B; Return C; Return D.
In weighted buddy system the memory block of size 2k+2 is split in 3.(2k ) and 2k sized blocks. Further 3.(2k) is split in two 2k+1 and 2k sized blocks. 64 48 16 32 16 12 4 24 8 12 4 8 4 3 1
In tertiary buddy system blocks of size 2k is split into blocks of three different sizes . These blocks in turn are split into two different ways depending on the size of the blocks to be split . Blocks of size 2k are split into three blocks of sizes 2k-1 ,3.2k-3 and 2k-3 . Blocks of size 3. 2k-3 are split into the blocks of sizes 2k-2 and 2k-3 . It decreases the amount of internal fragmentation by allowing more block sizes .
L i = Li –1 + Li -3 + Lβ(i) ◦ Where β is any function over positive integer with βi <i. 64 32 24 16 12 4 8 16 88 6 2 8 4 4 3 1
In LINUX, Buddy System in Contiguous Page Frame Allocation. All page frames are grouped into 10 lists of blocks that containing groups of 1, 2, 4, 8, 16, 32, 64, 128, 256, and 512 contiguous page frames, respectively The address of the first page frame of a block is a multiple of the group size For eg, a request for 128 then First checks for a free block in the 128 list If no free block, it then looks in the 256 list for a free block If it finds a block, the kernel allocates 128 of the 256 page frames and puts the remaining 128 into the 128 list If no block it looks at the next larger list, allocating it and dividing the block similarly If no block can be allocated an error is reported
Less external fragmentation. Search for a block of the right size is cheaper than, best fit because we need only find the first available block on the block list for blocks of size 2k; Merging adjacent free blocks is easy. In buddy systems, the cost to allocate and free a block of memory is low compared to that of best-fit or first-fit algorithms.
It allows internal fragmentation. For example, a request for 515k will require a block of size 1024k. In consequence, such an approach gives a waste of 509 k. Splitting and merging adjacent areas is a recurrent operation and thus very unpredictable and inefficient. The another drawback of the buddy system is the time required to fragment and coalesce blocks.