|
What is Wrong?
- Variable count is shared.
- Both process read and modify this variable
- The assembly code for these two statement may be something like following:
//count=count+1
|
|
P1: |
load |
R1,count |
C1: |
load |
R1,count |
P2: |
add |
R1,1 |
C2: |
sub |
R1,1 |
P3: |
store |
count,R1 |
C3: |
store |
count,R1 |
- Two processes run concurrently (Single or multiple CPUs)
A Possible Scenario
- The producer and consumer processes may be scheduled in any order and may be preempted.
- Consider the following sequence of statements
- P1, CSwitch, C1, C2, C3, CSwitch, P2, P3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|