Project ForgeSign up free
Operating Systems
Process Synchronization
GATE 2020
Expert

Each of a set of n processes executes the following code using two semaphores a and b initialized to 1 and 0, respectively. Assume that count is a shared variable initialized to 0 and not used in CODE SECTION P.

CODE SECTION P
wait(a); count=count+1;
if (count==n) signal(b);
signal(a); wait(b); signal(b);
CODE SECTION Q

What does the code achieve?

  • It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.
  • BIt ensures that at most two processes are in CODE SECTION Q at any time.
  • CIt ensures that all processes execute CODE SECTION P mutually exclusively.
  • DIt ensures that at most n−1 processes are in CODE SECTION P at any time.

Explanation

This is the classic semaphore barrier pattern. Semaphore a mutually excludes access to 'count' while incrementing it (mutual exclusion during the counting step, not throughout all of Section P). The LAST process to arrive (whose increment makes count==n) signals b once. Every process then does wait(b) — the first n−1 processes block here until that final signal arrives, and each process that passes wait(b) immediately does signal(b) again, 'passing along' exactly one permit so the next waiting process can also proceed (a classic cascading-release barrier). The net effect: NO process can enter CODE SECTION Q until every one of the n processes has incremented count, i.e. until all have reached (finished) CODE SECTION P — a barrier synchronization.

Source: GATE CS 2020 (2020)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free