Given below is a program which when executed spawns two concurrent processes :…

2005

Given below is a program which when executed spawns two concurrent processes :

semaphore X : = 0 ; /* Process now forks into concurrent processes P1 & P2 */

P1 : repeat forever P2 : repeat forever

V (X) ; P(X) ;

Compute ; Compute ;

P(X) ; V(X) ;

Consider the following statements about processes P1 and P2:

  1. It is possible for process P1 to starve.

  2. It is possible for process P2 to starve.

Which of the following holds?  

  1. A.

    Both I and II are true

  2. B.

    I is true but II is false

  3. C.

    II is true but I is false

  4. D.

    Both I and II are false

Attempted by 161 students.

Show answer & explanation

Correct answer: A

Answer: Both processes can starve.

  • Why the second process can starve: If the scheduler always runs the process that executes V(X) then Compute then P(X), that process can repeatedly perform its V and then its P without letting the other process run. The second process never gets CPU time to perform its initial P(X) and so is starved.

  • Why the first process can starve: After an initial V by the first process, the scheduler may repeatedly run the second process (which does P(X), Compute, V(X)). If the scheduler keeps scheduling the second process, the first process may never be scheduled or never be unblocked at its P(X), so it can be starved as well.

Reason: Semaphores do not by themselves provide fairness. Without a fair scheduler or additional synchronization to ensure bounded waiting, either process can be prevented from making progress by an adversarial scheduling sequence.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir