Let m[0] ... m[4] be mutexes (binary semaphores) and P[0] ... P[4] be…

2000

Let m[0] ... m[4] be mutexes (binary semaphores) and P[0] ... P[4] be processes. Suppose each process P[i] executes the following:

wait(m[i]);
wait(m[(i + 1) mod 5]);

// critical section

release(m[i]);
release(m[(i + 1) mod 5]);

This could cause:

  1. A.

    Thrashing

  2. B.

    Deadlock

  3. C.

    Starvation, but not deadlock

  4. D.

    None of the above

Attempted by 25 students.

Show answer & explanation

Correct answer: B

Each process first acquires m[i] and then tries to acquire m[(i + 1) mod 5]. If all five processes acquire their first mutex at the same time, then:

P[0] holds m[0] and waits for m[1].
P[1] holds m[1] and waits for m[2].
P[2] holds m[2] and waits for m[3].
P[3] holds m[3] and waits for m[4].
P[4] holds m[4] and waits for m[0].

This forms a circular wait, and none of the processes can proceed. Therefore, the code can cause deadlock.

Explore the full course: Gate Guidance By Sanchit Sir