IAE Academy

GATE CS

Inter-process communication, concurrency and synchronisation

Concurrent processes that share data can interleave their instructions in many ways, so correctness needs a controlled way to enter critical sections. This topic covers the critical-section requirements, software and hardware solutions, semaphores, mutexes and monitors, the classic synchronisation problems, and the two IPC models. GATE questions ask you to judge whether a protocol is correct, to find deadlocks, and to list the possible outputs or final values of concurrent code.

7 previous-year questions · 7 solved · 5 practice questions

Race conditions and the critical-section problem

A race condition occurs when the result of a computation depends on the order in which instructions of concurrent processes (or threads) are interleaved on shared data. A statement such as x = x + 1 is not atomic: it compiles to load x into a register, add 1, store the register into x. A context switch between these steps can lose an update.

The critical section (CS) is the part of a process that accesses shared data. Each process has the structure: entry section, critical section, exit section, remainder section. A correct solution must satisfy three requirements.

Requirement

Meaning

Mutual exclusion

At most one process is inside its critical section at any time.

Progress

If no process is in its CS and some want to enter, only processes that are not in their remainder sections take part in choosing who enters next, and this choice cannot be postponed indefinitely. A process in its remainder section must not stop others from entering.

Bounded waiting

After a process has requested entry, there is a bound on how many times other processes may enter their critical sections before its request is granted. This prevents starvation.

EXAM TIP

Progress is violated when a process that is in its remainder section (not interested in the CS) can stop another process from entering. Bounded waiting is about starvation; deadlock violates progress. A solution that gives mutual exclusion only is not a solution of the critical-section problem.

INTERLEAVINGS OF TWO INSTRUCTION SEQUENCES

Number of distinct interleavings of two independent sequences of m and n instructions, each sequence keeping its own order. Different interleavings may still give the same output.

CONDITION FOR TWO STATEMENTS TO RUN CONCURRENTLY

R(S) is the set of variables statement S reads and W(S) the set it writes. If all three intersections are empty, S1 and S2 give the same result whichever runs first or if they run in parallel.

Software solutions and why naive ones fail

Two processes P0 and P1 are used, with i the current process and j = 1 - i the other. Assume instructions execute in program order and memory reads and writes are atomic (sequential consistency).

Attempt

Idea

Result

Strict alternation

Shared turn. Process i waits while (turn != i), runs the CS, then sets turn = j.

Mutual exclusion holds. Progress fails: if P1 is slow or in its remainder section, P0 cannot enter twice in a row. Strict alternation is forced.

Flag, set after check

while (flag[j]); flag[i] = true; then CS.

Mutual exclusion fails: both can pass the while loop before either sets its flag.

Flag, set before check

flag[i] = true; while (flag[j]); then CS.

Mutual exclusion holds. If both set their flags first, both wait forever: deadlock, so progress fails.

Peterson's solution

Combines flag[] (I want to enter) with turn (whose turn to yield).

Mutual exclusion, progress and bounded waiting all hold for two processes.

Peterson's solution for process i:

/* shared: bool flag[2] = {false, false}; int turn; */
flag[i] = true;
turn = j;
while (flag[j] && turn == j)
    ;  /* busy wait */
/* critical section */
flag[i] = false;
/* remainder section */

Why it works: if both want to enter, both set turn, and the last write wins. The process whose write was overwritten (turn equals its own index) enters, and the other waits. When it leaves, it clears its flag and the waiter proceeds, so a process waits for at most one entry of the other (bounded waiting).

EXAM TIP

Peterson's solution is for exactly two processes and assumes no reordering of the writes to flag[i] and turn. If the two entry statements are swapped (turn = j first, then flag[i] = true), mutual exclusion can fail.

For n processes, the bakery algorithm is a software solution. A process that wants to enter takes a number one larger than the largest number currently held, and the process with the smallest pair (number, process index) enters. Two processes can take the same number, so ties are broken by the lower process index. It satisfies mutual exclusion, progress and bounded waiting.

Hardware support

Software-only solutions are complex beyond two processes. Hardware offers atomic instructions that read and modify a location as one indivisible step.

Test-and-set (TAS) atomically returns the old value of a boolean and sets it to true:

bool test_and_set(bool *lock) {   /* executed atomically */
    bool old = *lock;
    *lock = true;
    return old;
}

/* shared: bool lock = false; */
while (test_and_set(&lock))
    ;  /* spin */
