Concurrency & Synchronization MCQs for NSCT
Concurrency and synchronisation is about keeping shared data correct when several processes or threads run at the same time. Without it you get race conditions, and the standard tools against them are mutex locks, semaphores and monitors. It is topic 6 of the Operating Systems area, which carries 5% of the NSCT according to HEC's Areas of Competencies. Operating Systems had the lowest accuracy of any area in 2026, at 33.8%, per the HEC NSCT report and APP, so careful tracing pays off here.
We follow Silberschatz's Operating System Concepts. Threads themselves are covered in Thread Management.
Race conditions
A race condition occurs when several threads access and change shared data concurrently and the result depends on the order of access. The cause is that a statement such as count++ is really three machine steps: load, add, store.
Example: count = 7. T1 runs count++ and T2 runs count--.
| Step | T1 | T2 | count |
|---|---|---|---|
| 1 | load 7, add 1 (register = 8) | 7 | |
| 2 | load 7, subtract 1 (register = 6) | 7 | |
| 3 | store 8 | 8 | |
| 4 | store 6 | 6 |
The correct answer is 7, but an interleaving can also give 6 or 8.
The fix is a critical section, the code that touches shared data, protected by an entry and exit protocol. A correct solution must provide mutual exclusion (one process inside at a time), progress (if no one is inside, the choice of who enters next cannot be postponed forever) and bounded waiting (a limit on how many times others can overtake a waiting process).
Exam angle: to find the possible final values, list the interleavings of loads and stores; a "lost update" happens when a stale register value is stored last.
Hardware and software foundations
Peterson's solution solves the problem for two processes with flag[2] and turn, but modern CPUs may reorder memory operations, so it is not guaranteed to work on them without memory barriers. Hardware offers atomic instructions such as test_and_set() (return the old value and set to true) and compare_and_swap() (set to the new value only if the current value equals the expected one; always return the old value).
Mutex
A mutex lock has two operations, acquire() and release(), and a boolean "available". A thread must acquire it before the critical section and release it afterwards. If the lock spins while waiting it is a spinlock: it wastes CPU but avoids context switches, so it suits multicore systems with very short critical sections.
acquire(lock);
balance = balance + deposit; /* critical section */
release(lock);
Exam angle: a mutex usually has an owner, so only the thread that acquired it should release it. A semaphore has no owner.
Semaphores
A semaphore S is an integer accessed only through two atomic operations: wait() (P), which decrements it, and signal() (V), which increments it. A binary semaphore ranges over 0 and 1 and behaves much like a mutex; a counting semaphore is initialised to the number of available resources.
Where books differ: the classical busy-waiting definition never lets S drop below 0. Silberschatz's blocking implementation lets S go negative; its magnitude is then the number of waiting processes.
Semaphores also enforce order. To make S2 in P2 run after S1 in P1, set synch = 0; P1 runs S1; signal(synch); and P2 runs wait(synch); S2;.
Exam angle: misuse is a favourite question. signal before the critical section breaks mutual exclusion, and two processes taking two semaphores in opposite order can deadlock.
| Tool | Value | Owner | Typical use |
|---|---|---|---|
| Mutex | Locked or unlocked | Yes | Mutual exclusion |
| Binary semaphore | 0 or 1 | No | Mutual exclusion or signalling |
| Counting semaphore | Any integer | No | Pools of N identical resources |
| Monitor | Built-in lock plus condition variables | Implicit | Structured mutual exclusion |
Monitors
A monitor is a high-level construct that packages shared data with the procedures that use it, and guarantees that only one process is active inside at a time. Condition variables let a process wait inside the monitor: x.wait() suspends it, and x.signal() resumes one waiting process. If no process is waiting, x.signal() has no effect, unlike a semaphore's signal().
When P signals Q, one of them must wait. Under signal-and-wait, P waits and Q runs. Under signal-and-continue, P continues and Q runs later, so Q must recheck its condition in a while loop.
Exam angle: the producer-consumer, readers-writers and dining-philosophers problems use these tools; they are covered in depth in Semaphores and Classic Sync Problems.
Common traps
- Assuming
count++is atomic. It is load, add, store. - Initialising a mutual-exclusion semaphore to 0 (no one can enter) or to 2 (two can enter).
- In producer-consumer, calling
wait(mutex)beforewait(empty), which deadlocks when the buffer is full. - Treating a condition variable like a semaphore: a signal with no waiter is lost.
- Forgetting that priority inversion is fixed by priority inheritance, not by raising the medium task's priority.
- Calling strict alternation with one
turnvariable correct: it breaks progress.
Worked examples
1. Understanding. What is busy waiting, and how does a semaphore avoid it?
Answer: busy waiting means a waiting process loops, testing the lock and wasting CPU cycles. A blocking semaphore puts the process on the semaphore's waiting queue and suspends it; signal() later moves it back to the ready queue.
2. Application. S = 2. Processes run, in order: A wait, B wait, C wait, A signal, D wait. What is S after each step, and who is blocked at the end? Answer: S goes 1, 0, -1 (C blocks), 0 (A's signal wakes C), -1 (D blocks). At the end S = -1 and only D is blocked; B and C hold the resource.
3. Analysis. x = 0. T1 runs x = x + 2 and T2 runs x = x * 3, each as load, compute, store. What final values are possible? Answer: 0, 2 or 6. Serial orders give (0 + 2) x 3 = 6 or 0 x 3 + 2 = 2. If both load 0 and T1 stores last, x = 2; if T2 stores last, x = 0. An exhaustive interleaving search confirms exactly {0, 2, 6}.
Practise
The quiz below has 100 Concurrency & Synchronization questions, filterable by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between a mutex and a semaphore?
A mutex is a lock with an owner: the thread that acquires it releases it, and it is used for mutual exclusion. A semaphore is an integer counter with wait() and signal() and no owner, so one thread can signal another. A counting semaphore can also manage several identical resources.
What are the three requirements of the critical-section problem?
Mutual exclusion: only one process runs in its critical section at a time. Progress: if no process is inside, the processes that want to enter decide who goes next without indefinite delay. Bounded waiting: once a process asks to enter, others can enter only a limited number of times before it does.
Can a semaphore have a negative value?
It depends on the definition. In the classical busy-waiting version, wait() loops until S is positive, so S never goes below 0. In Silberschatz's blocking implementation, wait() decrements first, so S can be negative; then its magnitude equals the number of processes waiting on it.
What is priority inversion?
A high-priority task waits for a lock held by a low-priority task, while a medium-priority task preempts the low-priority one, so the high-priority task is effectively delayed by a lower one. Priority inheritance fixes it by temporarily giving the lock holder the waiter's priority.
Next step
Continue to Deadlocks, drill semaphores and classic problems, return to the Operating Systems MCQ hub, or take a timed NSCT mock test.