Skip to content
GATEiit

GATE CSE · Operating System

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.

Before you start

This topic comes before: Deadlock, Transactions and concurrency control

Concept notes

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:

  1. Mutual exclusion: at most one process in its CS at a time.
  2. 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.
  3. 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.

2. Software and hardware solutions

For two processes P0,P1 (here i is me, j the other):

Solution Entry code ME Progress Bounded waiting
Strict alternation while (turn != i); Yes No (must take turns) Yes
Flags only flag[i] = 1; while (flag[j]); Yes No (both can wait for ever) Yes
Peterson flag[i] = 1; turn = j; while (flag[j] && turn == j); Yes Yes Yes

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 S 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 S, wake one.

In this blocking form, a negative value −k means k processes are waiting. (In the busy-wait textbook form, wait spins while S≤0 and S 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 n allows up to n processes in at once, for example n identical resources.
  • Initialise to 0 to make one process wait for another (ordering): P1 does signal(S) after statement A; P2 does wait(S) before statement B; so A runs before B.

Counting operations: starting from S0, after p waits and v signals (none blocked at the end), S=S0−p+v.

Order of operations matters. Two semaphores taken in opposite orders by two processes can deadlock:

P1: wait(A); wait(B); and P2: 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 N slots uses mutex = 1, empty = N, full = 0:

  • Producer: wait(empty); wait(mutex); add item signal(mutex); signal(full);
  • Consumer: wait(full); wait(mutex); remove item signal(mutex); signal(empty);

If a producer does wait(mutex) before wait(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.

ProducerConsumeritemitembuffer of N slotsempty = N, full = 0, mutex = 1 at the startproducer waits on empty, consumer waits on full
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
S=S0−(#wait)+(#signal)
Waiting processes (blocking semaphore)
S<0⇒∣S∣ processes blocked on S
Peterson entry for process i
flag[i]=1; turn=j; while(flag[j]∧turn=j);

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 P1 runs x = x + 1 and P2 runs x = x * 2, each as read x into a register, compute, write x. List every possible final value of x.

  1. 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 S 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 S after the five waits, and after the signal? How many processes are blocked at each point?

  1. Step 1: Apply the five waits.

    Try it on paper first.

Example 3: Checking strict alternation

conceptual

Two processes share turn, initially 0. Pi runs while (turn != i); CS turn = 1 - i; remainder. Which of the three critical-section requirements hold?

  1. 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?

  1. 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.

Practise these 10 PYQs →

Topic test

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.