![]() |
VOOZH | about |
Operating system-based solutions to the critical section problem use tools like semaphores, Sleep-Wakeup and monitors. These mechanisms help processes synchronize and ensure only one process accesses the critical section at a time. They are widely used in multitasking systems for efficient resource management.
Different OS based solutions for critical section problem are:
A semaphore is a synchronization tool used in concurrent programming to control access to shared resources. It works like a counter that tracks how many processes can use a resource at a time.
Semaphores help prevent race conditions and allow coordination among multiple processes or threads. They are often called counting semaphores since their initial value decides how many processes can access the resource simultaneously
There are two types of semahore
A semaphore is a simple yet powerful synchronization tool used to manage access to shared resources in a system with multiple processes. It works by maintaining a counter that controls access to a specific resource, ensuring that no more than the allowed number of processes access the resource at the same time.
Now let us see how it does so. First, look at two operations that can be used to access and change the value of the semaphore variable.
π semaphoresA critical section is surrounded by both operations to implement process synchronization. The below image demonstrates the basic mechanism of how semaphores are used to control access to a critical section in a multi-process environment, ensuring that only one process can access the shared resource at a time.
π CriticalNow, let us see how it implements mutual exclusion. Let there be two processes P1 and P2 and a semaphore s is initialized as 1. Now if suppose P1 enters in its critical section then the value of semaphore s becomes 0. Now if P2 wants to enter its critical section then it will wait until s > 0, this can only happen when P1 finishes its critical section and calls V operation on semaphore s.
This way mutual exclusion is achieved. Look at the below image for details which is a Binary semaphore.
π Mutual Exclusion using SemaphoreThe description above is for binary semaphore which can take only two values 0 and 1 and ensure mutual exclusion. There is one other type of semaphore called counting semaphore which can take values greater than one.
In this implementation whenever the process waits it is added to a waiting queue of processes associated with that semaphore. This is done through the system call block() on that process. When a process is completed it calls the signal function and one process in the queue is resumed. It uses the wakeup() system call.
Read More about Semaphores
Monitors are advanced tools provided by operating systems to control access to shared resources. They group shared variables, procedures, and synchronization methods into one unit making sure that only one process can use the monitor's procedures at a time.
wait() and signal() to handle process synchronization:A condition variable has its own block queue.
If (x block queue empty)
// Ignore signal
else
// Resume a process from block queue.
Read More about Monitors
The Sleep and Wakeup mechanism is an operating system-based solution to the critical section problem. It helps processes to avoid busy waiting while waiting for access to shared resources.
Sleep
Wakeup
Imagine two processes, P1 and P2, want to use a printer (a shared resource):