Process Synchronization
Process Synchronization
Primary
Mutual Exclusion
Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if one
process is executing inside critical section then the other process must not enter in the
critical section.
Progress
Progress means that if one process doesn't need to execute into critical section
then it should not stop other processes to get into the critical section.
Secondary
Bounded Waiting
We should be able to predict the waiting time for every process to get into the
critical section. The process must not be endlessly waiting for getting into the
critical section.
There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to
enter critical section and before that request is gurnated.
Solutions To The Critical Section
In Process Synchronization, critical section plays the main role so that the
problem must be solved.
Here are some widely used methods to solve the critical section problem.
Peterson Solution:-
Peterson’s solution is widely used solution to critical section problems. This
algorithm was developed by a computer scientist Peterson that’s why it is named
as a Peterson’s solution.
In this solution, when a process is executing in a critical state, then the other
process only executes the rest of the code, and the opposite can happen. This
method also helps to make sure that only a single process runs in the critical
section at a specific time.
PROCESS Pi
FLAG[i] = true
while( (turn != i) AND (CS is !free) )
{
wait;
}
CRITICAL SECTION FLAG[i] = false
turn = j; //choose another process to go
to CS
• Assume there are N processes (P1, P2, … PN) and every process at some point
of time requires to enter the Critical Section
• A FLAG[] array of size N is maintained which is by default false. So, whenever a
process requires to enter the critical section, it has to set its flag as true. For
example, If Pi wants to enter it will set FLAG[i]=TRUE.
• Another variable called TURN indicates the process number which is currently
waiting to enter into the CS.
• The process which enters into the critical section while exiting would change
the TURN to another number from the list of ready processes.
• Example: turn is 2 then P2 enters the Critical section and while exiting turn=3
and therefore P3 breaks out of wait loop.
Synchronization Hardware
Some times the problems of the Critical Section are also resolved by hardware.
Some operating system offers a lock functionality where a Process acquires a lock
when entering the Critical section and releases the lock after leaving it.
So when another process is trying to enter the critical section, it will not be able
to enter as it is locked. It can only do so if it is free by acquiring the lock itself.
Mutex Locks
Synchronization hardware not simple method to implement for
everyone, so strict software method known as Mutex Locks was also
introduced.
In this approach, in the entry section of code, a LOCK is obtained over
the critical resources used inside the critical section. In the exit section
that lock is released.
Semaphore Solution
Semaphore is simply a variable that is non-negative and shared
between threads. It is another algorithm or solution to the critical
section problem. It is a signaling mechanism and a thread that is waiting
on a semaphore, which can be signaled by another thread.
It uses two atomic operations, 1)wait, and 2) signal for the process
synchronization.
Classical Problems of Synchronization
If the processes are run simultaneously they will not yield the expected output.
The solution to this problem is creating two semaphores, one full and the other empty
to keep a track of the concurrent processes.
Sleeping Barber Problem
When there are no customers the barber sleeps in his chair. If any customer
enters he will wake up the barber and sit in the customer chair. If there are no
chairs empty they wait in the waiting queue.
Dining Philosopher’s problem
This problem states that there are K number of philosophers sitting around a
circular table with one chopstick placed between each pair of philosophers. The
philosopher will be able to eat if he can pick up two chopsticks that are adjacent
to the philosopher.
This problem occurs when many threads of execution try to access the same
shared resources at a time. Some threads may read, and some may write. In this
scenario, we may get faulty outputs.