Consider the below program as two concurrent processes:
Semaphore x:= 0;
P1: repeat forever P2: repeat forever
1.V(x); 4. P(x);
2.Compute; 5.Compute;
3.P(x); 6.V(x);
Consider the following statements about process P1 & P2:
(1) It is possible for process P1 to starve.
(2) It is possible for process P2 to starve.
Which of the following is true?
(a) Both 1 & 2 are true (b) Both 1 & 2 are false
(c) 1 is true & 2 is false (d) 2 is true & 1 is false
NOTE: I am getting as both the statements are true.My explanation is as follows:
for statement 1: IT IS POSSIBLE FOR P1 TO STARVE
I have numbered the instructions so that i could show you the order of execution of process that would lead to both the statement to be true ,consider the below order of execution
P1:1,2 || P2:4,5,6,4,5,6,4,5,6....
P1 might not get the chance ,leaving it to starve ....as initially value of the semaphore "x" was 0, and then when p1 starts executing ,it will make value of x as 1 and will preampt at line 2 , now p2 will run and will run the lines 4,5,6 without ever preampting , and leaving P1 to starve ( please comment if my logic is wrong)
for statement 2 : IT IS POSSIBLE FOR P2 TO STARVE
consider the following order of execution:
P1:1,2,3,1,2,3,12,3.....
P2 might never get the chance to run , and it wil starve
(THIS IS WHAT I THOUGHT THE ANSWER SHOULD BE , PLEASE COMMENT, IF I AM WRONG ANYWHERE)