edited by
2,168 views
15 votes
15 votes

Consider the following concurrent program (where statements separated by | | with-in cobegin-coend are executed concurrently).

x:=1 
cobegin 
   x:= x + 1 ||    x:= x + 1  ||    x:= x + 1  
coend

Reading and writing of variables is atomic but evaluation of expressions is not atomic. The set of possible values of $x$ at the end of execution of the program is

  1. $\left \{ 4 \right \}$
  2. $\left \{ 2, 3, 4 \right \}$
  3. $\left \{2, 4  \right \}$
  4. $\left \{ 2, 3 \right \}$
  5. $\left \{2  \right \}$
edited by

3 Answers

Best answer
14 votes
14 votes

Reading and writing is atomic but evaluation not atomic

So, 

  1. read $1$, evaluate $1$ time, write $2$
  2. read $1$, evaluate $2$ time, write $3$
  3. read $1$, evaluate $3$ time, write $4$

Answer will be (B) $2,3,4$

edited by
3 votes
3 votes
consider 1) x=x+1 ,  2) x=x+1 ,  3) x=x+1

case 1: - [ 1) reads x=1 ] preempts -> [ 2) reads x=1 ] preempts -> [ 3) reads x=1  and perform x+1 and stores in x now x = 2 ]

                similarly [ 1) comes back performs x+1 and stores x now x=2] , for [ 2) also performs x+1 stores x now x=2]

                value of x for [ 1) is 2 ] , [ 2) is 2 ] , [ 3) is 2]  =  =  {2}

 

case 2:-  [ 1) reads x=1 perform x+1 stores in x now x = 2] , [ 2) reads x=2 ] preempts -> [ 3) reads x=2 and perform x+1 and                           stores x=3]

               similarly [ 2) comes back and perform x+1 stores x=3]

               value of x for [ 1) is 2] , [ 2) is 3 ], [ 3) is 3] == {2, 3}

 

case 3:- no one preempts [ 1) reads x= 1 and perform x+1 and stores x now x=2], [2 reads x=2 and perform x+1 and stores x now                x =3], [ 3) reads x = 3 and perform x+1 and now x=4]

              value of x for [ 1) is 2], [ 2) is 3], [ 3) is 4] == {2,3,4}

question is asked for possible values since possible values are {2}, {2,3}, {2,3,4}

but {2,3,4 } is only option which includes {2} as well as {2,3}

option is b) correct answer
1 votes
1 votes
we can do this, in this way also -

(1) first run all three parallely  so we get x= 2

(2) first run first statement(x=1+1=2) then second (x=2+1=3)then third so finally x=3+1=4

(3)first run any of two parallely so x will become x=2 then run third statement now x =2+1=3

{2,3,4} possible values of x.
Answer:

Related questions