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 in Operating System

Critical section

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:

  1. 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.
  2. 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.
  3. Bounded Waiting — a process attempting to enter its critical region will be able to do so eventually
Critical Section in Operating System

Real-life example

 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.

Pseudocode of Critical Section Problem

while(true){
// entry section
process_entered=1;
//loop until process_eneterd in 0
while(process_entered);
// critical section
// exit section
process_entered=0
// remainder section
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription