📝 Process synchronization in Windows (36 MCQs)
Windows Kernel Objects for Thread Synchronization
Windows provides synchronization facilities through kernel dispatcher objects, user-mode synchronization primitives, and interlocked processor operations. These mechanisms coordinate threads accessing shared resources and support event notification, mutual exclusion, and resource counting. Common facilities include mutexes, semaphores, events, critical sections, and slim reader-writer locks, allowing applications and system components to manage concurrent execution.
Why Windows Needs Multiple Synchronization Mechanisms
Different applications require different synchronization behaviour. A short shared-memory operation benefits from a lightweight lock, while a thread waiting for a device event may require a blocking notification mechanism. Providing several synchronization primitives allows Windows software to balance kernel-transition costs, fairness, ownership requirements, and the duration of protected operations.
Critical Sections, Mutexes, Semaphores, and Events
A Windows critical section provides mutual exclusion between threads within one process and commonly uses efficient user-mode operations before blocking when necessary. A mutex can coordinate access across processes, while semaphores represent multiple permits. Event objects notify waiting threads, and interlocked functions perform supported atomic updates to shared memory.
Coordinating Two Threads with a Windows Mutex
Suppose threads T1 and T2 in separate processes need access to a shared system resource. Both open the same named mutex and attempt to acquire it. T1 obtains ownership and performs its operation. T2 waits until T1 releases the mutex, after which T2 can acquire the resource and continue.
Blocking Efficiency, Ownership, and Kernel Overhead
Windows synchronization mechanisms support concurrent applications, device services, and system components. User-mode critical sections reduce unnecessary kernel transitions for uncontended locks, while dispatcher objects support blocking and notification. However, excessive contention increases waiting and context-switch overhead. Incorrect lock ordering can cause deadlocks, and abandoned mutex ownership requires careful recovery handling.
Misconceptions About Windows Synchronization Objects
'A Windows critical section can synchronize unrelated processes directly' is incorrect because it is process-local. 'An event object automatically protects shared data' is incorrect because it provides notification rather than mutual exclusion. 'Every mutex acquisition is a kernel-mode operation' is incorrect because implementation paths may optimise uncontended operations.
Exam Focus: Matching Windows Primitives to Synchronization Needs
Compare critical sections, mutexes, semaphores, events, and interlocked operations by their purpose and scope. Identify whether a primitive provides mutual exclusion, counting, notification, or atomic updating. Questions may distinguish process-local critical sections from cross-process mutexes, examine blocking behaviour, and evaluate suitable mechanisms for short and long critical sections.
Key Terms of Windows Synchronization
- Critical section - Process-local mutual-exclusion mechanism.
- Mutex - Ownership-based exclusive lock.
- Semaphore - Counts available resource permits.
- Event object - Notifies waiting execution activities.
- Interlocked operation - Atomic shared-memory operation.
- Dispatcher object - Kernel object supporting synchronization.
📝 All Process synchronization in Windows MCQs
Q1. Three threads in one Windows process update the same in-memory account record. Which primitive is designed to provide exclusive access among those threads with low overhead when uncontended?
📖 Explanation: A critical section provides mutual exclusion among threads in the same process. Its uncontended path is lightweight, while contention can make a thread wait, so it suits short in-process updates better than a cross-process kernel mutex.
Q2. Two independent Windows processes map the same shared-memory region and must prevent simultaneous updates. Which choice directly supports mutual exclusion across their process boundary?
📖 Explanation: Separate critical-section objects have independent process-local state and cannot coordinate the two processes. A named mutex lets both processes refer to the same synchronization object and take exclusive ownership of it.
Q3. A Windows service manages six identical database connections. At most six worker threads may use a connection simultaneously, and each completed operation returns one available connection. Which primitive best represents this limit?
📖 Explanation: A counting semaphore represents a finite number of available permits. Initializing it to six allows up to six successful waits before the count reaches zero; releasing a permit makes capacity available again.
Q4. During application shutdown, every worker waiting for a stop notification should be released, and later waiters should also observe the notification until it is cleared. Which event behavior fits?
📖 Explanation: A manual-reset event remains signaled until the program resets it, allowing multiple current and later waiters to proceed. An auto-reset event normally releases one waiter and then clears its signaled state.
Q5. A developer signals a Windows event before modifying a shared structure and assumes the event itself prevents two workers from changing that structure simultaneously. Which statement is correct?
📖 Explanation: An event communicates that a state or occurrence should be noticed; it does not grant exclusive ownership of ordinary data. Shared updates still need a lock or suitable atomic protocol to prevent races.
Q6. A monitoring thread calls WaitForSingleObject on a process handle representing a helper process. What condition normally makes that process object become signaled?
📖 Explanation: A process handle is waitable. Its object becomes signaled when the represented process terminates, allowing the monitoring thread to block efficiently until completion rather than repeatedly polling process state without busy waiting.
Q7. Thread T owns a Windows mutex and calls a wait function on that same mutex again. What should it normally expect?
📖 Explanation: Windows mutex ownership is recursive for its owning thread. A repeated successful wait increases the ownership recursion count, so the thread must call ReleaseMutex the corresponding number of times before another thread can acquire it.
Q8. WaitForSingleObject returns WAIT_ABANDONED for a mutex. What does the waiting thread know?
📖 Explanation: WAIT_ABANDONED reports that the previous owner terminated while owning the mutex. The waiting thread acquires ownership, but it must inspect and recover the protected resource because an update may have stopped midway.
Q9. A thread enters the same Windows critical section twice while executing nested helper functions, then leaves it twice. Which behavior is expected?
📖 Explanation: Windows critical sections are recursive for the owning thread. Nested code can enter the same object again, but each successful entry must be matched by a leave before other threads can enter.
Q10. A multiprocessor application sets a spin count for a critical section that is usually held for only a few instructions. What is the intended effect when another thread owns it?
📖 Explanation: Spinning lets a contending thread poll briefly for the lock to become free before entering a blocking wait. It can help when hold times are very short, but it consumes processor time and gives no FIFO guarantee.
Q11. Two Windows threads acquire two mutexes in opposite orders. Thread A holds M1 and waits for M2, while Thread B holds M2 and waits for M1. Which design change directly removes this circular wait pattern?
📖 Explanation: A consistent global lock order prevents the two threads from forming the shown cycle. If every path acquires M1 before M2, a thread cannot hold M2 while waiting for M1 in this protocol.
Q12. Two processes create or open the same named mutex, but one receives an access-denied error. What should the developer examine first?
📖 Explanation: Named synchronization objects are subject to Windows access control. Matching the object name does not bypass its security descriptor; the process token and requested access rights must permit the operation.
Q13. A Windows semaphore currently has a count of three. A thread successfully waits on it, and no other operation occurs. What is the new count?
📖 Explanation: A successful wait on a semaphore consumes one permit. Starting from three, the count becomes two; the object becomes nonsignaled only when its count reaches zero for subsequent waiting threads.
Q14. A semaphore has current count five and maximum count five. ReleaseSemaphore is called to add one permit and reports failure. What is the correct interpretation?
📖 Explanation: A semaphore cannot exceed its configured maximum count. When a release would raise the count beyond that limit, the call fails; code must check the result rather than assume a permit was added.
Q15. A producer calls SetEvent repeatedly on an auto-reset event while the event is already signaled and no worker is waiting. Why can these calls fail to represent every queued job?
📖 Explanation: An auto-reset event stores a state rather than a permit count. Repeated signals can coalesce while it is already signaled, so a producer that must account for every item should use a queue with a counting protocol.
Q16. A manual-reset event is signaled while four threads are waiting. What happens to the event state after those waiters are released?
📖 Explanation: A manual-reset event stays signaled after releasing waiting threads. Additional threads can also pass while it remains signaled; the application must call ResetEvent when the condition should no longer be treated as available.
Q17. A work queue may contain several jobs, but a producer uses one auto-reset event to wake consumers. A consumer wakes, removes one job, and returns to waiting while jobs remain. What design weakness is exposed?
📖 Explanation: An auto-reset event can coalesce notifications and does not track queue length. The design should drain the queue while work remains, reliably signal again, or use a semaphore whose permits correspond to available jobs.
Q18. A service limits simultaneous access to four identical devices using a semaphore initialized to four. Four threads each complete a successful wait and keep their permits. What happens when a fifth thread waits?
📖 Explanation: Each successful wait consumes one of the four permits. Once all are held, the semaphore count is zero and another waiter blocks until a thread releases a permit after finishing its device operation.
Q19. A producer waits on a free-slot semaphore for a bounded queue, reserves one slot, then detects an error before inserting anything. Which recovery preserves correct accounting?
📖 Explanation: The free-slot wait reserved capacity, but the failed insertion did not create a consumable item. Recovery should return the reservation to the free-slot count and must not increase the available-item count.
Q20. A manual-reset event releases two worker threads at once. Both then modify the same shared list without acquiring a lock. What is the main defect in this design?
📖 Explanation: A manual-reset event may release multiple waiters, but it does not serialize their subsequent work. The list still needs a critical section, mutex, or other compatible protection around conflicting modifications.
Q21. Two Windows threads increment the same aligned counter with InterlockedIncrement. What property does this operation provide for the increment itself?
📖 Explanation: InterlockedIncrement performs the increment as one atomic operation on the target variable. It prevents competing increments from overwriting one another, but it does not automatically protect unrelated data or a larger multi-step invariant.
Q22. A program atomically increments a shared item count and then separately inserts the item into a linked list. A consumer can observe the new count before the insertion completes. What was incorrectly assumed?
📖 Explanation: The interlocked increment protects only the counter update. The related list change and count must be coordinated as one invariant, for example under a lock or a carefully designed lock-free protocol.
Q23. A Windows program marks a shared flag volatile and assumes the producer's several writes to a structure become one indivisible transaction when the consumer sees that flag. Why is this assumption unsafe?
📖 Explanation: Volatile does not turn several writes into one indivisible transaction or protect the structure invariant. Use a lock or carefully designed atomic publication protocol so the consumer observes one consistent state.
Q24. A compare-and-swap loop updates a shared pointer. The pointer changes from address A to B and back to A before the original thread retries. Why can the comparison miss this history?
📖 Explanation: This is the ABA problem: the value appears unchanged even though it changed and returned to its earlier representation. Tagged versions, hazard pointers, or other safe reclamation techniques may be needed, depending on the design.
Q25. A thread has entered a critical section exactly once and calls SleepConditionVariableCS. What happens as it enters the condition-variable wait?
📖 Explanation: SleepConditionVariableCS atomically releases the supplied critical section while the thread enters the wait. When awakened or timed out, the function reacquires that critical section before returning to the caller under the associated lock.
Q26. A worker waiting on a Windows condition variable wakes, reacquires its lock, and discovers that another worker already consumed the available item. What should the first worker do?
📖 Explanation: Wakeups can be spurious or stolen by another thread that runs first. The predicate, not the wakeup alone, determines whether work is available, so the worker should recheck it while holding the associated lock.
Q27. A queue receives one item and calls WakeAllConditionVariable when twelve consumers are waiting. Most awakened consumers find the queue empty after one wins the item. What is the likely performance cost?
📖 Explanation: WakeAllConditionVariable releases all waiters, even though one item may satisfy only one consumer. The others contend for the lock, recheck the predicate, and sleep again, creating avoidable scheduling and synchronization overhead.
Q28. A team allocates a Windows condition-variable structure independently in two processes and expects a wake in one process to release a waiter in the other. Why is this design invalid?
📖 Explanation: Windows condition variables are process-local user-mode synchronization primitives. Separate structures in different processes do not form one shared wait channel; cross-process coordination needs an appropriate shared synchronization object, such as a named mutex.
Q29. A cache is read frequently and updated occasionally by threads in one Windows process. Readers may run together, but a writer must exclude all readers and other writers. Which primitive directly matches this access pattern?
📖 Explanation: An SRW lock supports shared acquisition by multiple readers and exclusive acquisition by a writer. It is process-local and nonrecursive, so code must follow its ownership rules and avoid assuming recursive acquisition.
Q30. A performance test reports these pairs of (worker threads, completed operations per second): (1,100), (2,192), (4,350), (8,515), (16,520). Between which two measurements does throughput gain nearly disappear?
📖 Explanation: Throughput rises from 515 to only 520 operations per second when workers increase from eight to sixteen. The near-flat final interval suggests contention or another shared bottleneck has largely removed scaling benefit.
Q31. A lock benchmark gives (critical-section hold time in milliseconds, mean waiter delay in milliseconds): (1,0.1), (2,0.3), (4,1.0), (8,3.7), (16,12.5). What delay is measured at an eight-millisecond hold time?
📖 Explanation: Read the pair whose first coordinate is eight: it is (8,3.7). Therefore the measured mean waiter delay is 3.7 milliseconds; the first coordinate is the hold time, not the delay.
Q32. A trace records (waiting consumers awakened by WakeAll, consumers that find no item): (1,0), (2,1), (4,3), (8,7), (16,15). If one item is available and all awakened consumers compete, how many of eight awakened consumers find no item?
📖 Explanation: One consumer removes the single available item, leaving seven awakened consumers with a false predicate. They must return to waiting, illustrating the unnecessary wakeups created when a broadcast is larger than the available work.
Q33. A producer uses an auto-reset event for a queue. The trace gives (time, queued items, event state): (0,0,nonsignaled), (1,1,signaled), (2,3,signaled), (3,2,nonsignaled), (4,2,nonsignaled). Which observation identifies the flaw?
📖 Explanation: The trace shows two queued items at time three while the auto-reset event is nonsignaled. A consumer can therefore remain asleep despite pending work, because one event state does not count every queued item.
Q34. WaitForMultipleObjects is called with bWaitAll set to FALSE and handles ordered as [event A, semaphore B, mutex C]. Event A is nonsignaled, while B and C are signaled. Which result is expected?
📖 Explanation: In wait-any mode, Windows checks handles in array order and returns the index of the first signaled object. Because B is the first signaled handle, its semaphore count is decremented as the wait succeeds.
Q35. A Windows UI thread owns windows and waits indefinitely with WaitForMultipleObjects while it must also process system messages. What is the safer design direction?
📖 Explanation: A thread that owns windows must continue processing messages. A message-aware wait can unblock for synchronization objects or queued input, avoiding deadlock risks caused by an infinite wait that prevents required message processing.
Q36. Two separate Windows processes share a bounded queue of five records. They need to prevent simultaneous queue updates, count free slots, and count available records. Which design assigns the correct role to each primitive?
📖 Explanation: The named mutex coordinates exclusive access across processes. Separate semaphores track free capacity and queued records: five and zero initially. Each successful insertion and removal must update the corresponding permit counts consistently.