779 views

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)

### 1 comment

yes ,it i think it is possible for both of them that they may starve if we consider preemption at point no.2 after that we give chance for p2 then p1 will be starve,in a similar way if we consider execution for p1 then p2 will be starve.correct me if i am wrong???

Process P2 can be starved as for its execution it needs the process P1 to execute the V(x) operation before & in the case when only P2 is present in the system it can be starved.

Process P1 will not starve as after P2  has executed its V(x) operation &  P1 loops on P(x) then there is a fair possibility for both P1 & P2 to get the next access to the semaphore x.

2 is true and 1st is fals .

so option D is correct ans .

by
Ans will be a. p2 will starve if p1 never executes at all and p1 can starve if p1 preempts at Compute and p2 enters into Compute and keeps on executing Compute and donot allow P1 to execute further
by