Chapter 2

Concurrency and synchronisation

A concurrent trace is constrained by program order and synchronisation, not by how the source is arranged on the page. GATE questions ask whether an execution is possible, whether a property always holds, and which initial semaphore values enforce an ordering.

2.1 Atomic actions and interleavings

Past papers · CS 2014–2026 (23 papers): 4 questions, 6 marks

An action is atomic only at the granularity specified by the problem or guaranteed by the primitive. A source statement such as x = x + 1 may require a load, arithmetic and a store. Two threads can read the same old value and lose an update.

For output or shared-variable problems, list the atomic reads and writes first. Preserve each thread's order, add semaphore or join constraints, and explore only legal choices. A single counterexample disproves an “always” claim. To prove an extreme value, show both a bound and a trace that attains it.

2.2 Critical-section requirements and locks

Past papers · CS 2014–2026 (23 papers): 4 questions, 7 marks

Mutual exclusion permits at most one participant in the protected region. Progress requires that a decision among interested processes cannot be postponed by uninterested ones. Bounded waiting limits how often others can overtake a requester after it announces interest.

ConstructionWhat to check
Strict alternationCan violate progress when the other process does not want to enter
Two flags set before waitingBoth can wait forever
Test-and-set or compare-and-swap lockAtomic acquisition gives exclusion; a plain spinlock need not bound waiting
Interrupt disablingLocal protection on one CPU; does not stop other CPUs
MutexOwnership matters; unlocking is the owner's operation

Peterson's two-process algorithm uses flags want[i] and a shared turn. Process ii sets its flag, assigns turn = j, then waits while want[j] && turn == j; it clears its flag on exit. Here jj is the other process. Under atomic reads/writes and sequential consistency, it gives exclusion, progress and bounded waiting. Modern compiler/CPU reordering needs appropriate atomic operations and ordering guarantees.

A non-reentrant lock can deadlock one thread that tries to acquire a lock it already holds. No second thread or second lock is needed. In ticket-based entry protocols, equal tickets need a deterministic process-ID tie-break; otherwise two contenders can each wait for the other. The bakery algorithm compares pairs (ticket, process_id) lexicographically and waits for a contender to finish choosing its ticket.

2.3 Counting and binary semaphores

Past papers · CS 2014–2026 (23 papers): 6 questions, 11 marks

wait(S) acquires one permit, blocking if none is available. signal(S) releases a permit or wakes a waiter. Both must be atomic with respect to their bookkeeping; a thread must not miss a wake-up between queueing and sleeping.

To order event A before event B, initialise a semaphore to zero, signal after A, and wait before B. For repeated alternation between two threads, use one initial permit on the first thread's gate and zero on the second. Fair wake-up order must be specified; semaphore operations alone do not promise FIFO service.

A one-use barrier lets no participant pass until all have arrived. Protect an arrival counter with a mutex; the last arrival opens a zero-initialised gate. Passing the gate permit onward lets every participant leave:

// Initially: arrived = 0, mutex = 1, gate = 0; n participants.
wait(mutex)
arrived = arrived + 1
if arrived == n: signal(gate)
signal(mutex)
wait(gate)
signal(gate)
// Continue only after all n arrivals.

Do not reuse this barrier for another round without resetting its state safely. The gate remains open after the first round. A binary semaphore has at most one available permit: extra signals to an already open gate do not accumulate like a counting semaphore.

2.4 Bounded buffer and resource conservation

Past papers · CS 2014–2026 (23 papers): 2 questions, 4 marks

For a buffer with KbK_b slots, initialise empty = K_b, full = 0, mutex = 1. A producer waits on empty, waits on mutex, inserts, signals mutex, then signals full. A consumer waits on full, waits on mutex, removes, signals mutex, then signals empty.

2.5 Readers, writers and dining philosophers

Readers may share access while no writer is active; a writer needs exclusive access. In a reader-preference solution, protect the reader count with a mutex. The first reader acquires the shared resource lock and the last reader releases it. New readers can delay a writer indefinitely. Writer preference can instead starve readers; fairness needs an explicit admission policy.

In dining philosophers, taking one fork and waiting for the next can form a circular wait. A global ordering of fork acquisition prevents a cycle. Another approach allows at most one fewer diner than forks to compete, assuming the usual ring with one fork between neighbours. It prevents the all-hold-one deadlock but does not by itself prove starvation freedom.

2.6 Monitors and condition variables

A monitor serialises entry to protected operations. A condition wait atomically releases the monitor lock and sleeps; the thread reacquires the lock before proceeding. A condition signal is not a stored permit: signalling when no thread waits need not have a later effect.

Under Mesa-style semantics, the signaller continues and an awakened thread competes for the lock. Test the predicate in a while loop after waking. Under Hoare-style semantics, signalling transfers monitor execution immediately to the waiter; distinguish this when interpreting textbook pseudocode.

Last-minute recall

  • Define the atomic step before enumerating schedules.
  • Exclusion, progress and bounded waiting are separate requirements.
  • A busy-waiting thread consumes CPU while waiting.
  • Initialise a resource semaphore to capacity and an ordering semaphore to zero.
  • A pair of waits can deadlock even if one atomic pair-acquisition would not.
  • Condition variables do not accumulate notifications like semaphore permits.
  • Deadlock freedom alone does not guarantee fairness.

Revise offline: the whole Operating System handbook as one PDF

50 pages · 2.0 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections