Sometimes a process has to access shared memory or files, or do other critical things that can lead to races. That part of the program where the shared memory is accessed is called the critical region or critical section.
If we could arrange matters such that no two processes were ever in their critical regions at the same time, we could avoid races. Although this requirement avoids race conditions, it is not sufficient for having parallel processes cooperate correctly and efficiently using shared data.
We need four conditions to hold to have a good solution:
- No two processes may be simultaneously inside their critical regions.
- No assumptions may be made about speeds or the number of CPUs.
- No process running outside its critical region may block other processes.
- No process should have to wait forever to enter its critical region.
Here process A enters its critical region at time Tj. A little later, at time T2 process B attempts to enter its critical region but fails because another process is already in its critical region and we allow only one at a time.
Consequently, B is temporarily suspended until time 73 when A leaves its critical region, allowing B to enter immediately. Eventually B leaves (at T4 ) and we are back to the original situation with no processes in their critical regions.