/* critical section */
lock = false;

The first process to execute TAS sees false and enters; the others see true and spin.

Compare-and-swap (CAS) atomically compares a location with an expected value and, only if they are equal, stores a new value. It returns the original value:

int compare_and_swap(int *v, int expected, int new_val) {   /* atomic */
    int old = *v;
    if (old == expected)
        *v = new_val;
    return old;
}

/* shared: int lock = 0; */
while (compare_and_swap(&lock, 0, 1) != 0)
    ;  /* spin */
/* critical section */
lock = 0;

Swap (exchange) atomically exchanges the contents of two locations. Each process has its own key:

/* shared: bool lock = false; local: bool key; */
key = true;
while (key == true)
    swap(&lock, &key);   /* atomic exchange */
/* critical section */
lock = false;

It behaves like the simple TAS lock: mutual exclusion holds, bounded waiting does not.

Method

Mutual exclusion

Bounded waiting

Notes

TAS / CAS / swap spin lock (simple form)

Yes

No: a process can lose the race repeatedly

Busy waiting wastes CPU. Works on multiprocessors.

TAS with a waiting array (extended form)

Yes

Yes: the leaving process hands over to the next waiting index in cyclic order, so a waiting process waits for at most n - 1 entries (n processes)

More code, but starvation-free.

Disabling interrupts

Yes on a single processor

Depends on the scheduler

Privileged and unsafe for user code. Does not stop other processors on a multiprocessor.

Semaphores, mutexes and monitors

A semaphore is an integer variable accessed only through two atomic operations. In the blocking (no busy-waiting) implementation:

wait(S) {            /* P */
    S.value--;
    if (S.value < 0) {
        /* add this process to S.list and block it */
    }
}

signal(S) {          /* V */
    S.value++;
    if (S.value <= 0) {
        /* remove one process from S.list and wake it */
    }
}

Convention used here: a negative value means that many processes are blocked on the semaphore. In the simple busy-waiting form, wait(S) is while (S <= 0) ; S--;, where the last successful test and the decrement happen as one atomic step, and the value never goes below zero.

SEMAPHORE VALUE INVARIANT

For the blocking form above: S_0 is the initial value, n_w the number of wait operations executed (including blocked ones) and n_s the number of signal operations executed. If S < 0 then -S processes are blocked. (In the busy-waiting form, n_w counts completed waits only.) Some questions use a form in which wait blocks without decrementing when S = 0, and signal wakes a blocked process instead of incrementing. There the final value is max(0, S_0 + n_s - n_w) and the number blocked is max(0, n_w - n_s - S_0).

Kind

Value range

Typical use

Binary semaphore

0 or 1

Mutual exclusion, signalling between two processes.

Counting semaphore

Initialised to a non-negative count (the number of available resources); negative only in the blocking convention

Managing a pool of identical resources, counting free or full buffer slots. Initialised to k, it lets at most k processes past wait at once.

For mutual exclusion initialise a binary semaphore mutex = 1 and bracket the CS with wait(mutex) and signal(mutex). To force an order (statement A in P1 before statement B in P2), initialise S = 0, put signal(S) after A and wait(S) before B. A mutex lock is used only for mutual exclusion. In typical thread libraries it has an owner: only the thread that locked it may unlock it. A binary semaphore has no owner, so any process may signal it.

Busy waiting versus blocking. Busy waiting (spinlock) burns CPU cycles but avoids a context switch, so it suits very short critical sections on multiprocessors. Blocking puts the process in a wait queue and uses no CPU, but costs two context switches. Misuse of semaphores (wrong order of wait operations, a missing signal) causes deadlock or starvation. For example, with two semaphores taken in opposite orders:

/* shared: semaphore S = 1, Q = 1; */
/* P0 */            /* P1 */
wait(S);            wait(Q);
wait(Q);            wait(S);

If P0 completes wait(S) and P1 completes wait(Q), each then blocks on the semaphore the other holds: deadlock.

A monitor is a language-level construct: shared data plus the procedures that access it, with the guarantee that at most one process is active inside the monitor at a time (mutual exclusion is automatic). Inside, condition variables provide waiting: x.wait() suspends the caller and releases the monitor; x.signal() resumes one process waiting on x. If no process is waiting, x.signal() has no effect. This differs from a semaphore, whose signal is remembered in the value. Since only one process may be active, after a signal either the signaller waits and the woken process runs (signal-and-wait), or the signaller continues and the woken process runs later (signal-and-continue). Under signal-and-continue the condition may have changed, so the woken process re-checks it in a while loop.

