Operating Systems · Process Synchronization Basics
Data Inconsistency with Shared Memory (count++ / count-- Example)
Overview
Data inconsistency is a possible error when two or more processes access and update the same shared variable or shared memory location. The operating system can give both processes access to a common shared-memory area, through which the processes communicate.
Key concepts
- Data inconsistency: a possible error when two or more processes access and update the same shared variable or shared memory location.
- Shared memory: the operating system can give both processes access to a common shared-memory area, through which the processes communicate.
- Shared variable: both processes use a common variable named
count; because both processes use the same variable, they have some shared memory to which both are given access by the operating system. - Concurrent updates: P1 wants to increment the shared variable and P2 wants to decrement the same shared variable; since both processes access the same shared memory/variable, data inconsistency is very possible, and other errors may also occur.
- Read-modify-write operations:
count++andcount--are read-modify-write operations, so interleaving can produce an incorrect result.
How it works
- Two processes, P1 and P2, are running in the computer.
- Both processes use a common variable named count.
- Because both processes use the same variable, this means they have some shared memory to which both are given access by the operating system.
- The initial value of the shared variable is
count = 5. - The code of P1 contains, at some point in its program, the operation
count++. - The code of P2 contains, at some point in its program, the operation
count--. - P1 wants to increment the shared variable, and P2 wants to decrement the same shared variable.
- Since both processes access the same shared memory/variable, data inconsistency is very possible, and other errors may also occur.
Worked examples
Shared variable:
| Item | Value / Operation |
|---|---|
| Shared variable | count |
| Initial value | 5 |
| Process P1 operation | count++ |
| Process P2 operation | count-- |
Pseudo-code representation:
/* Process P1 */
...
count++;
...
/* Process P2 */
...
count--;
...
- P1 performs increment on
count. - P2 performs decrement on
count. - Both operations are on the same shared variable.
- Because the two processes are concurrently using shared memory, the final value of
countcan become inconsistent depending on how their operations interleave.
Revision summary
- Shared memory allows processes to communicate through common variables.
- Data inconsistency is a major risk when multiple processes update the same shared variable.
- In the example, P1 does
count++and P2 doescount--on the same variablecount. - The initial value of
countis 5. - Both processes access the shared variable at some point in their programs, not necessarily at the same instruction location.
- Concurrent access to shared data can lead to data inconsistency and other errors.
count++andcount--are read-modify-write operations, so interleaving can produce an incorrect result.
Previous-year exam insights
- Race condition: when two or more processes access shared data concurrently and the final result depends on the order (interleaving) of their accesses, the situation is called a race condition; the critical section must be protected to avoid it.
Asked inGATE 2019GATE 2013 - Bernstein's conditions for two processes Pi and Pj to be safely executed concurrently (result independent of interleaving): R(Pi) ∩ W(Pj) = ∅, W(Pi) ∩ R(Pj) = ∅, and W(Pi) ∩ W(Pj) = ∅;
only R(Pi) ∩ R(Pj) = ∅ is NOT required (concurrent reads are harmless).
Asked inUGC NET 2015 - Each high-level statement such as x = x + k or x++ is not atomic: it is compiled into load (read x into register), modify (add/subtract in register), store (write back to memory), and interleaving can occur between any of these steps.
Asked inGATE 2019GATE 2013 - To find the minimum possible final value, let each process read the same initial value and write last (lost updates); to find the maximum, let each process read the value written by the previous process (serial execution).
Asked inGATE 2019GATE 2013 - A counting semaphore's P (wait) operation decrements the semaphore and blocks if the value becomes negative, while V (signal) increments it; mutual exclusion is achieved when all processes execute P before their critical section and V after it.
Asked inGATE 2013 - The critical-section problem requires three conditions: mutual exclusion, progress (no process outside the critical section blocks others), and bounded waiting (no process waits indefinitely).