What is Critical Section in Operating System
Critical Section in Operating System
Here, on this page, we will learn what is critical section in operating system and how to avoid problems like deadlock by managing critical sections efficiently.
Critical Section is any piece of code that is shared between different processes. If more than one process tries to operate in critical section we can reach to undesired output or else if one process starts to operate in critical section and does not release it then it may lead to deadlock, therefore, a single process is allowed to enter at a time. Whenever a process has entered a critical section, it doesn’t allow other processes to enter it by locking it for time being.
First, we have an entry section where each process checks whether the critical section is lock-free or not. If there is a lock then the process needs to wait else if there is no lock then the current process acquires the lock and enters the critical section. Once the process has been executed it releases the lock it has acquired. Whenever the next process comes it acquires the lock and then moves inside the critical section.
Necessary and sufficient conditions for a solution to the critical section problem:
- Mutual Exclusion — Only a single process is allowed to enter a critical section at a time. If a process pi is executing in its critical section then no other process is allowed to enter the critical section at that time.
- Progress — a process operating outside of its critical section cannot prevent other processes from entering theirs; processes attempting to enter their critical sections simultaneously must decide which process enters eventually.
- Bounded Waiting — a process attempting to enter its critical region will be able to do so eventually
Bathroom: At a time only a single person can use the bathroom. Whenever a person enters the bathroom, he/she locks the bathroom so that no other person can enter it. Any other person who wants to use the bathroom needs to wait outside till the person comes out. Here washroom is an example of a critical section and people are examples of the process.