🎓 BookMCQ
← Back to 9. Virtual Memory

📝 Slab allocation in Kernel Memory (15 MCQs)

Object-Based Allocation for Frequently Used Kernel Structures

Slab allocation is a kernel-memory management technique that allocates memory for frequently used kernel objects through caches of preallocated object slots. Each cache is associated with a particular object type, such as process descriptors or file-system structures. Slabs contain multiple objects and obtain their underlying pages from a lower-level allocator. This approach reduces repeated allocation overhead and improves reuse of common kernel structures.

Why Repeated Small Kernel Allocations Need Special Handling

Kernel subsystems repeatedly create and destroy objects of predictable sizes. General-purpose allocation can waste memory, increase fragmentation, and require repeated initialisation. Slab allocation addresses these costs by keeping object caches ready for reuse. It can also preserve initialised object state where appropriate, reducing repeated setup operations when objects of the same type are allocated again.

Creating Caches and Reusing Object Slots

The kernel creates an object cache with a defined object size and alignment. The allocator obtains memory pages, divides them into object slots, and groups the resulting slabs according to availability. Allocation returns a free object slot, while deallocation marks it reusable. Some implementations use constructors, per-CPU caches, and separate lists for full, partial, and empty slabs.

Allocating Process Descriptor Objects

Suppose a kernel cache manages process descriptor objects of 256 bytes each. A 4 KB slab page can contain 16 such objects before metadata and alignment overhead. When a new process requires a descriptor, the allocator returns an available slot rather than requesting a separate page. After process termination, the slot becomes reusable for another descriptor.

Reduced Fragmentation and Faster Object Reuse

Slab allocation improves performance for repeated fixed-size kernel objects and reduces fragmentation between objects of different types. Per-CPU caches can also lower locking and contention costs. However, partially occupied slabs may retain unused memory, and many object caches require metadata and management. Modern implementations vary in their allocation and reclamation strategies, including variants such as SLUB.

Common Errors About Slab-Based Allocation

'A slab cache stores only complete process images' is incorrect because caches contain fixed-size kernel objects. 'Every object receives a separate physical page' is incorrect because many objects share a slab. 'Slab allocation replaces every other kernel allocator' is incorrect because it commonly obtains pages from lower-level physical-memory allocation mechanisms.

Exam Focus: Slab Structure and Object Reuse

Explain the relationship between caches, slabs, pages, and individual object slots. Calculate how many objects fit in a slab before accounting for metadata and alignment. Distinguish empty, partial, and full slabs. Compare slab allocation with the buddy system and explain why the former is suitable for repeated small kernel-object allocations.

Key Terms of Slab Allocation

  • Slab allocator - Reuses fixed-size kernel objects.
  • Object cache - Holds objects of one type.
  • Slab - Group of allocated object slots.
  • Constructor - Performs initial object setup.
  • Per-CPU cache - Reduces processor allocation contention.
  • SLUB - A slab allocator implementation.
4
Easy
6
Medium
5
Hard

📝 All Slab allocation in Kernel Memory MCQs

Q1. A process requests a new stack page and the OS employs zero-fill-on-demand. Which description best matches the resulting physical memory state?

A.The page is copied from an existing page
B.The page is allocated and filled with zeros ✅
C.The page retains data from the previous process
D.The page is left uninitialized
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Zero-fill-on-demand means the operating system reserves a free physical page and writes zeros to every byte before handing it to the process. This guarantees that no leftover data is exposed, protecting security and ensuring a clean starting state. Option B accurately reflects this behavior. Options A and C describe copy-on-write, which is different, and D suggests no initialization, which would be unsafe.

Q2. When a child process modifies a copy-on-write page, what happens to the parent’s version of that page?

A.It is also marked dirty
B.It remains read‑only and unchanged ✅
C.It is deleted from memory
D.It is merged with the child’s changes
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: Copy-on-write ensures that only the process that writes to a page receives a private copy. The parent’s page stays read‑only and unchanged, allowing it to be shared safely. Therefore option B is correct. Option A is wrong because the parent does not become dirty; option C is incorrect because the page is still needed; option D is false because merging does not occur until a later copy‑on‑write fault.

Q3. A system has a pool of five free pages. Two pages are allocated for expanding a heap, and one page is used for a copy‑on‑write fault. How many free pages remain?

A.2
B.3 ✅
C.1
D.0
💡 Difficulty: easy | ✅ Correct: B

📖 Explanation: The pool started with five pages. After allocating two for heap growth, three remain. Allocating one more for a copy‑on‑write fault reduces the count to two, leaving three free pages. Option B correctly reflects the remaining pages. Options A, C, and D miscount the allocations, either forgetting the copy‑on‑write page or subtracting the wrong number.

Q4. Which OS mechanism allows a child process to share executable code pages with its parent without copying them?

A.Copy‑on‑write
B.Zero-fill-on-demand
C.Read‑only sharing ✅
D.vfork
💡 Difficulty: easy | ✅ Correct: C

📖 Explanation: Executable code pages are typically marked read‑only, allowing both parent and child to map the same physical pages safely. This sharing avoids unnecessary copies and conserves memory. Option C captures this mechanism. Copy‑on‑write (A) copies pages only when they are written, not for read‑only code. Zero-fill-on-demand (B) creates new zeroed pages, not shared ones. vfork (D) is a fork variant but does not itself define the sharing method.

Q5. A developer notices that after forking a process, the child’s memory usage spikes unexpectedly. Which scenario most likely explains this behavior?

A.The child writes to many stack pages, triggering copy‑on‑write copies ✅
B.The child immediately executes a different program via exec
C.The parent modifies shared library code
D.The OS uses lazy allocation for all pages
💡 Difficulty: medium | ✅ Correct: A

📖 Explanation: When a child writes to stack pages that were initially marked copy‑on‑write, each write causes the OS to allocate a private copy, increasing the child’s memory footprint. This matches option A. Executing a new program (B) would replace the address space, not increase usage. Modifying shared library code (C) is uncommon and would still be copy‑on‑write. Lazy allocation (D) affects page allocation, not copying after writes.

Q6. In a system that uses slab allocation for kernel objects, what advantage does allocating pages from a pre‑initialized slab provide when handling copy‑on‑write faults?

A.Reduces fragmentation
B.Ensures pages are zeroed
C.Speeds up allocation of fresh pages ✅
D.Improves cache locality
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Slab allocation keeps caches of pre‑initialized objects, allowing the kernel to quickly satisfy allocation requests without costly initialization each time. When a copy‑on‑write fault occurs, the OS can pull a ready‑to‑use page from the slab, speeding up the copy process. Option C reflects this speed benefit. Reducing fragmentation (A) is a general benefit but not specific to copy‑on‑write. Zeroing pages (B) is handled by zero‑fill‑on‑demand, not slab caches. Cache locality (D) is a secondary effect, not the primary advantage in this context.

Q7. Consider two processes sharing a read‑only code page. If the OS mistakenly marks the page as writable, what risk does this introduce?

A.Increased page‑fault latency
B.Potential data corruption between processes ✅
C.Higher CPU utilization
D.Reduced disk I/O
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Marking a shared code page as writable allows either process to modify the page, which can cause one process’s changes to affect the other, leading to unexpected behavior or crashes—i.e., data corruption. Option B captures this risk. Increased latency (A) might occur but is not the primary danger. Higher CPU usage (C) and reduced disk I/O (D) are unrelated to the correctness issue.

Q8. A system administrator observes that many copy‑on‑write pages are being allocated from the free page pool during a burst of process creation. Which tuning action is most likely to reduce this overhead?

A.Increase the size of the slab for kernel buffers
B.Enable aggressive page reclamation before forking ✅
C.Disable zero‑fill‑on‑demand
D.Reduce the maximum number of processes
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: Enabling aggressive page reclamation frees up pages before a fork, so the OS can satisfy copy‑on‑write demands without waiting for new allocations, thus lowering overhead. Option B is correct. Increasing slab size (A) helps kernel objects but not copy‑on‑write pages. Disabling zero‑fill‑on‑demand (C) would keep stale data and is unsafe. Reducing process limits (D) merely limits workload, not the allocation efficiency.

Q9. When a copy‑on‑write page is duplicated, which of the following metadata must be updated in the page tables of both parent and child?

A.Physical frame number only
B.Reference count only
C.Both reference count and protection bits ✅
D.Virtual address mapping only
💡 Difficulty: medium | ✅ Correct: C

📖 Explanation: Duplicating a copy‑on‑write page requires the OS to adjust the reference count (to reflect two owners) and change protection bits from read‑only to read‑write for the writer. Therefore both the count and protection bits are updated, making option C correct. Updating only the frame number (A) ignores reference tracking. Changing only the reference count (B) would leave protection incorrect. Virtual address mapping (D) stays the same for shared pages.

Q10. A program allocates a large buffer using malloc, then forks. The child immediately writes to the buffer. Which memory‑management behavior explains the observed increase in the child’s RSS?

