Skip to content
GATEiit

GATE CSE 2020 Q44: What a semaphore barrier between code sections P and Q achieves

Question

Each of a set of nn 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?

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: A (official GATE 2020 final answer key)

Concept used: A barrier: a counter protected by a mutex, plus a turnstile semaphore opened when the last process arrives.

First step: b starts at 0, so every process blocks on wait(b) until count reaches n; then each passing process re-signals b for the next.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2020 CS question paper and final answer key, organised by IIT Delhi. Question paper · Answer key