Last time we learned about the Process Synchronization and Race Condition where in race condition a two processes running at the same time and accessing the same variable can result in unwanted results which can hinder the ability of other processes to work properly in the future.
Today we will be discussing about the Critical Section Problem in process synchronization. This problem is discussed in order to prevent race conditions from occuring. Critical section problem states that no two processes should access the same varibale or data at the same time.
Consider a system where there are n number of processes running at the same time. Each process has a segment of code called critical section in which they share, access or manipulate data. The important thing here is that when one process is executing in this critical condition no other process should be allowed to execute in critical condition.
The critical section problem is to design a protocol to let all the processes synchronize their execution in order to share data cooperatively. Each process must ask for permission to enter the critical section. The section of code that is using the shared data is called the entry section and after entry section is done executing the process enters exit section. The remainder of code is called remainder section.
A solution to the Critical-Section Problem must satisfy the following three requirements.
- Mutual exclusion. If a process P1 is executing in the critical section, then no other process can be executing in the critical sections.
- Progress. If no process is executing in its critical section then some process that is not executing in their remainder section can ask for entering the critical section. The selection cannot be postponed indefinately.
- Bounded Waiting. There is a limit on the number of times that other processes are allowed to enter their critical section after a process has made a request to enter the critical section and that process is granted the access to the critical section.
Following are two approaches that are used to handle the critical section of processes in the operating systems.
Preemptive Kernels
Preempted kernel allows a process to be preempted while it is running in the kernel mode. In preemptive kernels race conditions must be handeled by properly implementing the kernels so that the only one process can enter the critical section at the same time.
Non-Preemptive Kernels
Non-Preempted kernel does not allows a process to be preempted running in the kernel mode. a kernel-mode process will run unitl it exits kernle mode or blocks or voluntarily yields control of the CPU.
A nonpreemptive kernel is free of race conditiions as only one process runs in the kernel at a time.