A.Demand paging of zero‑fill pages
B.Copy‑on‑write of the buffer’s pages ✅
C.Swapping of unrelated pages
D.Lazy allocation of heap space
💡 Difficulty: medium | ✅ Correct: B

📖 Explanation: When the child writes to the newly allocated buffer, the pages that were shared copy‑on‑write are duplicated, causing the child’s resident set size (RSS) to grow. Option B captures this. Demand paging (A) concerns bringing pages into memory, not duplication. Swapping (C) moves pages to disk, not increase RSS. Lazy allocation (D) delays allocation but does not cause immediate RSS growth after a write.

Q11. Which statement best describes why zero‑fill‑on‑demand pages improve security in an OS that uses copy‑on‑write?

A.They prevent accidental overwrites of shared pages
B.They ensure that newly allocated pages contain no residual data ✅
C.They allow pages to be shared without copying
D.They speed up process creation
💡 Difficulty: hard | ✅ Correct: B

📖 Explanation: Zero‑fill‑on‑demand clears any leftover data from previous uses before a page is handed out, guaranteeing that no confidential information leaks to a new process. This security benefit is captured by option B. Preventing accidental overwrites (A) is a function of copy‑on‑write, not zero‑fill. Sharing without copying (C) is also a copy‑on‑write property. Speeding up creation (D) may be a side effect but is not the primary security reason.

Q12. During a high‑load benchmark, a system exhibits frequent copy‑on‑write faults on stack pages, leading to memory exhaustion. Which architectural change would most directly mitigate this problem?

A.Replace copy‑on‑write with eager copying
B.Increase the size of the physical memory pool
C.Use larger page sizes (e.g., 2 MiB huge pages) ✅
D.Disable paging entirely
💡 Difficulty: hard | ✅ Correct: C

📖 Explanation: Larger page sizes reduce the number of pages needed for a given memory region, so fewer copy‑on‑write faults occur for the same amount of stack growth, directly easing memory pressure. Option C is correct. Eager copying (A) would increase memory usage. Adding more physical memory (B) helps but does not change the fault frequency. Disabling paging (D) is impractical and unrelated.

Q13. A kernel developer proposes to store pre‑zeroed pages in a dedicated slab to serve copy‑on‑write requests faster. What potential drawback must the developer consider?

A.Increased CPU cycles for slab maintenance ✅
B.Higher risk of exposing stale data
C.Reduced flexibility for different page sizes
D.Potential fragmentation of the slab cache
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Maintaining a slab of pre‑zeroed pages adds overhead: the kernel must track allocation, deallocation, and periodic refilling, consuming CPU cycles. This cost is the main drawback, making option A correct. The pages are zeroed, so stale data risk (B) is low. Slabs can be designed for a single page size, so flexibility (C) is manageable. Fragmentation (D) is less of a concern because pages are uniform.

Q14. In a system that implements both copy‑on‑write and zero‑fill‑on‑demand, which sequence occurs when a forked child first writes to a previously unmodified page?

A.Zero‑fill, then copy‑on‑write duplication
B.Copy‑on‑write duplication, then zero‑fill
C.Only zero‑fill is performed
D.Only copy‑on‑write is performed ✅
💡 Difficulty: hard | ✅ Correct: D

📖 Explanation: When a page has never been written, it is typically marked copy‑on‑write at fork time. The first write by the child triggers the copy‑on‑write mechanism, which allocates a new page (often from the zero‑fill‑on‑demand pool) and copies the original content. However, the logical step is the copy‑on‑write duplication; zero‑fill occurs as part of allocating the new page but is not a separate step in the sequence presented. Thus option D best matches the described behavior. Options A and B misorder the operations, and C ignores the duplication entirely.

Q15. Which of the following best explains why executable code pages are never marked copy‑on‑write during a fork?

A.They are immutable and can be safely shared ✅
B.They are always read‑only due to hardware constraints
C.The OS lacks sufficient free pages for duplication
D.Copy‑on‑write only applies to heap pages
💡 Difficulty: hard | ✅ Correct: A

📖 Explanation: Executable code pages contain instructions that never change at runtime, making them immutable. Because they are read‑only, both parent and child can map the same physical pages without risking unintended modifications, eliminating the need for copy‑on‑write. Option A captures this reasoning. While hardware may enforce read‑only (B), the primary reason is immutability. Lack of free pages (C) is unrelated, and copy‑on‑write is not limited to heap pages (D).

🔗 Related Topics (MCQs)