📝 Process Synchronization in operating system (36 MCQs)
Coordinating Concurrent Processes and Shared Resources
Process synchronization is a process-management mechanism that coordinates concurrent processes or threads accessing shared data and resources. It ensures that operations execute in an appropriate order and that conflicting updates do not corrupt shared information. Synchronization mechanisms include critical sections, mutex locks, semaphores, monitors, and hardware atomic instructions. The operating system and application programs use these mechanisms to preserve data consistency during concurrent execution.
Why Concurrent Execution Creates Race Conditions
Multiple processes may read and modify the same variable, file, or shared-memory region. When their instructions interleave unpredictably, the final result can depend on execution timing. This race condition makes program behaviour unreliable. Synchronization establishes controlled access and ordering, preventing conflicting operations while preserving concurrency wherever simultaneous execution is safe.
Mutual Exclusion, Progress, and Bounded Waiting
A process enters a critical section after acquiring an appropriate synchronization mechanism. Other competing processes must wait until the protected operation finishes. A correct critical-section solution aims to provide mutual exclusion, progress, and bounded waiting. The implementation may use atomic hardware instructions, blocking primitives, or higher-level constructs such as monitors.
Race Condition During a Shared Counter Update
Suppose two threads increment a shared counter initially containing 10. Both read 10 before either writes the updated value. Each calculates 11 and stores it, leaving the counter at 11 instead of the expected 12. Protecting the increment operation with a mutex ensures that both updates occur without conflicting interleaving.
Data Consistency, Concurrency, and Synchronization Overhead
Synchronization protects shared information in databases, operating system kernels, file systems, and multithreaded applications. It prevents race conditions and supports reliable resource allocation. However, excessive locking increases waiting time and reduces parallelism. Incorrect lock ordering can cause deadlocks, while unfair access policies can produce starvation among competing execution activities.
Misconceptions About Concurrent Process Execution
'Synchronization always means executing processes sequentially' is incorrect because independent operations can remain concurrent. 'Atomic variables eliminate every race condition' is incorrect because compound operations may still conflict. 'Mutual exclusion guarantees bounded waiting' is incorrect because a lock may prevent simultaneous entry while still allowing indefinite postponement.
Exam Focus: Race Conditions and Critical-Section Requirements
Trace concurrent instruction sequences and identify race conditions involving shared variables. Explain mutual exclusion, progress, and bounded waiting, distinguishing their individual requirements. Compare mutexes, semaphores, monitors, and hardware atomic operations. Questions may present interleaved execution sequences and ask you to determine the final shared value or identify an appropriate synchronization mechanism.
Key Terms of Process Synchronization
- Race condition - Result dependent on execution timing.
- Critical section - Code accessing protected shared data.
- Mutual exclusion - Prevents simultaneous protected access.
- Progress - Ensures eligible entry decisions proceed.
- Bounded waiting - Limits postponement before entry.
- Atomicity - Guarantees indivisible operation execution.
📝 All Process Synchronization in operating system MCQs
Q1. Two worker threads each execute 100 separate read, increment, and write operations on a shared counter initially equal to 0. No synchronization is used. Which result is possible?
📖 Explanation: Each increment is a read-modify-write sequence, not necessarily one indivisible operation. If threads read the same old value, one update can overwrite the other. Lost updates can therefore leave the final counter below 200.
Q2. Which statement best describes the purpose of process synchronization when several processes share data?
📖 Explanation: Synchronization controls the ordering of operations that share or depend on common state. It does not require unrelated work to run sequentially, and it does not replace CPU scheduling or guarantee equal processor time.
Q3. A trace records P1 inside a critical section from time 1 to 5 and P2 inside it from time 4 to 7. Which conclusion follows from this timeline?
📖 Explanation: P1 occupies the critical section during the interval 1 to 5, while P2 occupies it during 4 to 7. Their simultaneous occupancy from time 4 to 5 violates mutual exclusion.
Q4. A banking service updates an account balance and a transaction counter as one logical operation. Which code region should be treated as the critical section?
📖 Explanation: A critical section contains the operations that access shared state requiring coordinated protection. Because the two updates maintain one account invariant, both belong inside the protected region; unrelated calculations need not be included.
Q5. Process P is executing a protected critical section. What does mutual exclusion require of a competing process Q?
📖 Explanation: Mutual exclusion permits at most one process at a time to execute the protected critical section for the same shared resource. Priority, instruction count, or the number of accesses does not remove that restriction.
Q6. A team uses a monitor to coordinate access to a shared buffer. Which feature is typically provided by a monitor construct?
📖 Explanation: A monitor groups shared data and operations behind a construct that permits one process or thread at a time to execute its procedures. Condition variables support waiting for and signaling required states.
Q7. A critical section is empty, and processes P and Q both request entry. Which requirement says that an uninterested process outside the critical section must not delay the selection indefinitely?
📖 Explanation: Progress requires an entry decision to proceed when the critical section is free and contenders exist. Processes that are not requesting entry must not indefinitely control or postpone the selection of the next entrant.
Q8. A protocol promises that no more than two other successful entries may occur after a process requests access and before it enters. Which observation violates this guarantee?
📖 Explanation: Bounded waiting places a limit on how many competing entries may pass a requester. Three intervening entries exceed the stated maximum of two, whereas waiting for the current owner alone does not violate it.
Q9. Which situation is a race condition rather than merely concurrent execution?
📖 Explanation: Concurrent execution alone is not an error. A race condition occurs when operations access shared state without sufficient coordination and the observed result depends on their relative timing or interleaving.
Q10. A thread acquires a mutex before updating a shared queue and releases it after the update. What is the mutex intended to ensure?
📖 Explanation: A mutex provides exclusive ownership of a protected region while it is held. It does not by itself prevent preemption outside that region, promise FIFO fairness, or impose a particular queue length.
Q11. A counting semaphore represents four identical available devices and is initially 4. Four workers each acquire one permit successfully. What is its value afterward?
📖 Explanation: Each successful wait consumes one available permit. The count changes from 4 to 3, then 2, then 1, and finally 0. A fifth worker must wait until a permit is released.
Q12. Why is an atomic hardware instruction useful when implementing a lock?
📖 Explanation: Lock acquisition must not allow two contenders to observe an available state and both claim ownership. An atomic instruction makes the relevant test and state change indivisible, supporting mutual exclusion.
Q13. A student implements a lock by reading a shared Boolean flag and then setting it to true in a separate instruction. What is the flaw in this reasoning?
📖 Explanation: The flawed step is treating the separate read and write as one atomic acquisition. Both contenders can read false before either writes true. The corrected design uses an atomic operation or a proven locking protocol.
Q14. Two threads read counter value 10 and both attempt an atomic compare-and-swap from 10 to 11. What happens when both operations execute correctly?
📖 Explanation: The first successful compare-and-swap changes 10 to 11 atomically. The second operation then finds that the current value is not its expected value of 10, so it fails and must retry using the updated value.
Q15. Several processes repeatedly use an atomic test-and-set instruction to acquire one lock. What does atomicity guarantee by itself?
📖 Explanation: Atomic test-and-set serializes competing lock-state updates and can enforce mutual exclusion. It does not record arrival order or bound the number of failed attempts, so a process may repeatedly lose and starve.
Q16. In a strict-alternation protocol, P may enter only when turn equals P. P wants to enter, but turn belongs to Q, which remains in its remainder section. Which property can fail?
📖 Explanation: Strict alternation can prevent simultaneous entry, yet still block a process when the other process does not want the critical section. Progress is violated because an uninterested process controls admission despite the section being free.
Q17. A producer writes a payload and then sets a ready flag; a consumer observes the flag and reads the payload. What must the synchronization protocol guarantee?
📖 Explanation: The flag alone is insufficient unless the memory model orders publication correctly. A release operation by the producer and a matching acquire operation by the consumer can ensure earlier payload writes become visible before the read.
Q18. A developer claims that declaring a shared variable volatile makes an unsynchronized increment safe. Which correction is accurate?
📖 Explanation: The incorrect assumption is that visibility implies atomicity. A volatile declaration does not generally combine reading, adding, and writing into one indivisible operation. Use an atomic operation or protect the complete update with synchronization.
Q19. Thread A acquires lock L1 and then requests L2. Thread B acquires L2 and then requests L1. Which policy most directly prevents this particular circular wait?
📖 Explanation: The conflicting acquisition orders let each thread hold one lock while waiting for the other. A consistent global order prevents this cycle by ensuring every thread requests the same lock sequence.
Q20. A payment operation changes both an account balance and its transaction count. A programmer locks only the balance update. What can still go wrong?
📖 Explanation: A lock protects only operations included in its protocol. Because balance and transaction_count describe one logical state change, protecting one while leaving the other exposed can violate the invariant linking them.
Q21. A semaphore is initialized to 0. Process P executes wait, and process Q later executes signal. Under blocking semaphore semantics, what occurs?
📖 Explanation: A wait on a zero-count semaphore cannot consume a permit, so P blocks. Q then signals the semaphore, making a permit available or waking a waiting process according to the semaphore implementation.
Q22. A buffer has four slots and is initially empty. Which initial semaphore values correctly represent available slots and stored items?
📖 Explanation: An empty four-slot buffer has four available positions and contains no items. Therefore, empty starts at 4 and full starts at 0, preventing insertion beyond capacity and removal before an item exists.
Q23. A consumer waits on a condition variable until a shared queue contains an item. Why should it normally recheck the queue condition in a loop after waking?
📖 Explanation: A notification does not reserve an item or prove the predicate remains true. Spurious wake-ups and competing consumers are possible, so the thread must reacquire the mutex and test the queue condition again.
Q24. A service measures completed operations per second as worker count rises: (1,10), (2,19), (4,30), (8,32), and (16,21). Which interpretation best fits these results?
📖 Explanation: Throughput rises from 10 to 32 operations per second as workers increase to 8, then falls to 21 at 16. The decline suggests contention, resource saturation, or scheduling overhead rather than guaranteed linear scaling.
Q25. A queue producer holds the mutex, signals the consumer, and only afterward inserts the item. The consumer wakes, finds the queue empty, and waits again. What is the best correction?
📖 Explanation: The flawed order announces a condition change before the shared predicate becomes true. Insert the item while holding the associated mutex, then notify; the consumer must still recheck the queue predicate after waking.
Q26. On a single-core machine, a lock holder is descheduled while another process continuously spins on the same lock. Why can this be especially inefficient?
📖 Explanation: The waiter repeatedly consumes the only processor while the owner is not running. This can delay the owner from releasing the lock, so a blocking wait may be preferable when the owner is descheduled.
Q27. A driver holds a spinlock while waiting several milliseconds for disk I/O to finish. What is the strongest design objection?
📖 Explanation: Spinning is suitable mainly when the owner is expected to release a lock quickly. Holding it across slow I/O can waste substantial processor time and may be invalid where sleeping is required.
Q28. A system completes work continuously, but one process repeatedly loses a lock race while others acquire and release the lock. Which conclusion is correct?
📖 Explanation: Deadlock prevents a set of processes from progressing, whereas starvation can affect one process while others continue. Repeatedly bypassing the same waiter shows why system-wide progress does not guarantee individual fairness.
Q29. A running thread repeatedly releases and reacquires an unfair mutex before a waiting thread is scheduled. What risk does this pattern create?
📖 Explanation: An unfair acquisition policy may let the running thread win each reacquisition race. The waiter can therefore remain excluded despite the lock becoming free repeatedly, violating bounded-waiting expectations for that particular request.
Q30. A shared counter must be incremented, and an event must be triggered exactly once when the new value first reaches 100. Which approach preserves the requirement?
📖 Explanation: The increment and threshold decision form one logical operation. Protecting only the increment leaves a race in which multiple threads may trigger the event. One atomic transition or a lock covering both steps enforces exactly-once behavior.
Q31. A lock-free counter uses compare-and-swap loops. It performs well with two threads but slows sharply with 32 threads updating the same memory location. What is the most likely explanation?
📖 Explanation: Each successful compare-and-swap changes the value expected by competing threads, causing their attempts to fail. Under heavy contention, repeated retries and cache-coherence traffic can outweigh the benefit of avoiding blocking.
Q32. A timeline shows P1 holding a lock on [0,3), P2 waiting from 1 to 3, and P2 holding the lock on [3,6). Which property is demonstrated by this particular trace?
📖 Explanation: P1 leaves at time 3, exactly when P2 begins, so the displayed critical-section intervals do not overlap. This trace is consistent with mutual exclusion, although one trace cannot prove universal correctness.
Q33. A programmer protects a linked-list traversal with a mutex, but another thread removes and frees a node without acquiring that mutex. What is the most serious risk?
📖 Explanation: The lock protects the list only when every relevant access follows the same protocol. Removal without the lock can free a node that a reader still references, causing use-after-free errors, crashes, or corrupted data.
Q34. A developer protects every operation with one global mutex. Independent counters then update one at a time, reducing throughput as worker count grows. Which redesign is most appropriate?
📖 Explanation: One global mutex serializes operations that do not conflict. Separate locks allow independent counters to be updated concurrently while preserving their individual invariants; operations acquiring multiple locks still need a consistent ordering policy.
Q35. A benchmark records CPU time spent spinning for critical-section durations: (1 microsecond, 2%), (5, 8%), (20, 31%), (50, 68%), and (100, 91%). What trend does the data show?
📖 Explanation: The measured spinning share rises from 2 percent at one microsecond to 91 percent at 100 microseconds. Longer lock hold times make busy-waiting increasingly costly, supporting blocking or a shorter critical section.
Q36. A database validates that an account has at least 400 units, then two concurrent requests each withdraw 400 from a balance of 500. Both validation steps occur before either update. What correction prevents an invalid combined withdrawal?
📖 Explanation: Both requests can read 500 and independently pass validation, even though their combined withdrawals exceed the balance. Isolation or an atomic conditional update makes the second request observe the first committed withdrawal before proceeding.