Critical Section in Operating System
What is Critical section?
In operating systems, a critical section is a section of code that is executed by multiple threads or processes and accesses shared resources, such as variables or data structures. A critical section must be executed atomically, which means that it should not be interrupted by another thread or process that also wants to access the same shared resource.
The purpose of implementing critical sections is to ensure that shared resources are accessed in a mutually exclusive manner, so that the results of each thread's or process's operations on the shared resource are consistent and correct.
To prevent race conditions, which occur when two or more threads or processes access the same shared resource simultaneously and the outcome depends on the order in which the operations are performed, operating systems use synchronization mechanisms such as semaphores and monitors to enforce mutual exclusion in critical sections.
Example of Critical Section
A common example of a critical section is a bank account that is shared among multiple threads or processes. Suppose that two threads attempt to simultaneously withdraw money from the account, and the balance is updated after each withdrawal operation. If the threads do not access the account in a mutually exclusive manner, race conditions can occur, and the resulting balance may be incorrect.
To address this problem, the code that withdraws money from the account must be executed in a critical section. In this critical section, the mutual exclusion property ensures that only one thread can access the account at a time, and the balance is updated atomically to ensure consistency and correctness.
Here is an example of a critical section that withdraws money from a bank account:
- Enter critical section
- Read the current balance of the account
- Withdraw the requested amount from the account
- Update the account balance
- Exit critical section
In this example, only one thread or process can execute the critical section at a time, and any other thread that attempts to enter the critical section must wait until the first thread has completed its operation and exited the critical section. By enforcing mutual exclusion and atomicity in the critical section, the bank account is protected from race conditions and ensures that the resulting balance is correct and consistent.
Characteristics of Critical Section
The characteristics of a critical section in an operating system include the following:
- Shared Resource: The critical section involves accessing a shared resource that is shared among multiple threads or processes.
- Mutual Exclusion: Only one thread or process can access the shared resource in a critical section at any given time to prevent race conditions and ensure correctness of operations.
- No pre-emption: Once a thread or process enters a critical section, it cannot be pre-empted or interrupted by another thread or process until it exits the critical section. This ensures that the thread or process can complete its operations on the shared resource in a mutually exclusive manner.
- Finite Execution Time: A critical section must execute in a finite amount of time to avoid causing delays in other processes or threads that are waiting to access the shared resource.
- Blocking: If a thread or process is unable to enter a critical section due to the presence of another thread or process that is currently accessing the shared resource, it must block or wait until it can enter the critical section.
- Atomicity: Operations performed within the critical section must be performed atomically, meaning that they must execute as a single, indivisible operation. This ensures that the shared resource is left in a consistent state after each operation, regardless of any race conditions that may arise.
Solution of critical section problems
There are several solutions to the critical section problem in operating systems, which ensure that only one thread or process can access a shared resource at any given time. Some of the commonly used solutions are:
- Locks:One of the most popular solutions to the critical section problem is the use of locks, such as mutexes and semaphores, which are synchronization mechanisms that allow threads or processes to acquire exclusive access to a shared resource. A thread or process that wants to enter the critical section acquires the lock and releases it when it exits the critical section.
- Atomic Operations: Atomic operations are operations that execute as a single, indivisible unit, and cannot be interrupted by other threads or processes. By using atomic operations for operations within the critical section, the mutual exclusion property is automatically enforced.
- Monitors: Monitors are higher-level synchronization constructs that encapsulate data and procedures into a single module. Monitors provide mechanisms to enter and exit the critical section and ensure that only one thread or process can access the shared resource at any given time.
- Peterson's Algorithm: Peterson's algorithm is a classical solution to the critical section problem that uses two shared variables to enforce mutual exclusion. The algorithm allows each thread to enter its critical section while preventing other threads from entering their critical section at the same time.
- Test-and-Set Instruction: The test-and-set instruction is a hardware instruction that can be used to implement mutual exclusion. The instruction reads a memory location and sets it to a specified value in a single atomic operation. By using the test-and-set instruction, threads or processes can acquire and release locks in a mutually exclusive manner.