- The statements
counter++;
counter--;
must be performed atomically.
- Atomic operation means an operation that completes in its entirety without interruption.
- The statement “count++” may be implemented in machine language as:
register1 = counter
register1 = register1 + 1
counter = register1
counter = register1
- The statement “count—” may be implemented as:
register2 = counter
register2 = register2 – 1
counter = register2
- If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved.
- Interleaving depends upon how the producer and consumer processes are scheduled.
- Assume counter is initially 5. One interleaving of statements is:
producer: register1 = counter (register1 = 5)
producer: register1 = register1 + 1 (register1 = 6)
consumer: register2 = counter (register2 = 5)
consumer: register2 = register2 – 1 (register2 = 4)
producer: counter = register1 (counter = 6)
consumer: counter = register2 (counter = 4)
- The value of count may be either 4 or 6, where the correct result should be 5.
No comments:
Post a Comment