The GATE Grind

GATE 2020 CS – Question 44

Operating System · Concurrency and Synchronization · 2 marks · Multiple choice

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?

  1. It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.
  2. It ensures that at most two processes are in CODE SECTION Q at any time.
  3. It ensures that all processes execute CODE SECTION P mutually exclusively.
  4. It ensures that at most $n-1$ processes are in CODE SECTION P at any time.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

Explanation

This is a barrier. The last process to arrive (count==n) signals b, and each process then does wait(b) followed by signal(b), so all are released only after all have finished section P.