in Operating System edited by
953 views
1 vote
1 vote

It says as follows:

Pseudocode

Integer X = 0 , Y = 23 ;

Semaphore mx = 1 , my = Null ;

Codebegin

       Begin

            P(mx);

              x= x+1;

            V(my);

      End

       Begin

            P(my);

              x= y+1;

            V(mx);

      End

Question: Possible final values of X??

I tried to find the values and i concluded that 1 and 24 could be the final values but solution says only 24 :(

Any idea??

in Operating System edited by
953 views

1 Answer

4 votes
4 votes
Best answer

Assuming mx and my as binary semaphores , 

The part of the code which is doing down operation on 'mx' will be successful initially and not the one doing down operation on 'my' variable..

Hence the code corresponding to 'mx' variable will be executed first followed by that of 'my' variable..This will not be executed other way as down on 'my' variable is not possible initially..

Hence the code will runs :

P(mx)  ;  

x = x + 1       //  x is incremented to 1

V(my)  ;       //  my is set to 1 now

P(my)  ;     // successful down operation

x = y + 1   // x is updated as 23 + 1  =  24..

V(mx) ;

This way the entire code is executed ..Thus the second part will execute after first part only.Thus final value of 'x' is decided by the second part..

Hence final value of  x  =   24 

selected by

2 Comments

I figured that out as soon as i typed the question :)
1
1
That's great @vishwa ratna..:) The art of debugging one's mistakes oneself is very helpful in the long run..
2
2

Related questions