🎓 BookMCQ
← Back to 9. Virtual Memory

📝 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.
2
Easy
6
Medium
3
Hard

📝 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?

A.It immediately copies the page for both processes.
B.It delays copying the page until a write occurs. ✅
C.It deletes the shared page and allocates a new one.
D.It marks the page as read‑only without copying.
💡 Difficulty: easy | ✅ Correct: B

📖 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?

A.They are marked as read‑only and never change. ✅
B.They are smaller than data pages.
C.They are stored in a special cache.
D.They are encrypted for security.
💡 Difficulty: easy | ✅ Correct: A

📖 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?

A.A new copy of the parent’s page A
B.A copy of the original page C ✅
C.The original page C without copying
D.A shared copy of page B
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.It pre‑zeros the page so stale data cannot be exposed. ✅
B.It compresses the page to save space.
C.It locks the page to prevent other processes from accessing it.
D.It swaps the page to disk before copying.
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.Abort the fork operation.
B.Swap out other pages to free memory.
C.Allocate a new page from disk and zero‑fill it. ✅
D.Reuse an existing page without copying.
💡 Difficulty: medium | ✅ Correct: C

📖 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?

A.Both now reference the same physical page B.
B.Parent keeps original B, child gets a private copy of B. ✅
C.Both get new private copies of B.
D.Parent’s B is discarded and only child’s copy remains.
💡 Difficulty: hard | ✅ Correct: B

📖 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?

A.It copies only pages that are actually modified, leaving others shared. ✅
B.It compresses pages after copying.
C.It stores pages in a compressed swap file.
D.It deletes unmodified pages.
💡 Difficulty: hard | ✅ Correct: A

📖 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?

A.When the child calls exec() immediately after vfork().
B.When the child writes to any page after vfork().
C.When the child only reads memory after vfork(). ✅
D.When the child creates a new thread after vfork().
💡 Difficulty: hard | ✅ Correct: C

📖 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?

A.It remains locked in memory forever.
B.It is reclaimed by the system as a free page. ✅
C.It is duplicated for each process before termination.
D.It is written to disk as a swap page.
💡 Difficulty: medium | ✅ Correct: B

📖 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?

A.It always allocates the smallest possible block that is a power of two. ✅
B.It merges all free pages into one large block.
C.It allocates pages randomly to avoid fragmentation.
D.It reserves a fixed number of pages for each process.
💡 Difficulty: medium | ✅ Correct: A

📖 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?

A.Windows XP only
B.Linux only
C.Solaris only
D.Windows XP, Linux, and Solaris ✅
💡 Difficulty: medium | ✅ Correct: D

📖 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.

🔗 Related Topics (MCQs)