# GATE - 2020 | OS | Each of a set of n processes executes the following code using two semaphores ## Question:

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 ?
(A) It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P
(B) It ensures that two processes are in CODE SECTION Q at any time
(C) It ensures that all processes execute CODE SECTION P mutually exclusively
(D) It ensures that at most n−1 processes are in CODE SECTION P at any time

## Solution:

### Option A is Correct

The wait(a) ensures that count value is correctly incremented (no race condition)

if(count==n) signal (b);

This signal(b) statement is executed by the last (nth) process only.

Rest of the n-1 processes are blocked on wait(b).

Once the nth process makes signal(b) then rest of the processes can proceed an enter Code section Q.

-----------------------------------------------------

All the processes running the given code will remain block due to wait(b) until the value of count becomes n.

When the value of count will become equals to n, value of b changes to 1 which will subsequently allow a process to go into section Q.

Hence, no process can go to section Q until all the process executes code section P.

Thank You