đ Buddy system in Allocating Kernel Memory (11 MCQs)
Power-of-Two Memory Blocks for Kernel Allocation
The buddy system is a physical-memory allocation technique used to obtain and release contiguous blocks of kernel memory. It organizes free memory into blocks whose sizes are powers of two, such as 4 KB, 8 KB, and 16 KB. The allocator splits larger blocks to satisfy smaller requests and merges compatible free buddies when memory is released, helping control external fragmentation.
Why Kernel Memory Needs Fast Contiguous Allocation
Kernel components frequently require physical memory for buffers, page tables, and other structures. A suitable contiguous region may be difficult to find after repeated allocations and deallocations. The buddy system simplifies free-space management by organising blocks into size classes. However, rounding requests upward to the next supported block size can produce internal fragmentation.
Splitting and Merging Buddy Blocks
When a request cannot be satisfied by an available block of the required order, the allocator finds a larger block and splits it into two equal buddies. It continues splitting until the requested order is reached. When a block is freed, the allocator checks whether its corresponding buddy is also free and merges them into a larger block when permitted.
Allocating a Twelve-Kilobyte Memory Request
Suppose the buddy allocator provides power-of-two blocks and receives a request for 12 KB. It must allocate a block of at least 16 KB. If only a 32 KB block is available, it splits that block into two 16 KB buddies and assigns one. The remaining 16 KB buddy stays available for another allocation.
Fast Allocation and Internal Fragmentation
The buddy system supports efficient splitting, merging, and finding contiguous physical blocks. Its structured size classes simplify free-list management and reduce external fragmentation through coalescing. However, requests that fall between power-of-two sizes can waste significant memory. Kernel allocators often combine buddy allocation with slab-based mechanisms to handle frequent small object requests more efficiently.
Clarifying Buddy Relationships and Block Sizes
'Any two adjacent free blocks are buddies' is incorrect because buddies must be corresponding halves of the same parent block. 'A 12 KB request receives exactly 12 KB' is incorrect because a 16 KB power-of-two block is required. 'Freeing a block always merges it immediately' is incorrect because its buddy must also be free and correctly aligned.
Exam Focus: Splitting, Orders and Buddy Merging
Trace allocation from a larger power-of-two block and show each splitting operation. Calculate the allocated size and internal waste for requests that fall between block sizes. When releasing memory, identify the correct buddy using the block's address and order. Explain how merging reduces external fragmentation and why the allocator cannot combine arbitrary adjacent blocks.
Key Terms of the Buddy System
- Buddy system - Allocates power-of-two blocks.
- Buddy block - Matching half of a larger block.
- Order - Identifies a block size.
- Splitting - Divides a larger free block.
- Coalescing - Merges free buddy blocks.
- Internal fragmentation - Unused allocated block space.
đ All Buddy system in Allocating Kernel Memory MCQs
Q1. What does the copyâonâwrite technique do when a process attempts to write to a shared page?
đ Explanation: The technique postpones duplication until a write is actually performed, so the shared page remains common until one process needs to modify it. OptionâŻB correctly describes this behavior. OptionâŻA is wrong because copying is not immediate; OptionâŻC is incorrect because the page is not deleted; OptionâŻD is false since the page is still copied when needed. This delayed copying saves memory and matches realâworld OS behavior.
Q2. Why can pages that contain executable code be shared between a parent and its child after a fork?
đ Explanation: Executable code pages are typically marked readâonly because the CPU only fetches instructions; they are not written to during normal execution. Therefore both processes can safely map the same physical page, which is described in optionâŻA. OptionâŻB is irrelevant to sharing, optionâŻC mischaracterizes storage, and optionâŻD adds an unnecessary security step that does not affect sharing.
Q3. A child process modifies a stack page after a fork that uses copyâonâwrite. Which physical page does the child receive?
đ Explanation: When the child writes to a stack page that was marked copyâonâwrite, the operating system allocates a fresh physical page and copies the contents of the original page (here pageâŻC) into it. The child then works on this private copy, so optionâŻB is correct. OptionâŻA refers to the wrong page, optionâŻC would violate copyâonâwrite semantics, and optionâŻD describes sharing rather than copying.
Q4. How does zeroâfillâonâdemand complement copyâonâwrite when allocating a new page for a write operation?
đ Explanation: Zeroâfillâonâdemand ensures that any newly allocated page is cleared of previous contents, preventing accidental leakage of old data. This cleared page becomes the destination for the copyâonâwrite operation, making optionâŻA correct. Compression (B) and locking (C) are unrelated mechanisms, and swapping (D) is unnecessary for a freshly allocated page.
Q5. If the systemâs pool of free pages is exhausted, what must the operating system do when a fork with copyâonâwrite requires a new page?
đ Explanation: When no free pages remain, the OS can obtain a fresh page from secondary storage, zeroâfill it, and use it for the copyâonâwrite request, which is described in optionâŻC. Aborting the fork (A) would be unnecessary, swapping (B) is a possible step but not the direct allocation, and reusing a page without copying (D) would break isolation.
Q6. Parent process has pages A, B, C. After a fork with copyâonâwrite, the child writes to page B. What is the final state of page B in both processes?
đ Explanation: Because pageâŻB was marked copyâonâwrite, the childâs write triggers the creation of a new physical page containing a copy of the original contents. The parent continues to use the original pageâŻB, while the child works on its private copy, matching optionâŻB. OptionâŻA ignores the copyâonâwrite rule, optionâŻC would require copying for the parent as well, and optionâŻD incorrectly discards the parentâs page.
Q7. Why does copyâonâwrite generally use less memory than an eager copy of all pages during a fork?
đ Explanation: Copyâonâwrite postpones duplication until a write occurs, so only the pages that change are physically copied. Unmodified pages remain shared between parent and child, dramatically reducing memory consumption, which optionâŻA captures. Compression (B) and swap storage (C) are unrelated to the fundamental saving, and deleting pages (D) would lose needed data.
Q8. A system provides both fork() with copyâonâwrite and vfork(). In which situation does the child NOT obtain its own copy of pages?
đ Explanation: vfork() creates a child that shares the address space of the parent until the child either execs a new program or exits. If the child only reads memory, no copy is made, making optionâŻC correct. Reading after a normal fork (A) would still involve copyâonâwrite, writing (B) forces a copy, and creating a thread (D) changes the execution context but does not prevent copying.
Q9. If a page is marked copyâonâwrite but neither the parent nor the child ever writes to it, what happens to that page after both processes terminate?
đ Explanation: When no writes occur, the page stays shared and never gets duplicated. Once both processes exit, the operating system can reclaim the page and return it to the pool of free pages, which is described by optionâŻB. Locking it (A) would waste resources, duplication (C) is unnecessary, and swapping (D) is not required for a cleanable shared page.
Q10. A kernel uses the buddy system to satisfy a request for a free page needed for copyâonâwrite. Which property of the buddy system makes this allocation efficient?
đ Explanation: The buddy system maintains free blocks in sizes that are powers of two, allowing the allocator to quickly find the smallest block that satisfies a request and to split or merge buddies as needed. This efficiency is captured in optionâŻA. Merging all pages (B) would create a single huge block, random allocation (C) defeats the purpose, and fixed reservations (D) reduce flexibility.
Q11. Which operating systems are known to implement copyâonâwrite as described?
đ Explanation: All three listed systemsâWindows XP, Linux, and Solarisâsupport copyâonâwrite for forked processes, making optionâŻD correct. Each of the first three options isolates a single OS, which is inaccurate because the technique is not exclusive to any one of them.