18 views

Consider the program given below using parbegin-parend.

Semaphore ml = 0, m = 1;

integer x = 0, y = 23;

parbegin
begin

V(ml);

x = x + 1;

p(m);

end

begin

V(ml);

x = y + 1;

p(ml);

end

parend



What are final values of x?

I am getting 24 but not 1 and 25.

Can someone please tell me how I can get 1 and 25.

| 18 views
0
bro consider the operations

x=y+1;

LD y

STORE x;

x=x+1;

LD x

as non atomic you will get the answer
0

So for getting 1. x will be incremented to 1 and then prempt right?

After that it will go to x = y + 1;

Here, it will also preempt and we will get 1 as the answer.

Is this calculation right?

+1
x=y+1;                          x=x+1;

LD r2,y;                        LD r1,x;

r2=r2+1;                        r1=r1+1;

ST r2,x;                        ST r1,x;

now to get x=1:

perform all operation of x=y+1 but before storing prempt it and then perform all the operations of x=x+1 and before storing preempt it again. and now perform ST r2,x;  and then  ST r1,x; so the final storing value is from r1 so x will be updated to 1.
0
Thanks