edited by
658 views
2 votes
2 votes

Pseudocode:

Int x = 0 ,  y =20;

Semaphores SX=1 , SY= 1;

Parbegin

Begin

             P(SX) , P(SY) ; X=X+1;

             V(SY), V(SX);                                                                                     

End

Begin

             P(SY) , P(SX) ; X=Y+2;

             V(SX), V(SY);

End

Parend

Question:

Final Values of X?

I am getting 1, 22 and deadlock.

How come answer is 22, 23 and deadlock possible??

edited by

2 Answers

3 votes
3 votes

FIRST CASE

The two processes are running concurrently, now let first parbegin executes, and it's P(SX) and P(SY), executes before parabegin of second executes P(SY) then, it would go to execute X=X+1=1, then V(SY) V(SX).

Now second Parabegin starts(remember now, the value of X is 1), and gets the control of critical section and executes, X=Y+2=(20+2)=22.

SECOND CASE

The two processes are running concurrently, now let second parbegin executes, and it's P(SY) and P(SX), executes before parabegin of first executes P(SS) then, it would go to execute X=Y+2=22, then V(SX) V(SY).

Now first Parabegin starts(remember now, the value of X is 22), and gets the control of critical section and executes, X=X+1=(22+1)=23.

THIRD CASE

The two processes are running concurrently, and the first parbegin executes, and its P(SX) executes and then there is interrupt occurs and CPU assigned with the second parbegin which executes P(SY), here comes the deadlock.

1 votes
1 votes

i think in the question they forget to ask maximum values possible....

for getting max values of X...we execute down operation for 2nd loop...which is P(SY)P(SX)...x=20+2=22....now we perform up on semaphore and went for 1st ...portion where we get X=22+1=23.....

if they asked minimum values possible..you are correct...1 and 22...(NOT 23 as you written)....

and yes dead-lock too possible...

Related questions

0 votes
0 votes
1 answer
1
Mrityudoot asked Jan 27
178 views
Can a counting semaphore acquire a negative value?S = 2;15 P operations done, should the semaphore be 0 or -13
1 votes
1 votes
1 answer
2
0 votes
0 votes
1 answer
3
Na462 asked Jul 19, 2018
546 views
0 votes
0 votes
1 answer
4
Na462 asked Jul 14, 2018
689 views