Critical Section Problem in Operating System
- A section of code or set of operations, in which process may be changing shared variables, updating a common file or a table etc. is known as critical section of that process.
- A process is in the critical section if it executes code that manipulates shared data and resources. In other words, critical section is that overlapping portion of each process where shared data and resources are accessed.
For example, consider the following two processes P1 and P2 modifying the global variable i Global int i=0;
Increment i by 1
Store the result into i
Increment i by 2
Store the result into i
- In the above example, global variable i is being accessed by both P1 and P2 hence this is a critical section problem.
- The critical section problem is to design a protocol to achieve cooperation among processes.
- Each process should seek the permission to enter its critical section. The code section that implements this request is called entry section.
- There can be an exit section that follows critical section.
- The remaining code comes under the remainder section.
- For the processes that execute concurrently, it should be ensured that the critical section should be made atomic.
- Atomic means that either an operation in the critical section should happen entirely or not at all i.e. it couldn’t be interrupted in the middle.
There are three necessary and sufficient requirements for critical section solution. These are :
1. Mutual Exclusion :
It states that if one process is executing in its critical section, then no other process can execute in that critical section. Thus only one of the processes is allowed to execute its critical section at a time. No two critical sections can be under execution simultaneously. This is also known as mutual exclusion principle.
2. Bounded Wait :
It states that if a process makes a request to enter its critical section and before that request is granted, there is a limit on number of times, other processes are allowed to enter that critical section.
- It states that process cannot stop another processes from entering their critical sections, if it is not executing in its critical section.
There are four assumptions in the critical section problem. These assumptions are:
- No assumption can be made about relative speeds or the number of process
- No process exists for indefinite interval of time in its critical section.
- It is not possible to interrupt a memory operation.
- A number of mechanisms have been used to implement mutual exclusion. Thus mechanisms include hardware supported solutions, operating system primitive software solutions and constructs implemented in programming languages.