edited by
948 views
1 votes
1 votes

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??

edited by

1 Answer

Best answer
4 votes
4 votes

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

Related questions

0 votes
0 votes
0 answers
1