Process Synchronisation
Why This Chapter Matters
Synchronisation is one of the most-tested OS topics in GATE — 6-10 marks. Critical section problem, semaphores, deadlock conditions (Coffman), and Banker's algorithm are tested frequently.
Core Concepts
1. Critical Section Problem
Critical section: Code accessing shared resources.
Requirements for correct solution:
2. Peterson's Solution (2 processes)
flag[i] = true (process i wants to enter)
turn = j (give turn to other process)
Entry: flag[i]=true; turn=j; while(flag[j] && turn==j) wait;
Exit: flag[i] = false
Satisfies all 3 requirements. Works for 2 processes.
3. Semaphores
Binary semaphore (mutex): 0 or 1. Simple lock/unlock.
Counting semaphore: Non-negative integer. Controls access to k resources.
wait(S): while S<=0 do nothing; S--
signal(S): S++
Producer-Consumer with semaphores:
mutex = 1 (mutual exclusion)
full = 0 (count of full slots)
empty = N (count of empty slots)
Producer: wait(empty); wait(mutex); add item; signal(mutex); signal(full)
Consumer: wait(full); wait(mutex); remove item; signal(mutex); signal(empty)
4. Deadlock
Four Necessary Conditions (Coffman):
Deadlock Prevention: Ensure at least one Coffman condition never holds.
Deadlock Avoidance: Banker's Algorithm.
Deadlock Detection: Allow deadlock, detect and recover.
5. Banker's Algorithm
State is SAFE if a sequence exists such that each process can complete.
For each process: check if its maximum remaining need can be satisfied by current available resources. If yes, allocate, let it finish, release all resources. Repeat.
PYQs
GATE 2024: How many binary semaphores are needed to solve n-process critical section problem?
1 binary semaphore (mutex=1) is sufficient for mutual exclusion.
GATE 2023: Which condition is NOT required for deadlock?
Deadlock requires ALL FOUR Coffman conditions. Removing any one prevents deadlock.
GATE 2022: System has 3 resources, 4 processes. Max need, current allocation, available resources given. Is it in safe state?
Apply Banker's algorithm — check if safe sequence exists.

