edited by
576 views
1 votes
1 votes

Two processes $P_1$ and $P_2$ have a common shared variable count. While $P_1$ increments it, $P_2$ decrements it. Given that $R_0$ is a register, the corresponding assembly language codes are:

$P_1$ count++   $P_2$ count--  
MOV $count$ $R_0$ MOV $count$ $R_0$
ADD #1 $R_0$ SUB #1 $R_0$
MOV $R_0$ $count$ MOV $R_0$ $count$

 $$\begin{array}{} \hline P_{1} & & \text{count}++ & & &  P_{2}  & \text{count}--  \\\hline  \text{MOV} & & \text{count} & \: R_{0} & &\text{MOV} & \text{count} &\: R_{0}  \\\hline  \text{ADD} & & \#1 & \: R_{0} & &\text{SUB} &\#1 &\: R_{0} \\\hline  \text{MOV} & &  R_{0}  & \text{count} & &\text{MOV} & \: R_{0}  & \text{count} \\ \hline \end{array}$$

Give an example to justify whether a race condition may occur if $P_1$ and $P_2$ are executed simultaneously.

edited by

1 Answer

1 votes
1 votes
Yes, RACE condition (output changing depending on the execution sequence) can occur.
Consider initial value of count as $10$ and $P_1$ and $P_2$ executing. The expected final value is $10.$

Consider $P_1$ doing the $\text{MOV}$ followed by $P_2$ doing the same. Now, if $P_1$ does the remaining two instructions it will write back the value as $11$ while $P_2$ after finishing the remaining two instructions will write back the value as $9$ hence causing race condition (this scenario is similar to dirty-read in database transactions).

Related questions