📝 Process synchronization in Linux (115 MCQs)
📖 From Operating System • 5. Process Synchronization • 115 questions available
What is Process synchronization in Linux?
Definition:
Linux process synchronization offers futexes, POSIX pthread primitives, and System V IPC mechanisms, with futexes providing fast-path user-space locking backed by kernel wait queues.
Example:
A pthread mutex uses syscall only on contention, keeping uncontended entirely in user space with atomic .
Reason:
Futex optimization minimizes syscall overhead for the common uncontended case, making Linux synchronization highly scalable for multi-core systems while maintaining POSIX compatibility.
📝 All Process synchronization in Linux MCQs
Q1. Before version 2.6, the Linux kernel was characterized as:
📖 Explanation: The Linux kernel before version 2.6 was non-preemptive, meaning processes running in kernel mode could not be preempted even if higher-priority processes became available.
Q2. In the modern Linux kernel (2.6 and later), kernel preemption is:
📖 Explanation: The Linux kernel 2.6 and later is fully preemptive, allowing tasks to be preempted even when running in kernel mode, improving system responsiveness.
Q3. What happens to a higher-priority process in Linux 2.6 when a lower-priority process is executing in kernel mode?
📖 Explanation: In the fully preemptive Linux kernel (2.6+), a higher-priority process can preempt a lower-priority process even when it is executing in kernel mode, unlike the older non-preemptive kernel.
Q4. The change from non-preemptive to preemptive kernel in Linux primarily improves:
📖 Explanation: Preemptive kernels allow higher-priority processes to interrupt lower-priority kernel operations, improving system responsiveness and real-time performance characteristics.
Q5. Which of the following best describes the preemption capability of the Linux kernel before version 2.6?
📖 Explanation: Before version 2.6, the Linux kernel was non-preemptive, meaning a process running in kernel mode could not be preempted. Preemption was only possible when tasks were executing in user mode.
Q6. The primary disadvantage of a non-preemptive kernel design is:
📖 Explanation: In a non-preemptive kernel, high-priority tasks cannot interrupt lower-priority tasks running in kernel mode, leading to poor responsiveness and potential latency issues for time-sensitive operations.
Q7. How does the Linux kernel determine if a task can be safely preempted?
📖 Explanation: The Linux kernel uses the preempt_count counter in the task's thread-info structure to determine if preemption is safe. If preempt_count is greater than 0, the task holds locks and cannot be safely preempted.
Q8. A system administrator notices poor responsiveness in a Linux 2.4 kernel system. Which upgrade would most directly address this issue?
📖 Explanation: Upgrading to Linux 2.6+ provides a fully preemptive kernel, which directly addresses responsiveness issues by allowing higher-priority processes to preempt lower-priority kernel operations.
Q9. In the context of Linux kernel preemption, what does 'preemption' refer to?
📖 Explanation: Preemption refers to the involuntary interruption of a currently running process by the scheduler to allow another process to execute, typically based on priority or time quantum considerations.
Q10. Which scenario demonstrates the benefit of preemptive kernel design in Linux?
📖 Explanation: A preemptive kernel allows a high-priority real-time audio application to preempt a lower-priority background process even when the background process is executing in kernel mode, preventing unacceptable audio delays.
Q11. The transition to a preemptive kernel in Linux 2.6 primarily benefits which type of system?
📖 Explanation: Desktop and real-time systems benefit most from preemptive kernels as they require responsive interaction with users and time-sensitive applications, while server workloads may not show as dramatic improvements.
Q12. What architectural change was required to support full kernel preemption in Linux 2.6?
📖 Explanation: Full kernel preemption required implementing preemption control mechanisms including the preempt_disable() and preempt_enable() functions, and the preempt_count counter to track lock acquisition and ensure safe preemption points.
Q13. When a task running in kernel mode holds a lock in Linux, preemption is:
📖 Explanation: When a task holds a lock, preemption is disabled by incrementing the preempt_count. The kernel cannot be safely preempted while locks are held to prevent corruption and deadlocks.
Q14. Which of the following is NOT a characteristic of a preemptive kernel?
📖 Explanation: Preemptive kernels actually have more complex synchronization requirements because synchronization primitives must be designed to handle potential preemption at any point during critical section execution.
Q15. The Linux kernel's preemption model directly impacts which system characteristic?
📖 Explanation: The preemption model directly impacts interrupt latency and system responsiveness, as preemptible kernels can respond more quickly to interrupts and high-priority tasks.
Q16. What data type does Linux use for atomic integer operations?
📖 Explanation: Linux uses the opaque data type atomic_t for atomic integer operations. This type ensures all operations performed on it are executed without interruption.
Q17. Which function is used to set an atomic integer to a specific value in Linux?
📖 Explanation: The atomic_set() function is used to assign a value to an atomic_t variable. For example, atomic_set(&counter, 5) sets the atomic integer counter to 5.
Q18. What is the primary advantage of using atomic integers over locking mechanisms?
📖 Explanation: Atomic integers are more efficient than locking mechanisms because atomic operations are performed by the hardware without interruption, eliminating the overhead of acquiring and releasing locks.
Q19. Which operation adds a value to an atomic integer in Linux?
📖 Explanation: The atomic_add() function adds a specified value to an atomic integer. For example, atomic_add(10, &counter) adds 10 to the counter variable.
Q20. What is the limitation of using atomic integers for synchronization?
📖 Explanation: Atomic integers are limited to protecting a single integer variable. When multiple variables contribute to a possible race condition, more sophisticated locking mechanisms like mutexes or semaphores must be used.
Q21. In the Linux kernel, an atomic integer operation is guaranteed to be:
📖 Explanation: Atomic integer operations are guaranteed to be executed without interruption. This means the operation completes fully before any other process or interrupt can access the variable.
Q22. Given the following code sequence: atomic_set(&counter, 5); atomic_add(10, &counter); atomic_sub(4, &counter); atomic_inc(&counter); What is the final value of counter?
📖 Explanation: Starting with counter=5: add 10 gives 15, subtract 4 gives 11, increment by 1 gives 12. The final value is 12.
Q23. Which function reads the current value of an atomic integer in Linux?
📖 Explanation: The atomic_read() function returns the current value of an atomic integer. For example, value = atomic_read(&counter) reads the current value of counter into the variable value.
Q24. Why are atomic integers considered opaque data types in Linux?
📖 Explanation: Atomic integers are opaque data types, meaning their internal structure and representation are hidden. Developers must use the provided atomic operations functions rather than accessing the data directly.
Q25. What hardware feature enables atomic integer operations on most architectures?
📖 Explanation: Atomic integer operations rely on special atomic instructions provided by the CPU instruction set architecture, such as compare-and-exchange or test-and-set, which guarantee uninterruptible execution.
Q26. The atomic_inc() function performs which operation?
📖 Explanation: The atomic_inc() function increments an atomic integer by exactly 1. It is a convenience function equivalent to atomic_add(1, &counter).
Q27. In which scenario would atomic integers be inappropriate for synchronization?
📖 Explanation: Atomic integers are inappropriate for protecting complex data structures with multiple variables because they can only provide atomicity for a single integer. Multiple variables require locks like mutexes or semaphores.
Q28. What is the correct way to decrement an atomic integer by 1 in Linux?
📖 Explanation: Both atomic_dec() and atomic_sub(1, &counter) correctly decrement an atomic integer by 1. Linux provides both the convenience function atomic_dec() and the more general atomic_sub() function.
Q29. How does Linux ensure atomic integer operations are thread-safe?
📖 Explanation: Linux combines hardware atomic instructions with compiler barriers to ensure atomic integer operations are thread-safe. The atomic_t type and its operations include the necessary memory barriers to prevent compiler optimizations from reordering operations.
Q30. Which of the following operations is NOT typically provided for atomic_t in Linux?
📖 Explanation: Linux atomic_t operations do not include atomic_multiply(). The atomic operations are limited to addition, subtraction, increment, decrement, set, read, and various bit operations.
Q31. When using atomic integers, the operation is performed:
📖 Explanation: Atomic integers perform operations without the overhead of locking mechanisms. They use hardware atomic instructions that guarantee uninterruptible execution without requiring locks.
Q32. What would happen if a standard integer operation is used instead of atomic_t in a critical path?
📖 Explanation: Using standard integer operations instead of atomic_t can lead to race conditions because the operation may be interrupted, leaving the variable in an inconsistent state. Atomic_t operations ensure atomicity.
Q33. The atomic_t data type in Linux is designed to be:
📖 Explanation: The atomic_t data type implementation is platform-dependent, with different CPU architectures providing different atomic instructions. However, the interface remains consistent across platforms.
Q34. What is the purpose of making atomic_t an opaque type?
📖 Explanation: Making atomic_t opaque prevents developers from directly accessing or modifying the underlying data, ensuring that all operations go through the atomic functions which maintain atomicity guarantees.
Q35. Which operation would you use to add 5 to an atomic integer named count?
📖 Explanation: The atomic_add(5, &count) function adds 5 to the atomic integer count. The function takes the value to add as the first parameter and a pointer to the atomic_t variable as the second.
Q36. What is the typical use case for atomic integers in the Linux kernel?
📖 Explanation: Atomic integers are commonly used for reference counters, usage counters, and simple statistical counters where only a single integer needs to be updated atomically without the overhead of locks.
Q37. If atomic operations were not available, what would be the alternative for simple counter updates?
📖 Explanation: Without atomic operations, simple counter updates would need protection using spinlocks or mutex locks. However, locks introduce significant overhead for what should be a simple operation, making atomic operations more efficient.
Q38. The atomic_sub() function in Linux:
📖 Explanation: The atomic_sub() function subtracts a specified value from an atomic integer. For example, atomic_sub(4, &counter) subtracts 4 from the counter variable.
Q39. Which of the following correctly declares an atomic integer in Linux?
📖 Explanation: The correct declaration for an atomic integer in Linux is 'atomic_t counter;'. This declares a variable named counter of type atomic_t that can be used with atomic operations.
Q40. Why might atomic integers be insufficient for protecting a shared data structure?
📖 Explanation: Atomic integers cannot maintain consistency across multiple variables. When multiple variables need to be updated together to maintain data structure integrity, locks like mutexes or spinlocks must be used.
Q41. In a multi-core system, how do atomic integer operations maintain consistency?
📖 Explanation: Atomic integer operations on multi-core systems rely on cache coherency protocols and atomic instructions that ensure all CPU caches see the same value when an atomic operation is performed.
Q42. Which function must be called to acquire a mutex lock in the Linux kernel?
📖 Explanation: The mutex_lock() function is used to acquire a mutex lock before entering a critical section in the Linux kernel. It handles blocking if the mutex is currently held by another task.
Q43. What happens when a task calls mutex_lock() and the mutex is already held by another task in Linux?
📖 Explanation: When a mutex is held by another task, calling mutex_lock() puts the calling task into a sleep state. The task is awakened when the lock's owner invokes mutex_unlock().
Q44. Which function releases a mutex lock in the Linux kernel?
📖 Explanation: The mutex_unlock() function releases a mutex lock after a task exits its critical section. This function may wake up one of the tasks waiting on the mutex.
Q45. What is the primary advantage of mutex locks over spinlocks in Linux?
📖 Explanation: Mutex locks are more appropriate for long critical sections because tasks waiting for a mutex enter a sleep state and do not consume CPU cycles, whereas spinlocks waste CPU cycles spinning.
Q46. A mutex lock in Linux is best suited for:
📖 Explanation: Mutex locks are appropriate when a lock must be held for a longer period. Tasks waiting for a mutex sleep rather than spin, making them efficient for longer-duration critical sections.
Q47. When a task holding a mutex invokes mutex_unlock(), what may occur?
📖 Explanation: When mutex_unlock() is called, the mutex is released, and one of the tasks waiting on the mutex may be awakened to acquire the lock and enter its critical section.
Q48. Why are mutex locks inappropriate for use in interrupt handlers in Linux?
📖 Explanation: Mutex locks should not be used in interrupt handlers because they can put the calling task to sleep, and interrupt handlers cannot sleep. Spinlocks are the preferred synchronization mechanism for interrupt context.
Q49. What does the Linux kernel do when a task attempts to acquire a held mutex?
📖 Explanation: When a mutex is held, the calling task is placed in a sleep state and added to the mutex's wait queue. The task will be awakened when the mutex becomes available.
Q50. In what context are mutex locks typically used in Linux?
📖 Explanation: Mutex locks are designed for process context where tasks can sleep. They should not be used in interrupt context, atomic contexts, or contexts where sleeping is not permitted.
Q51. What happens to a task waiting on a mutex when the lock owner is preempted?
📖 Explanation: If the lock owner is preempted while holding a mutex, waiting tasks remain asleep. They will be awakened only when the owner resumes execution and calls mutex_unlock() to release the mutex.
Q52. Which synchronization primitive in Linux puts a task to sleep when the lock is unavailable?
📖 Explanation: Mutex locks put tasks to sleep when they attempt to acquire an already-held lock. This is in contrast to spinlocks, which busy-wait and consume CPU cycles.
Q53. How does a mutex compare to a binary semaphore in Linux?
📖 Explanation: Mutexes have ownership tracking, meaning they know which task holds the lock. This enables features like priority inheritance and ensures that only the owner can unlock the mutex. Binary semaphores do not track ownership.
Q54. What is a critical section in the context of Linux mutex locks?
📖 Explanation: A critical section is a section of code that accesses shared resources and must be executed atomically to prevent race conditions. Mutex locks protect critical sections by ensuring mutual exclusion.
Q55. When using mutex locks, what is the correct order of function calls?
📖 Explanation: The correct order is to call mutex_lock() before entering the critical section and mutex_unlock() after exiting. This ensures the critical section is protected and the mutex is properly released.
Q56. In the Linux kernel, mutex locks are preferred over spinlocks when:
📖 Explanation: Mutex locks are preferred for long-duration locks because waiting tasks sleep rather than spin, conserving CPU resources. Spinlocks are preferred for short-duration locks.
Q57. What is the potential issue with holding a mutex lock for an extended period?
📖 Explanation: Holding a mutex lock for an extended period can cause starvation of waiting tasks that need to access the protected resource. This is why mutex locks, while appropriate for longer durations, should still be held as briefly as possible.
Q58. Which statement about mutex locks and interrupts in Linux is correct?
📖 Explanation: Mutex locks should be used only in process context where sleeping is allowed. In interrupt context, spinlocks or other non-sleeping synchronization mechanisms must be used.
Q59. What happens if a task tries to unlock a mutex it does not own in Linux?
📖 Explanation: If a task attempts to unlock a mutex it does not own, the kernel will generate an error, potentially causing a kernel panic. Mutex locks enforce ownership to prevent unauthorized unlocking.
Q60. Which function is used to conditionally acquire a mutex without blocking in Linux?
📖 Explanation: mutex_trylock() attempts to acquire the mutex and returns immediately regardless of whether the mutex is available. It returns 0 if the mutex was acquired and a negative value if the mutex is held by another task.
Q61. How does Linux handle priority inversion with mutex locks?
📖 Explanation: Linux mutexes implement priority inheritance to handle priority inversion. If a high-priority task is blocked on a mutex held by a low-priority task, the low-priority task inherits the high priority until the mutex is released.
Q62. On an SMP machine, what is the fundamental locking mechanism in Linux?
📖 Explanation: On SMP machines, spinlocks are the fundamental locking mechanism in the Linux kernel. The kernel is designed so spinlocks are held only for short durations.
Q63. Why are spinlocks unsuitable for use on single-processor machines in Linux?
📖 Explanation: On single-processor machines, spinlocks are unsuitable because a task holding a spinlock cannot be preempted, so other tasks spinning for the lock waste CPU cycles without any chance of the lock being released.
Q64. How does Linux handle the equivalent of spinlocks on single-processor systems?
📖 Explanation: On single-processor systems, spinlocks are replaced by disabling and enabling kernel preemption. Rather than acquiring a spinlock, the kernel disables preemption; and rather than releasing a spinlock, it enables preemption.
Q65. What is the primary characteristic of a spinlock in Linux?
📖 Explanation: A spinlock causes the task to busy-wait (spin) until the lock becomes available. This spinning consumes CPU cycles but can be efficient for very short critical sections.
Q66. In Linux, spinlocks should be held only for:
📖 Explanation: Spinlocks in Linux should be held only for short durations because tasks waiting on a spinlock busy-wait, consuming CPU cycles. Holding spinlocks for extended periods wastes CPU resources.
Q67. What replaces a spinlock on a single-processor Linux system?
📖 Explanation: On single-processor systems, spinlocks are replaced by disabling kernel preemption. The kernel disables preemption instead of acquiring a spinlock and enables preemption instead of releasing the spinlock.
Q68. When a spinlock is held on an SMP system, what is the state of the tasks waiting for the lock?
📖 Explanation: Tasks waiting for a spinlock on an SMP system enter a busy-wait (spin) state. They continuously check the lock variable until it becomes available, consuming CPU cycles in the process.
Q69. Which Linux synchronization primitive is most efficient for protecting short critical sections on a multi-core system?
📖 Explanation: Spinlocks are most efficient for short critical sections on multi-core systems because the overhead of acquiring and releasing spinlocks is minimal, and waiting tasks can quickly acquire the lock when released.
Q70. What is the relationship between kernel preemption and spinlocks in Linux?
📖 Explanation: Spinlocks in Linux automatically disable kernel preemption when acquired. This ensures the task holding the spinlock cannot be preempted, preventing deadlocks and ensuring the lock is released quickly.
Q71. Which of the following scenarios would best use a spinlock in Linux?
📖 Explanation: Spinlocks are best for protecting short operations like updating a single variable. For operations involving large data structures or longer durations, mutex locks or semaphores would be more appropriate.
Q72. Why must spinlocks be held only for short durations in Linux?
📖 Explanation: Spinlocks must be held for short durations to avoid wasting CPU cycles on busy-waiting. When a spinlock is held, other tasks waiting for the lock continuously consume CPU cycles, reducing overall system performance.
Q73. On a single-processor system, what is the effect of disabling kernel preemption instead of acquiring a spinlock?
📖 Explanation: Disabling kernel preemption on a single-processor system prevents the current task from being preempted by the scheduler, achieving the same mutual exclusion effect as a spinlock without busy-waiting.
Q74. What happens when a spinlock is acquired and the CPU is interrupted on an SMP system?
📖 Explanation: If an interrupt handler attempts to acquire a spinlock already held by the interrupted task, a deadlock can occur. This is why spinlocks in interrupt context require careful design, often using spin_lock_irqsave() to disable interrupts.
Q75. Which variant of spinlock is most appropriate for use in an interrupt handler in Linux?
📖 Explanation: spin_lock_irqsave() is the most appropriate variant for interrupt handlers because it disables interrupts on the local CPU and saves the interrupt flags. This prevents deadlocks that could occur if the handler were interrupted by another interrupt.
Q76. What is the advantage of spinlocks over mutex locks in the Linux kernel?
📖 Explanation: Spinlocks are suitable for interrupt context because they don't sleep. Mutex locks cannot be used in interrupt context as they may block, which is not permitted in interrupt handlers.
Q77. On an SMP system, what is the process for acquiring a spinlock?
📖 Explanation: On SMP systems, tasks acquiring a spinlock check the lock variable and enter a busy-wait loop if the lock is held by another task. They keep checking the variable until it becomes available.
Q78. What is the primary drawback of using spinlocks in the Linux kernel?
📖 Explanation: The primary drawback of spinlocks is that waiting tasks consume CPU cycles while spinning, which wastes CPU resources. This is why spinlocks should only be used for very short critical sections.
Q79. In the Linux kernel, semaphores are most appropriate for:
📖 Explanation: Semaphores in Linux are appropriate for critical sections that must be held for longer periods. When a task cannot acquire a semaphore, it can sleep, making semaphores efficient for longer-duration locks.
Q80. Which synchronization primitives in Linux have reader-writer versions?
📖 Explanation: Linux provides reader-writer versions of both spinlocks and semaphores. These allow multiple readers to access a resource concurrently while providing exclusive access for writers.
Q81. What distinguishes reader-writer semaphores from regular semaphores in Linux?
📖 Explanation: Reader-writer semaphores allow multiple readers to hold the lock concurrently while providing exclusive access for writers. This improves concurrency when read operations are more frequent than write operations.
Q82. Why are semaphores preferred over spinlocks for long critical sections?
📖 Explanation: Semaphores allow tasks that cannot acquire the lock to sleep, not consuming CPU cycles while waiting. This makes them more efficient for long critical sections compared to spinlocks, which busy-wait.
Q83. Which of the following is NOT a use case for semaphores in the Linux kernel?
📖 Explanation: While semaphores could protect a single variable, atomic integers are more efficient for this use case. Semaphores are overkill for simple variable updates and are better suited for complex resource management.
Q84. What is the primary difference between a mutex lock and a counting semaphore in Linux?
📖 Explanation: Mutex locks are designed for mutual exclusion with a binary lock state, while counting semaphores maintain a counter that can be used to manage multiple resources, allowing multiple concurrent users up to the counter value.
Q85. In Linux, a reader-writer lock is most beneficial when:
📖 Explanation: Reader-writer locks are most beneficial when there are many readers and few writers. Multiple readers can access the resource concurrently, increasing parallelism and performance for read-heavy workloads.
Q86. What happens when a task attempts to acquire a semaphore with a count of zero in Linux?
📖 Explanation: When a task attempts to acquire a semaphore with a count of zero, the task enters a sleep state until the semaphore is released by another task, increasing its count.
Q87. When should a spinlock be used instead of a semaphore in the Linux kernel?
📖 Explanation: Spinlocks should be used for protecting very short operations that cannot sleep, such as simple data structure updates. Semaphores should be used for longer operations or when sleeping is acceptable.
Q88. What is the primary advantage of reader-writer semaphores in Linux?
📖 Explanation: Reader-writer semaphores allow greater concurrency because multiple readers can hold the lock simultaneously. This improves performance when read operations are frequent and write operations are rare.
Q89. Which system call disables kernel preemption in Linux?
📖 Explanation: The preempt_disable() system call disables kernel preemption in Linux. When preemption is disabled, the current task cannot be preempted, even if higher-priority tasks become available.
Q90. Which system call enables kernel preemption in Linux?
📖 Explanation: The preempt_enable() system call enables kernel preemption in Linux. When preemption is enabled, the scheduler is allowed to preempt the current task if a higher-priority task is available.
Q91. What does the preempt_count variable in Linux track?
📖 Explanation: The preempt_count variable tracks the number of locks held by a task. When preempt_count is greater than 0, the kernel cannot be safely preempted because the task holds one or more locks.
Q92. When is it safe to preempt the kernel in Linux?
📖 Explanation: It is safe to preempt the kernel when preempt_count equals 0, indicating the task holds no locks and there are no outstanding calls to preempt_disable(). In this state, the kernel can be safely interrupted.
Q93. What happens when a task acquires a lock in Linux regarding preemption?
📖 Explanation: When a task acquires a lock, preempt_count is incremented to indicate that the task holds a lock and should not be preempted. The counter is decremented when the lock is released.
Q94. What is the purpose of the thread-info structure in Linux?
📖 Explanation: The thread-info structure contains the preempt_count counter that tracks the number of locks held by a task. This information is used to determine whether it is safe to preempt the kernel.
Q95. If preempt_count is greater than 0 for a task in the Linux kernel, what does this indicate?
📖 Explanation: A preempt_count greater than 0 indicates the task holds at least one lock. The kernel cannot be safely preempted while the task holds locks, as this could lead to corrupted data or deadlocks.
Q96. Which of the following would cause the kernel to NOT be preemptible in Linux?
📖 Explanation: The kernel is not preemptible when a task holds a lock (preempt_count > 0). This ensures that critical sections are not interrupted, preventing race conditions and maintaining data integrity.
Q97. What is the relationship between preempt_disable() calls and preempt_count?
📖 Explanation: Each call to preempt_disable() increments preempt_count. Multiple calls allow nested disabling of preemption, with preemption only being re-enabled when the number of enable calls matches the number of disable calls.
Q98. What does a call to preempt_enable() check after decrementing preempt_count?
📖 Explanation: After decrementing preempt_count, preempt_enable() checks if the counter has reached zero. If preempt_count is zero and there are pending reschedule requests, preempt_enable() invokes the scheduler to preempt the current task if appropriate.
Q99. Why does Linux maintain preempt_count per task rather than globally?
📖 Explanation: preempt_count is maintained per task because each task independently holds locks and controls its own preemption state. This allows the kernel to determine on a per-task basis whether preemption is safe.
Q100. If a task has preempt_count = 2, and then releases one lock, what is the new value of preempt_count?
📖 Explanation: When a lock is released, preempt_count is decremented. Starting from 2 and decrementing by 1 gives 1. The kernel can be preempted only when preempt_count reaches 0, so preemption remains disabled.
Q101. What is the implication of nested calls to preempt_disable() in Linux?
📖 Explanation: Nested calls to preempt_disable() are allowed and each call increments preempt_count. Each preempt_disable() must be matched with a corresponding preempt_enable() call to properly re-enable preemption.
Q102. What prevents the kernel from being preempted when a task holds locks in Linux?
📖 Explanation: When a task holds locks, preempt_count is non-zero, indicating that the kernel cannot be safely preempted. The scheduler respects this by not preempting tasks with preempt_count > 0.
Q103. How does Linux ensure that a task holding a spinlock is not preempted?
📖 Explanation: Spinlocks in Linux increment preempt_count when acquired, ensuring the task cannot be preempted. This prevents the task from being interrupted while it holds the spinlock, maintaining data integrity.
Q104. What is the preempt_count value when a task holds a mutex lock in Linux?
📖 Explanation: When a task holds a mutex lock, preempt_count is incremented (typically to 1 or higher). This indicates the task holds a lock and should not be preempted until the lock is released.
Q105. In the Linux kernel, preemption is disabled when:
📖 Explanation: Preemption is disabled when a task has preempt_count > 0, indicating it holds locks. The kernel cannot be safely preempted in this state, as the task needs to complete its critical section without interruption.
Q106. Which of the following correctly describes the preemption state when a task holds a spinlock?
📖 Explanation: When a task holds a spinlock, preemption is disabled. This prevents the task from being interrupted while in the critical section protected by the spinlock, avoiding race conditions.
Q107. The thread-info structure in Linux contains the preempt_count counter. What is the significance of this structure being per-task?
📖 Explanation: The per-task preempt_count allows independent preemption control for each task. This means each task can independently disable preemption without affecting other tasks, providing fine-grained control.
Q108. What is the purpose of the preempt_count counter being updated on lock acquisition and release?
📖 Explanation: preempt_count is updated to track the number of locks held by a task. This information determines whether the kernel can be safely preempted: preemption is safe only when preempt_count is zero.
Q109. How does Linux handle preemption when a task has preempt_count > 0 and a higher-priority task becomes available?
📖 Explanation: If preempt_count > 0, the task holds locks and cannot be safely preempted. The higher-priority task must wait until preempt_count returns to 0, at which point preemption becomes safe and the scheduler can act.
Q110. What is the purpose of the preempt_disable() system call in Linux?
📖 Explanation: preempt_disable() is used to prevent the current task from being preempted. This is useful when executing code that cannot be interrupted, such as critical sections protected by locks.
Q111. When a task with preempt_count = 0 is running in the kernel, what can occur?
📖 Explanation: When preempt_count = 0, the task holds no locks and the kernel can be safely preempted. This allows higher-priority tasks to preempt the current task even when it's running in kernel mode.
Q112. What is the relationship between lock acquisition and preempt_count in Linux?
📖 Explanation: In Linux, lock acquisition increments preempt_count. This ensures that when a task holds locks, the kernel cannot be preempted, preventing race conditions and maintaining data integrity during critical sections.
Q113. Which scenario would not prevent kernel preemption in Linux?
📖 Explanation: A task with preempt_count = 0 does not prevent kernel preemption. This means the kernel can be safely preempted even if the task is running in kernel mode, as it holds no locks.
Q114. When multiple locks are held by a task in Linux, how does this affect preempt_count?
📖 Explanation: preempt_count tracks the exact number of locks held by a task. When a task holds multiple locks, preempt_count equals the total number of locks held, ensuring preemption remains disabled until all locks are released.
Q115. What would happen if the Linux kernel allowed preemption while a task holds a lock?
📖 Explanation: Allowing preemption while a task holds a lock could lead to race conditions and data corruption. The lock's critical section would be interrupted, potentially leaving shared data in an inconsistent state and causing crashes or data corruption.