Classic synchronisation problems

Producer-consumer (bounded buffer of N slots). Semaphores: mutex = 1 (buffer access), empty = N (free slots), full = 0 (filled slots).

/* producer */
while (1) {
    produce_item();
    wait(empty);
    wait(mutex);
    insert_item();
    signal(mutex);
    signal(full);
}

/* consumer */
while (1) {
    wait(full);
    wait(mutex);
    remove_item();
    signal(mutex);
    signal(empty);
}

EXAM TIP

The order of the two wait calls matters. If the producer does wait(mutex) before wait(empty), then with a full buffer it holds the mutex while blocked on empty, and the consumer blocks on mutex: deadlock. The order of the two signal calls does not cause deadlock.

Readers-writers (readers' priority). Many readers may read together; a writer needs exclusive access. Semaphores mutex = 1, wrt = 1, integer readcount = 0.

/* reader */
wait(mutex);
readcount++;
if (readcount == 1)
    wait(wrt);
signal(mutex);
/* read */
wait(mutex);
readcount--;
if (readcount == 0)
    signal(wrt);
signal(mutex);

/* writer */
wait(wrt);
/* write */
signal(wrt);

The first reader locks out writers and the last reader lets them in. Writers can starve if readers keep arriving.

Dining philosophers. N philosophers sit round a table with one chopstick between each pair of neighbours; eating needs both adjacent chopsticks. If every philosopher picks up the left chopstick (wait(chopstick[i])) and then the right one, all can hold one chopstick and wait forever: deadlock.

Standard remedies

  1. Allow at most N - 1 philosophers to sit (or compete) at once, using a counting semaphore initialised to N - 1.

  2. Asymmetric order: odd-numbered philosophers pick left first, even-numbered pick right first.

  3. Pick up both chopsticks only if both are available, inside a critical section or monitor.

  4. Number the chopsticks and always pick the lower-numbered one first (resource ordering).

  5. These remedies remove deadlock. They do not all remove starvation: with the both-or-none rule, a philosopher whose two neighbours keep eating alternately can wait forever.

Concurrent outputs and inter-process communication

Procedure for finding possible outputs or final values

  1. Break every statement on shared data into its atomic steps (load, compute, store) unless the question says the statement is atomic.

  2. Note each process's fixed order of steps; interleavings must preserve it.

  3. For semaphore code, record which operations can block and which signal releases them. A blocked process cannot print until released.

  4. Find the extreme cases first. For a counter that each process increments once, all processes loading the same old value gives the minimum and no overlap gives the maximum. With loops, one process can hold an old value across many updates by the others and then overwrite them, so the minimum can be far lower (see the first example below).

  5. List the reachable outputs or values and answer exactly what is asked (a count, a minimum, or whether a given output is possible).

WORKED EXAMPLE

Shared integer x = 0. Processes P1 and P2 each execute the loop body x = x + 1 twice, where each execution is the three atomic steps load, add, store (no other synchronisation). What are the minimum and maximum final values of x?

Solution
  1. Maximum: run P1 completely and then P2 completely. Each of the four increments sees the previous result, so x = 4.

  2. Minimum: P1 loads 0. P2 completes its first increment (x = 1) and then loads 1 for its second. P1 stores 1 (x = 1). P1 then completes its second increment (loads 1, stores 2, x = 2). Finally P2 stores 2 (its loaded 1 plus 1). x = 2.

  3. Final value 1 is impossible: stores always write a value of at least 1, so x never returns to 0, and the last store is some process's second store, which loaded a value already at least 1 and therefore writes at least 2.

  4. So the final value is between 2 and 4.

Answer: Minimum 2, maximum 4.

WORKED EXAMPLE

Semaphores S = 0 and T = 0. P1 executes: print A; signal(S); wait(T); print B. P2 executes: wait(S); print C; signal(T); print D. List all possible outputs.

Solution
  1. P2 blocks at wait(S) until P1 executes signal(S), which comes after A. So A is printed first.

  2. C comes after A, since it needs the signal on S.

  3. P1 blocks at wait(T) until P2 executes signal(T), which comes after C. So B comes after C.

  4. D comes after C in P2. After signal(T), B (P1) and D (P2) can run in either order.

Answer: ACBD and ACDB.

Inter-process communication (IPC) lets cooperating processes exchange data. There are two models.

Aspect

Shared memory

Message passing

How it works

Processes map a common memory region and read and write it directly.

Processes use send(message) and receive(message) provided by the kernel.

Kernel involvement

Only to set up the region. Later access needs no system call.

Every send and receive is a system call.

Synchronisation

Programmer must provide it (semaphores, mutexes), or races occur.

Built in: a blocking receive waits for the data.

Speed

Faster for large data once set up.

Slower, but simpler for small messages and works across machines.

Examples

POSIX shared memory.

Pipes, message queues, sockets.

Message passing can be direct (the sender names the receiver) or indirect (through a mailbox or port), and blocking (synchronous) or non-blocking (asynchronous). A blocking send waits until the message is received; a blocking receive waits until a message is available. The queue on a link has zero capacity (the sender waits until the receiver takes the message, a rendezvous), bounded capacity (the sender blocks only when the queue is full) or unbounded capacity (the sender never blocks).

PREVIOUS-YEAR QUESTIONS

7 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 2 · GATE CSE 2024 Set 1 · GATE CSE 2024 Set 2 · GATE CSE 2023 · GATE CSE 2022 · GATE CSE 2020 · GATE CSE 2018

GATE 2026 CS2 Q.51

GATE 2026 · CS2Q.51 · 2 marks · MSQ

Consider three processes P1, P2, and P3 running identical code, as shown in the pseudocode below. A and B are two binary semaphores initialized to 1 and 0, respectively. X is a shared variable initialized to 0. Each line in the pseudocode is executed atomically.

Pseudocode of P1, P2, and P3

Wait(A);
Print(*);
X = X+1;
If (X == 2)
   {
      Print($);
      Signal(B);
   }
Signal(A);
Wait(B);
Print(#);
Signal(B);

Assume that any of the three processes can start to execute first and context switching can happen between these processes at any arbitrary time and in any arbitrary order.

Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?

  1. (A)

    **$*###

  2. (B)

    **$#*##

  3. (C)

    **$##*#

  4. (D)

    ***$###

Answer and solution
See the question in the paper
GATE 2026 CS2 Q.51: answer and solution

GATE 2024 CS1 Q.40

GATE 2024 · CS1Q.40 · 2 marks · MCQ

Consider the following two threads T1 and T2 that update two shared variables a and b. Assume that initially a = b = 1. Though context switching between threads can happen at any time, each statement of T1 or T2 is executed atomically without interruption.

T1:

a = a + 1;
b = b + 1;

T2:

b = 2 * b;
a = 2 * a;

Which one of the following options lists all the possible combinations of values of a and b after both T1 and T2 finish execution?

  1. (A)

    (a = 4, b = 4); (a = 3, b = 3); (a = 4, b = 3)

  2. (B)

    (a = 3, b = 4); (a = 4, b = 3); (a = 3, b = 3)

  3. (C)

    (a = 4, b = 4); (a = 4, b = 3); (a = 3, b = 4)

  4. (D)

    (a = 2, b = 2); (a = 2, b = 3); (a = 3, b = 4)

Answer and solution
See the question in the paper
GATE 2024 CS1 Q.40: answer and solution

GATE 2024 CS2 Q.46

GATE 2024 · CS2Q.46 · 2 marks · MSQ

Consider a multi-threaded program with two threads T1 and T2. The threads share two semaphores: s1 (initialized to 1) and s2 (initialized to 0). The threads also share a global variable x (initialized to 0). The threads execute the code shown below.

// code of T1
wait(s1);
x = x+1;
print(x);
wait(s2);
signal(s1);
// code of T2
wait(s1);
x = x+1;
print(x);
signal(s2);
signal(s1);

Which of the following outcomes is/are possible when threads T1 and T2 execute concurrently?

GATE 2024 CS2 question 46: Two columns of code side by side, T1 on the left and T2 on the right, as given in the stem.Tap to open full size
Figure from the GATE 2024 CS2 paper. It is part of the question.
Figure description

Two columns of code side by side, T1 on the left and T2 on the right, as given in the stem.

  1. (A)

    T1 runs first and prints 1, T2 runs next and prints 2

  2. (B)

    T2 runs first and prints 1, T1 runs next and prints 2

  3. (C)

    T1 runs first and prints 1, T2 does not print anything (deadlock)

  4. (D)

    T2 runs first and prints 1, T1 does not print anything (deadlock)

Answer and solution
GATE 2024 CS2 Q.46: answer and solution

GATE 2023 CS Q.38

GATE 2023 · CSQ.38 · 2 marks · MCQ

Consider the two functions incr and decr shown below.

incr(){
 wait(s);
 X = X+1;
 signal(s);
}
decr(){
 wait(s);
 X = X-1;
 signal(s);
}

There are 5 threads each invoking incr once, and 3 threads each invoking decr once, on the same shared variable X. The initial value of X is 10.

Suppose there are two implementations of the semaphore s, as follows:

I-1: s is a binary semaphore initialized to 1.

I-2: s is a counting semaphore initialized to 2.

Let V1, V2 be the values of X at the end of execution of all the threads with implementations I-1, I-2, respectively.

Which one of the following choices corresponds to the minimum possible values of V1, V2, respectively?

  1. (A)

    15, 7

  2. (B)

    7, 7

  3. (C)

    12, 7

  4. (D)

    12, 8

Answer and solution
See the question in the paper
GATE 2023 CS Q.38: answer and solution

GATE 2022 CS Q.19

GATE 2022 · CSQ.19 · 1 mark · MCQ

Consider the following threads, T1, T2, and T3 executing on a single processor, synchronized using three binary semaphore variables, S1, S2, and S3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

Which initialization of the semaphores would print the sequence BCABCABCA....?

GATE 2022 CS question 19: A three-column table with headers T1, T2, T3.Tap to open full size
Figure from the GATE 2022 CS paper. It is part of the question.
Figure description

A three-column table with headers T1, T2, T3. T1: while(true) wait(S3); print("C"); signal(S2);. T2: while(true) wait(S1); print("B"); signal(S3);. T3: while(true) wait(S2); print("A"); signal(S1);.

  1. (A)

    S1 = 1; S2 = 1; S3 = 1

  2. (B)

    S1 = 1; S2 = 1; S3 = 0

  3. (C)

    S1 = 1; S2 = 0; S3 = 0

  4. (D)

    S1 = 0; S2 = 1; S3 = 1

Answer and solution
GATE 2022 CS Q.19: answer and solution

GATE 2020 CS Q.34

GATE 2020 · CSQ.34 · 2 marks · MCQ

Each of a set of 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. (A)

    It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

  2. (B)

    It ensures that at most two processes are in CODE SECTION Q at any time.

  3. (C)

    It ensures that all processes execute CODE SECTION P mutually exclusively.

  4. (D)

    It ensures that at most processes are in CODE SECTION P at any time.

Answer and solution
See the question in the paper
GATE 2020 CS Q.34: answer and solution

GATE 2018 CS Q.40

GATE 2018 · CSQ.40 · 2 marks · MCQ

Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is . Three semaphores , and mutex are defined with respective initial values of 0, and 1. Semaphore denotes the number of available slots in the buffer, for the consumer to read from. Semaphore denotes the number of available slots in the buffer, for the producer to write to. The placeholder variables, denoted by P, Q, R, and S, in the code below can be assigned either or . The valid semaphore operations are: wait() and signal().

Producer:

do{
    wait(P);
    wait(mutex);
    //Add item to buffer
    signal(mutex);
    signal(Q);
}while(1);

Consumer:

do{
    wait(R);
    wait(mutex);
    //Consume item from buffer
    signal(mutex);
    signal(S);
}while(1);

Which one of the following assignments to P, Q, R and S will yield the correct solution?

  1. (A)

    P: full, Q: full, R: empty, S: empty

  2. (B)

    P: empty, Q: empty, R: full, S: full

  3. (C)

    P: full, Q: empty, R: empty, S: full

  4. (D)

    P: empty, Q: full, R: full, S: empty

Answer and solution
See the question in the paper
GATE 2018 CS Q.40: answer and solution

OTHER EXAMS

ISRO: 3 questions

From ISRO's Scientist/Engineer written test: ISRO CS 2025, ISRO CS 2023. They are not counted in the GATE numbers on this page.

ISRO CS 2023 Q.49

ISRO 2023 · CSQ.49 · 1 mark · MCQ

Purpose of priority inheritance protocol in synchronization mechanisms is

  1. (A)

    To prevent priority inversion in systems having two or lesser priorities for a resource

  2. (B)

    To provide mutual exclusion between threads and prevent data corruption

  3. (C)

    To prevent priority inversion and ensure fairness in resource allocation

  4. (D)

    To allow multiple threads to access any resource simultaneously

Answer and solution
See the question in the paper
ISRO CS 2023 Q.49: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · MCQ

Two processes use a shared variable turn initialised to 0. Process Pi (i = 0 or 1) executes while (turn != i) ;, then its critical section, then turn = 1 - i. Which requirement of the critical-section problem does this solution violate?

  1. (A)

    Mutual exclusion only

  2. (B)

    Progress

  3. (C)

    Bounded waiting only

  4. (D)

    None of the three requirements

Answer and solution

Answer: (B)

  1. Only the process whose index equals turn can enter, so two processes are never inside together: mutual exclusion holds.

  2. P0 can enter only when turn = 0 and it changes turn to 1 on exit. If P1 never comes (it is in its remainder section), P0 cannot enter a second time, although no process is in its CS.

  3. A process outside its critical section blocks another one, so progress is violated.

PRACTICE 21 mark · NAT

A counting semaphore S is initialised to 3. Using the blocking implementation in which a negative value equals the number of blocked processes, 8 wait(S) operations and 2 signal(S) operations are executed in some order (no other operations). How many processes are blocked on S at the end?

Answer and solution

Answer:

  1. Final value = 3 - 8 + 2 = -3.

  2. A negative value gives the number of blocked processes, so 3 processes are blocked.

PRACTICE 32 marks · MSQ

A critical section is protected by a simple spinlock: while (test_and_set(&lock)) ; as the entry section and lock = false; as the exit section, with lock initially false and test_and_set executed atomically. Which statements are correct?

  1. (A)

    Mutual exclusion is guaranteed.

  2. (B)

    Bounded waiting is guaranteed.

  3. (C)

    A process waiting for the lock consumes CPU time while it waits.

  4. (D)

    The lock works even if test_and_set is not atomic.

Answer and solution

Answer: (A), (C)

  1. A is true: only the process that executes test_and_set while lock is false gets the value false back and enters; all others get true.

  2. B is false: with several waiters, the same process can win the race each time the lock is released, so there is no bound on the wait.

  3. C is true: waiting is a busy loop (spinning).

  4. D is false: if two processes read false before either sets true, both enter.

PRACTICE 42 marks · NAT

Shared integer x = 0. Three processes P1, P2 and P3 each execute the single statement x = x + 1 exactly once. The statement is not atomic: it consists of the separate atomic steps load x into a register, add 1 to the register, and store the register into x. Each process uses its own register. How many distinct final values of x are possible?

Answer and solution

Answer:

  1. Final value 3: run the processes one after another.

  2. Final value 2: P1 and P2 both load 0 and store 1 (x = 1); P3 then loads 1 and stores 2.

  3. Final value 1: all three load 0 first, and all three store 1.

  4. Every store writes (loaded value + 1) with loaded value at least 0, so x is at least 1. At most three increments, so x is at most 3, and no value outside 1 to 3 is possible.

  5. The possible values are 1, 2 and 3: three values.

PRACTICE 52 marks · MCQ

A bounded buffer has N = 2 slots. Semaphores: mutex = 1, empty = 2, full = 0. The producer executes wait(mutex); wait(empty); insert item; signal(mutex); signal(full); in a loop. The consumer executes wait(full); wait(mutex); remove item; signal(mutex); signal(empty); in a loop. Which statement is true?

  1. (A)

    The solution is correct for any speeds of the processes.

  2. (B)

    A deadlock can occur when the buffer is full.

  3. (C)

    A deadlock can occur when the buffer is empty.

  4. (D)

    Mutual exclusion on the buffer can be violated.

Answer and solution

Answer: (B)

  1. When the buffer is full, empty = 0. The producer executes wait(mutex) (mutex becomes 0) and then blocks on wait(empty) while holding the mutex.

  2. The consumer executes wait(full) successfully (full goes from 2 to 1), then blocks on wait(mutex). The producer waits for the consumer to signal empty, and the consumer waits for the producer to release mutex: deadlock. So A is false and B is true.

  3. When the buffer is empty, the consumer blocks on wait(full) before taking the mutex, so the producer can proceed: C is false.

  4. The mutex still brackets every buffer access, so mutual exclusion holds: D is false.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

inter‐process communication, concurrency and synchronization

Preparing in Kolkata? IAE Academy's course for GATE CSE.