Inter-process communication, concurrency and synchronization
Shared memory and message passing, race conditions and the critical-section problem, Peterson's solution and hardware locks, semaphores, and the classic producer–consumer, readers–writers and dining philosophers problems as GATE tests them.
1. IPC, race conditions and the critical-section problem
Cooperating processes communicate by inter-process communication (IPC):
Shared memory: a region mapped into both address spaces. Fast, but the processes must synchronize themselves.
Message passing: send and receive through the kernel (pipes, message queues, sockets). Easier to get right, slower. Sends and receives can be blocking (synchronous) or non-blocking.
A race condition occurs when the result depends on the order in which processes access shared data. x = x + 1 is really load, add, store; if two processes interleave these steps, one update can be lost.
The code that touches shared data is the critical section (CS). A correct solution must give:
Mutual exclusion: at most one process in its CS at a time.
Progress: if no process is in its CS, the choice of who enters next is made only by processes trying to enter, and it cannot be postponed for ever.
Bounded waiting: after a process asks to enter, there is a limit on how many times others can enter before it.
Mutual exclusion is about safety; progress and bounded waiting rule out deadlock and starvation. A solution must also not depend on CPU speed or the number of CPUs.
In every case the exit code clears what the entry code set (flag[i] = 0, or turn = j for alternation).
Hardware support:
Disabling interrupts: works only on a single CPU, and is unsafe in user mode.
Test-and-set (TSL) atomically returns the old value and sets the lock to 1: while (TestAndSet(&lock)); then CS, then lock = 0. Gives mutual exclusion and progress but not bounded waiting, and it busy-waits (spinlock).
Compare-and-swap works the same way.
Spinlocks waste CPU while waiting, but they avoid a context switch, so they suit short critical sections on multiprocessors.
3. Semaphores
A semaphore is an integer accessed only by two atomic operations:
wait(S) (also P or down): decrement; if the result is negative, the caller blocks.
signal(S) (also V or up): increment; if a process is blocked on , wake one.
In this blocking form, a negative value means processes are waiting. (In the busy-wait textbook form, wait spins while and never goes below 0.)
A binary semaphore (0 or 1) works as a lock. Initialise to 1 for mutual exclusion.
A counting semaphore initialised to allows up to processes in at once, for example identical resources.
Initialise to 0 to make one process wait for another (ordering): does signal(S) after statement A; does wait(S) before statement B; so A runs before B.
Counting operations: starting from , after waits and signals (none blocked at the end), .
Order of operations matters. Two semaphores taken in opposite orders by two processes can deadlock:
: wait(A); wait(B); and : wait(B); wait(A);
A monitor is a language construct in which only one process is active at a time; it uses condition variables with wait and signal for ordering. Unlike a semaphore, a condition-variable signal with no waiter is lost.
4. The classic synchronization problems
Bounded buffer (producer–consumer) with slots uses mutex = 1, empty = N, full = 0:
If a producer does wait(mutex)beforewait(empty) when the buffer is full, it blocks while holding the mutex; the consumer can never get in, so both deadlock. The order of the two signal calls does not matter.
Readers–writers: many readers may read together, but a writer needs exclusive access. The first-readers solution keeps readcount, protected by mutex, and a semaphore wrt; the first reader does wait(wrt) and the last reader does signal(wrt). Writers can starve in this version.
Dining philosophers: 5 philosophers, 5 forks. If each picks up the left fork and then the right, all can hold one fork and wait for ever (deadlock). Fixes: allow at most 4 at the table, make one philosopher pick right first (asymmetric), or pick up both forks only when both are free.
Bounded buffer: empty counts free slots, full counts filled slots, mutex guards the buffer
Formulas to remember
Semaphore after a sequence of operationsWhen no process is left blocked
Waiting processes (blocking semaphore)
Peterson entry for process i
Common mistakes
Treating x = x + 1 as atomic; it is a read, a compute and a write.
Taking the mutex before the counting semaphore in producer–consumer; this can deadlock.
Saying strict alternation fails mutual exclusion; it fails progress.
Assuming test-and-set gives bounded waiting.
Forgetting that in the blocking implementation a negative semaphore value counts the blocked processes.
Disabling interrupts to protect a critical section on a multiprocessor; other CPUs still run.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Lost update: all possible final values
numerical
Shared x = 1. Process runs x = x + 1 and runs x = x * 2, each as read x into a register, compute, write x. List every possible final value of x.
Step 1: Run them one after the other in both orders.
Try it on paper first.
Example 2: Tracking a counting semaphore
numerical
A counting semaphore starts at 2 and uses the blocking implementation. Five processes each perform wait(S) one after another. Then one process holding the resource performs signal(S). What is after the five waits, and after the signal? How many processes are blocked at each point?
Step 1: Apply the five waits.
Try it on paper first.
Example 3: Checking strict alternation
conceptual
Two processes share turn, initially 0. runs while (turn != i); CS turn = 1 - i; remainder. Which of the three critical-section requirements hold?
Step 1: Can both be in the CS together?
Try it on paper first.
Example 4: Swapping waits in producer–consumer
conceptual
In the bounded-buffer solution, the producer is changed to wait(mutex); wait(empty); add item signal(mutex); signal(full);. The consumer is unchanged. What can go wrong?
Step 1: Let the buffer fill up, then run the producer.
Try it on paper first.
Previous year questions
10 official GATE CSE questions on this topic, newest first.
10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.