📝 Kernel synchronization in The Linux System (18 MCQs)
Coordinating Concurrent Access to Kernel Data
Kernel synchronisation coordinates access to shared data and resources when multiple CPUs, interrupts, or kernel threads execute concurrently. Linux uses atomic operations, spinlocks, mutexes, reader-writer locks, semaphores, and read-copy-update mechanisms for different access patterns. These primitives protect critical sections, preserve data consistency, and prevent race conditions within the kernel's concurrent execution environment.
Why Shared Kernel State Needs Protection
On multicore systems, two processors may update the same structure simultaneously. Interrupt handlers and deferred work can also interact with data used by ordinary kernel code. Without synchronisation, updates may be lost or structures may become inconsistent. The selected primitive must reflect whether execution can sleep, whether interrupts are involved, and how long protection is required.
Spinlocks, Mutexes and Atomic Operations
A spinlock makes a competing CPU wait actively until the lock becomes available and is appropriate for short, non-sleeping critical sections. A mutex allows a task to sleep while waiting. Atomic operations protect simple updates, while RCU supports efficient read-mostly access through controlled update and reclamation procedures.
Example of Protecting a Shared Kernel Counter
Two kernel threads increment the same shared counter. Without protection, both may read 10 and write 11, losing one increment. An atomic increment or suitable lock ensures that both updates are accounted for, producing the correct final value of 12.
Concurrency, Scalability and Deadlock Risks
Correct synchronisation preserves kernel data integrity across CPUs and interrupt contexts. Locks introduce waiting and contention, reducing scalability when critical sections are lengthy. Incorrect lock ordering can cause deadlock, while excessive spinning wastes CPU time. Developers must also consider pre-emption and interrupt-state rules when selecting a locking primitive.
Misconceptions About Kernel Locks
'A spinlock allows the waiting task to sleep' is false because it spins while waiting. 'A mutex is suitable inside every interrupt handler' ignores that mutex acquisition may sleep. 'Atomic operations eliminate every race condition' is incorrect because compound operations and surrounding data access may still require broader protection.
Exam Focus: Choosing the Correct Synchronisation Primitive
Compare spinlocks, mutexes, semaphores, atomic operations, and RCU by their use cases. Determine whether a critical section can sleep or run in interrupt context. Questions may trace a lost update, identify deadlock from lock ordering, or explain why long-held spinlocks reduce multicore performance.
Key Terms of Linux Kernel Synchronisation
- Spinlock - Lock using active waiting.
- Mutex - Sleeping mutual exclusion lock.
- Atomic operation - Indivisible shared-state update.
- Critical section - Code accessing protected shared data.
- RCU - Read-copy-update synchronisation mechanism.
📝 All Kernel synchronization in The Linux System MCQs
Q1. A program creates two tasks using clone() with flags CLONE_FS and CLONE_VM only. Which resource is NOT shared between the parent and child?
📖 Explanation: The CLONE_FS flag shares the current working directory, and CLONE_VM shares the memory space, so both of those are common. Open file descriptors are shared only when CLONE_FILES is set, which is absent here, so they remain separate. Signal handlers are shared only with CLONE_SIGHAND, which also isn’t set, making them the only resource not shared. This helps developers decide which flag to add when they need specific isolation.
Q2. What does the CLONE_FILES flag cause the child task to share with its parent?
📖 Explanation: When CLONE_FILES is specified, the kernel makes the child use the same files_struct as the parent, meaning the file descriptor table is shared. Virtual memory requires CLONE_VM, signal handlers need CLONE_SIGHAND, and the process identifier is always unique. Knowing this lets programmers choose CLONE_FILES when they want threads to operate on the same set of open files.
Q3. Which system call in Linux creates a new task that can share resources with its parent?
📖 Explanation: clone() accepts a mask of flags that decide which resources—such as memory, file system, or file descriptors—are shared with the parent. fork() always creates a separate copy of resources, while exec() replaces the current process image, and wait() merely waits for a child to finish. Therefore, clone() is the call that enables selective sharing, essential for building user‑level threads.
Q4. A server program uses clone() with CLONE_VM, CLONE_FS, CLONE_FILES, but not CLONE_SIGHAND. After creating a child task, the parent changes its signal handler for SIGTERM. What happens in the child?
📖 Explanation: Because CLONE_SIGHAND was not set, the child has its own signal‑handler table. Changing the handler in the parent therefore does not affect the child, which continues to use the handler that existed at creation time. This isolation is useful when a thread must handle signals differently from the rest of the program.
Q5. A developer wants two threads to operate on the same memory but have independent file descriptor tables. Which combination of flags should be used?
📖 Explanation: The requirement is to share memory (CLONE_VM) while keeping file descriptors separate, so CLONE_FILES must be omitted. The simplest flag set that meets this is just CLONE_VM, which gives a shared address space and independent file‑descriptor tables. Adding extra flags like CLONE_FS or CLONE_SIGHAND would share additional resources that are not needed.
Q6. If a task created with clone() shares CLONE_VM and later calls execve(), what happens to the shared memory region?
📖 Explanation: execve() replaces the calling task’s entire address space with a new program image, discarding the previous memory mapping. Consequently, any memory that was previously shared via CLONE_VM is no longer visible to the other task, effectively making the region private to the calling task after the exec. This behavior prevents unintended sharing after a program change.
Q7. Why does Linux treat processes and threads as tasks rather than separate entities?
📖 Explanation: Linux stores each component of a process—such as its memory, file system, and signal tables—in separate kernel structures. The main task_struct then points to these subcontexts. This design lets the kernel treat both processes and threads uniformly as tasks, because sharing or isolating any component is just a matter of pointing to the same or different substructures.
Q8. How does the presence of independent subcontexts enable selective sharing of resources?
📖 Explanation: When a task is created, the clone() flags tell the kernel which subcontext pointers (e.g., to the memory descriptor or file‑descriptor table) should be duplicated and which should be shared. This pointer‑level control means the kernel can mix shared and private resources without merging entire processes, giving fine‑grained control over what is common between tasks.
Q9. Which flag indicates that the child’s file‑system information will be shared with the parent?
📖 Explanation: CLONE_FS explicitly tells the kernel to let the new task use the same struct path that stores the current working directory and other file‑system state. Without this flag, the child would receive a copy of the file‑system context, making its cwd independent. Knowing the flag semantics helps developers decide when two tasks should see the same directory hierarchy.
Q10. A multi‑threaded database server uses clone() with CLONE_VM, CLONE_FS, CLONE_FILES, and CLONE_SIGHAND. After a bug, one thread modifies the file descriptor table, causing other threads to lose access to a log file. Which flag’s sharing caused this side effect?
📖 Explanation: The problem arises because CLONE_FILES makes all tasks share the same files_struct, which contains the file descriptor table. When one thread closes or alters a descriptor, the change is visible to every other thread that shares that table. The other flags affect memory, cwd, and signal handlers, none of which impact file descriptor visibility.
Q11. Consider two tasks created with clone() using only CLONE_SIGHAND. The parent changes its current working directory using chdir(). What is the effect on the child?
📖 Explanation: Since only CLONE_SIGHAND was set, the child has its own file‑system context. Changing the directory in the parent therefore does not propagate to the child, which continues to operate in the directory that existed at the time of creation. This isolation is useful when tasks need different working directories while sharing signal handling.
Q12. A program creates a child task with clone() passing CLONE_VM | CLONE_FS | CLONE_SIGHAND, but not CLONE_FILES. The child opens a new file descriptor. How does this affect the parent?
📖 Explanation: Because CLONE_FILES was omitted, the child receives its own files_struct. Opening a descriptor modifies only that private table, leaving the parent’s descriptor table untouched. Consequently, the parent does not see the new descriptor at all. This behavior is crucial when a thread must work with temporary files without exposing them to the rest of the process.
Q13. A process spawns three tasks with different flag sets: Task A shares CLONE_VM | CLONE_FS, Task B shares CLONE_VM | CLONE_SIGHAND, Task C shares CLONE_FILES. Which pair of tasks can safely modify the same global variable without causing race conditions due to separate memory spaces?
📖 Explanation: Only tasks that do NOT share CLONE_VM have separate address spaces, meaning each has its own copy of any global variable. Task C does not share memory with either A or B, but A and B both share CLONE_VM, so they would see the same variable and could race. Since no pair has both separate memory and a common variable, none can safely modify the same global without additional synchronization.
Q14. Explain how the Linux kernel's use of independent subcontexts for file‑system, memory, and signal handling influences the design of user‑space threading libraries.
📖 Explanation: Thread libraries can create lightweight threads by invoking clone() with a chosen subset of flags, letting them share memory, file descriptors, or signal handlers as needed. The independent subcontexts give the kernel the flexibility to expose only the resources a library wants to share, simplifying the implementation of user‑level thread management while still providing isolation where desired.
Q15. What is the primary advantage of using clone() with a subset of flags over creating separate processes with fork() in terms of synchronization overhead?
📖 Explanation: When tasks share memory via CLONE_VM, they avoid the costly copying of pages that fork() would perform (even with copy‑on‑write). This reduces the amount of data the kernel must manage during a context switch, leading to faster inter‑task communication and lower latency for synchronization primitives. The trade‑off is that shared resources must be protected by user‑level synchronization.
Q16. A developer writes a program that creates a child via clone() with CLONE_VM | CLONE_FS | CLONE_FILES. After the child exits, the parent attempts to read from a file descriptor that the child closed. What is the expected behavior?
📖 Explanation: Because CLONE_FILES makes the file‑descriptor table shared, closing a descriptor in the child removes it for all tasks that share that table. When the parent later attempts to read from the now‑closed descriptor, the read call fails with an error (typically EBADF). This illustrates the need for careful coordination when sharing file descriptors.
Q17. Which kernel data structure holds the file‑descriptor table that can be shared between tasks?
📖 Explanation: The files_struct is the kernel object that contains the array of open file descriptors for a task. When CLONE_FILES is used, multiple task_structs point to the same files_struct, enabling descriptor sharing. The other structures store scheduling information (task_struct), memory mappings (mm_struct), or signal handlers (signal_struct). Understanding this mapping helps developers predict the impact of flag choices.
Q18. When two tasks share CLONE_SIGHAND but not CLONE_VM, how are signal handlers stored?
📖 Explanation: CLONE_SIGHAND makes the tasks point to the same signal_struct, which contains the array of signal handlers. Because CLONE_VM is not set, each task still has its own memory space, but they share the same handler definitions. This arrangement allows different threads to react to signals uniformly while keeping their memory isolated.