edited by
2,266 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

1.3k
views
2 answers
9 votes
makhdoom ghaya asked Dec 8, 2015
1,291 views
Let $t_{n}$ be the sum of the first $n$ natural numbers, for $n 0$. A number is called triangular if it is equal to $t_{n}$ for some $n$. Which of the following statemen...
4.2k
views
4 answers
32 votes
makhdoom ghaya asked Dec 5, 2015
4,189 views
There is a set of $2n$ people: $n$ male and $n$ female. A good party is one with equal number of males and females (including the one where none are invited). The total n...
1.6k
views
1 answers
14 votes
makhdoom ghaya asked Dec 5, 2015
1,590 views
Consider the following $3 \times 3$ matrices.$M_{1}=\begin{pmatrix} 0&1&1 \\1&0&1 \\1&1&0 \end{pmatrix} $$M_{2}=\begin{pmatrix} 1&0&1 \\0&0&0 \\1&0&1 \end{pmatrix} ...
3.1k
views
5 answers
25 votes
makhdoom ghaya asked Dec 8, 2015
3,137 views
Consider the following grammar (the start symbol is $E$) for generating expressions.$E \rightarrow T - E \mid T + E \mid T$$T \rightarrow T * F \mid F$$F \rightarrow 0 \m...