📝 Management of Physical Memory in The Linux System (25 MCQs)
Allocating and Reclaiming Physical Memory Pages
Linux physical memory management controls the allocation, tracking, and reclamation of actual RAM pages used by kernel components and user processes. The kernel organises memory into page frames and maintains information about their availability and purpose. Physical memory management works alongside virtual memory, allowing processes to use protected address spaces while the kernel maps them to physical RAM.
Why RAM Requires Structured Allocation
Processes and kernel subsystems request memory in different sizes and for different lifetimes. Managing every allocation as an unrelated region would create fragmentation and inefficient reuse. Linux therefore uses page-based allocation for physical frames and specialised allocators for smaller kernel objects, while accounting for hardware and NUMA constraints.
Buddy Allocation and Slab Based Object Allocation
The buddy allocator manages physical pages in power-of-two groups, splitting larger blocks to satisfy smaller requests and merging eligible free buddies on release. SLUB and related slab allocation mechanisms manage frequently used kernel objects efficiently. Memory zones and NUMA nodes help satisfy allocation requirements according to physical location and device constraints.
Example of a Multi Page Allocation
A kernel component requests eight contiguous pages. Since eight is a power of two, the buddy allocator seeks a free order-3 block. If only smaller separated blocks are available, the request may fail or require suitable reclamation and compaction, even when total free memory appears sufficient.
Fragmentation, Reclamation and Memory Locality
Physical memory management supports efficient allocation, DMA requirements, and kernel object reuse. External fragmentation can prevent large contiguous allocations despite sufficient total free pages. Reclaiming page-cache or anonymous memory may create overhead, while NUMA-remote access can increase latency. Allocation flags influence which memory zones and reclaim behaviours are suitable.
Misconceptions About Physical Memory Management
'Free memory means every allocation will succeed' ignores fragmentation and zone constraints. 'The buddy allocator handles every kernel object directly' overlooks slab-based object allocation. 'Physical memory is assigned permanently to one process' is false because pages can be reclaimed, remapped, or reused.
Exam Focus: Buddy Orders, Zones and Slab Allocation
Calculate the number of pages in a buddy allocation using , where is the allocation order. Distinguish page-frame allocation from slab object allocation. Questions may examine splitting and merging, memory zones, contiguous allocation failure, and the role of reclamation and NUMA placement.
Key Terms of Linux Physical Memory
- Page frame - Fixed-size unit of physical RAM.
- Buddy allocator - Allocates power-of-two page groups.
- SLUB - Allocator for kernel objects.
- Memory zone - Physical memory allocation region.
- Compaction - Rearranges pages to form larger free blocks.
📝 All Management of Physical Memory in The Linux System MCQs
Q1. A programmer wants to create a thread that shares the parent’s memory and file descriptors but not the signal handlers. Which combination of clone flags should be used?
📖 Explanation: The correct answer is A because CLONE_VM shares the virtual memory space and CLONE_FILES shares the open‑file table, exactly what the scenario requires. Option B shares filesystem information and signal handlers, which is unnecessary. Option C shares memory and signal handlers, violating the requirement to keep signal handlers separate. Option D shares filesystem and files but not memory, so the thread would have a distinct address space. In real code, using the right flags prevents unintended sharing of resources, improving program safety.
Q2. When a process calls fork() without any clone flags, which resources are shared with the child?
📖 Explanation: Option B is correct because fork() creates a new process with its own copies of all resources; nothing is shared between parent and child. Option A is false since fork does not share resources. Option C is incorrect because the file descriptor table is duplicated, not shared. Option D is wrong because the memory space is also duplicated. Understanding this behavior helps developers decide when to use fork versus clone for efficient resource sharing.
Q3. Which flag ensures that the child created by clone() shares the same current working directory as the parent?
📖 Explanation: CLONE_FS is the correct flag because it shares filesystem information, including the current working directory. CLONE_SIGHAND shares signal handlers, not filesystem data. CLONE_VM shares the memory space, and CLONE_FILES shares the file descriptor table. Choosing the appropriate flag is crucial when designing multithreaded programs that need to operate in the same directory, avoiding extra system calls to change directories later.
Q4. A system administrator observes that two threads created with clone() are interfering with each other's file writes. Which flag is likely missing from the clone call?
📖 Explanation: The missing flag is CLONE_FILES, which shares the open‑file table between the parent and child. Without it, each thread has its own file descriptor set, leading to separate file offsets and possible write conflicts. CLONE_VM shares memory, CLONE_FS shares filesystem metadata, and CLONE_SIGHAND shares signal handlers; none of these directly affect file descriptor sharing. Adding CLONE_FILES resolves the interference by ensuring both threads see the same file offsets.
Q5. If a developer wants a child process to have its own independent memory but share the same signal handlers as the parent, which clone flag combination should be used?
📖 Explanation: Option A is correct because CLONE_SIGHAND shares signal handlers while CLONE_VM would share memory, which is not desired; however, the question asks for independent memory, so only CLONE_SIGHAND should be used. Since none of the options provide just CLONE_SIGHAND, the closest is A, but it incorrectly includes CLONE_VM. Therefore the best answer is B, which uses only CLONE_SIGHAND, making the memory separate. This highlights the importance of selecting only needed flags.
Q6. A programmer uses clone() with CLONE_VM and CLONE_FS flags only. Which resources remain unshared?
📖 Explanation: The correct answer is B because CLONE_VM shares memory and CLONE_FS shares filesystem information, leaving signal handlers and the file descriptor table unshared. Option A lists resources that are actually shared. Option C incorrectly states memory is unshared. Option D incorrectly claims filesystem information is unshared. Recognizing which resources are shared helps avoid bugs where threads unintentionally affect each other's signals or file descriptors.
Q7. What does the CLONE_SIGHAND flag specifically cause to be shared between parent and child?
📖 Explanation: CLONE_SIGHAND shares the signal handler table, allowing both parent and child to use the same signal handling functions. Virtual memory pages are shared by CLONE_VM, the file descriptor table by CLONE_FILES, and the current working directory by CLONE_FS. Knowing the exact effect of each flag prevents accidental sharing of resources that could lead to race conditions or unexpected behavior in multithreaded programs.
Q8. Which of the following statements about clone() and fork() is true?
📖 Explanation: Option B is correct because clone() allows selective sharing via flags, whereas fork() always duplicates resources, resulting in separate copies. Option A reverses the behavior. Option C is false because fork() does not share file descriptors; it copies them. Option D is incorrect since the two system calls have distinct sharing capabilities. Understanding these differences is essential for developers choosing the appropriate mechanism for process or thread creation.
Q9. In Linux, which data structure holds a process’s virtual memory context separately from other contexts?
📖 Explanation: The mm_struct holds the virtual memory information, keeping it separate from other contexts like file descriptors or signal handlers. The task_struct is the main task descriptor, but it references mm_struct for memory. fs_struct stores filesystem information, and files_struct stores the open file table. Separating these structures enables Linux to share specific parts between tasks without duplicating the whole process state.
Q10. Why does Linux not need a distinct process‑level data structure to hold all context information?
📖 Explanation: Linux stores each context—filesystem, file descriptors, signal handlers, and virtual memory—in separate substructures, allowing selective sharing between tasks. This modular design eliminates the need for a single, large process structure. Option A is incorrect; Linux uses multiple structures. Option C is false because Linux does support multithreading via clone(). Option D misrepresents memory handling. This architecture simplifies resource sharing and improves performance.
Q11. If a thread created with clone() shares the same memory space as its parent, which of the following synchronization mechanisms is most appropriate to avoid race conditions?
📖 Explanation: Spinlocks provide low‑overhead protection for shared memory regions, making them ideal when threads share the same address space via CLONE_VM. Polling and busy‑wait loops are inefficient and can waste CPU cycles. Assuming no synchronization is needed is risky because concurrent writes can corrupt data. Using the proper lock ensures thread‑safe access to shared variables, reflecting real‑world multithreaded programming practices.
Q12. A developer wants a child task to inherit the parent’s open files but have a separate memory space and signal handlers. Which flag set achieves this?
📖 Explanation: CLONE_FILES shares the open file table while leaving memory (CLONE_VM) and signal handlers (CLONE_SIGHAND) unshared, matching the requirement. Option A shares only files but does not clarify other resources, which remain default separate copies—still correct but less explicit. Options B and C incorrectly share memory or signal handlers, violating the need for separation. Selecting the minimal required flag reduces overhead and prevents unintended sharing.
Q13. Which flag combination would you use to create a new task that behaves like a traditional Unix process, sharing no resources with its parent?
📖 Explanation: Using no flags (0) causes clone() to behave like fork(), creating a completely independent task with no shared resources. Combining any flags results in some shared state, contradicting the requirement for full independence. Therefore options B, C, and D are incorrect because they each enable sharing of memory, files, filesystem info, or signal handlers. This mirrors the typical use of fork() when isolation is needed.
Q14. When two threads share the same virtual memory via CLONE_VM, what is a likely consequence if they both write to the same variable without synchronization?
📖 Explanation: Sharing memory means both threads can access the same location concurrently, leading to data races if they modify it without coordination. The OS does not automatically lock variables (A) nor duplicate memory (C) when CLONE_VM is used. Option D is false because the kernel does not intervene in user‑level data races. Proper synchronization such as mutexes is required to maintain consistency.
Q15. What is the primary advantage of using clone() with specific flags over fork() when creating a new thread?
📖 Explanation: clone() lets the programmer choose exactly which resources to share, enabling efficient thread creation with shared memory, files, or signal handlers. fork() always duplicates all resources, which can be wasteful. While clone() may also use less CPU (C) due to reduced copying, the main benefit is the fine‑grained control over sharing (B). The kernel is still involved in both calls, so D is incorrect.
Q16. A system call returns -1 with errno set to ENOSYS. Which of the following is the most likely cause when invoking clone()?
📖 Explanation: ENOSYS indicates that the kernel does not implement the requested system call, often because the kernel version lacks clone() support or the specific flags are not recognized. Insufficient memory would yield ENOMEM, invalid flags might cause EINVAL, and file descriptor limits result in EMFILE. Recognizing ENOSYS helps developers diagnose compatibility issues across kernel versions.
Q17. Which of the following best describes how Linux achieves resource sharing between tasks created with clone()?
📖 Explanation: Linux keeps shared resources in separate substructures (e.g., mm_struct for memory) and allows multiple task_structs to point to the same substructure when appropriate flags are set. Copying all resources (A) would defeat sharing. Merging task_structs (C) is not how Linux works, and a global lock (D) would be inefficient and unnecessary.
Q18. A programmer observes that after using clone() with CLONE_VM, changes to a global array in the child are visible in the parent. Which concept does this illustrate?
📖 Explanation: When CLONE_VM is set, the parent and child share the same virtual memory, so modifications to shared data structures like a global array are immediately visible to both. This is an example of shared virtual memory. Copy‑on‑write (A) applies when memory is duplicated lazily, not when CLONE_VM forces sharing. Separate address spaces (B) and process isolation (D) are contrary to the observed behavior.
Q19. If a thread created with clone() shares the file descriptor table (CLONE_FILES) but not the memory space, what happens when the parent closes a file descriptor?
📖 Explanation: When CLONE_FILES is used, the parent and child share the same file descriptor table; thus closing a descriptor in the parent removes it for the child as well. The descriptors are not independent (A) and do not become invalid merely by being closed (C). The kernel does not generate an error for the child; the descriptor simply disappears (B). This behavior is crucial for coordinated resource management.
Q20. Consider a system where clone() is used with CLONE_VM and CLONE_SIGHAND, but not CLONE_FILES. Which of the following side effects is most likely?
📖 Explanation: Sharing signal handlers (CLONE_SIGHAND) without sharing file descriptors can lead to both tasks using the same handlers, so a change in one could affect the other unintentionally, causing overwritten handlers. File descriptor leaks (A) would require CLONE_FILES, which is not set. Memory corruption (C) relates to CLONE_VM but is less directly tied to signal handling. Deadlock (D) is possible but less likely than handler interference.
Q21. A developer needs to create a lightweight thread that shares memory and open files but must have a distinct current working directory. Which flag should be omitted?
📖 Explanation: Omitting CLONE_FS ensures that the child gets its own filesystem context, including a separate current working directory, while still sharing memory (CLONE_VM) and open files (CLONE_FILES). Including CLONE_SIGHAND is irrelevant to the directory. Therefore, CLONE_FS must be omitted. Options A and B would keep sharing memory and files, which is desired. Option D does not affect the directory.
Q22. When clone() is invoked with an invalid combination of flags that the kernel cannot reconcile, which error code is typically returned?
📖 Explanation: EINVAL indicates an invalid argument, which includes unsupported or contradictory flag combinations. EFAULT relates to bad memory addresses, ENOSYS signals an unimplemented system call, and EBUSY denotes a resource conflict. Therefore, an invalid flag set triggers EINVAL. Recognizing this error helps developers correct their clone() usage and avoid runtime failures.
Q23. A system administrator wants to limit the number of threads that can be created with clone() to improve stability. Which kernel parameter is most appropriate to adjust?
📖 Explanation: The /proc/sys/kernel/threads-max file controls the maximum number of threads the kernel will allow, directly affecting clone() thread creation. Swappiness (B) influences paging behavior, file-max (C) limits total file descriptors, and pid_max (D) caps process IDs. Adjusting threads-max helps prevent excessive thread creation that could exhaust system resources.
Q24. If two tasks share memory via CLONE_VM and one task performs a write to a page that is currently marked read‑only, what mechanism ensures the write succeeds?
📖 Explanation: When a page is read‑only, a write triggers a page‑fault; the kernel’s copy‑on‑write mechanism creates a private copy for the writer if needed, allowing the write to succeed while preserving isolation. Page‑level locking (B) does not change permissions. Signal delivery (C) is unrelated to memory protection. Automatic permission elevation (D) would break security guarantees. Understanding COW is essential for managing shared memory correctly.
Q25. A developer uses clone() with CLONE_VM and later calls execve() in the child. What happens to the shared memory after execve()?
📖 Explanation: execve() replaces the child’s address space but does not affect the existing shared memory region established by CLONE_VM; the memory remains shared unless explicitly unmapped. Therefore, the shared region persists, allowing communication after execve(). Option B is false because execve() does not duplicate memory. Option C is incorrect; memory is not discarded. Option D is not automatic. This behavior enables techniques like shared memory pre‑initialization before executing a new program.