Synchronization and Deadlocks
Synchronization and Deadlocks
Critical Section:
Each process has a code segment, called critical section, in which the shared data is accessed and it
ensure that when one process is executing in its critical section, no other process is allowed to execute in its
critical section. This called critical section problem and designed as follow
do {
entry section
critical section
exit section
reminder section
} while (1);
Mutual Exclusion. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.
Progress. If no process is executing in its critical section and there exist some processes that wish
to enter their critical section, then the selection of the processes that will enter the critical section
next cannot be postponed indefinitely.
Bounded Waiting. A bound must exist on the number of times that other processes are allowed to
enter their critical sections after a process has made a request to enter its critical section and before
that request is granted.
3.3 Semaphores:
To overcome the difficulties of critical section problem, we use a synchronization tool called a
Semaphore. It is an integer variable S that can be initialized. It can be accessed only through the standard
operation.
1. Wait (termed as P)
2. Signal (termed as V)
The definitions of wait and signal are:
General form:
Wait (S)
{
while(S<=0); // no operation
}
Signal (S)
{
S++;
}
Usage:
Binary semaphore deals for multiple processes. The n processes share a semaphore mutex, initialized
to 1. Each process Pi is organized as
do {
wait(mutex);
//critical section
Signal(mutex);
//remainder section
}while(TRUE);
Counting semaphores can be used to control access to a given resources consisting of a finite number
of instances. The semaphore is initialized to the number of resources available.
Deadlock
Deadlock:
A process requests a resource and it is not available at that time, then process enters a waiting state.
Sometimes, a waiting process will never change its state, because the resources they have requested are held
by other waiting process. This situation is called DEADLOCK.
3. No Preemption:-
Resources cannot be preempted. Resource granted to a process can be released voluntarily,
after the process has completed its task.
4. Circular Wait:- Deadlock
processes are involved in a circular chain such that each process holds one or more resource being
requested by the next process in the chain.
To ensure that deadlock never occurs, deadlock prevention & deadlock avoidance scheme is used.
No Preemption:
Here, we say that if a process is holding some resource and requests another resource that
cannot be immediately allocated to it, then all resources currently being held must be preempted.
The process will be restarted only when it can regain its old resources, as well as the new ones that it is
requesting.
Circular Wait:
To ensure that this condition never holds, we must order all resource types. It is required that
each process can only request the required resource in an increasing order.
Example: A set of resource type are assigned with following number.
F ( Tape Drive ) 1 R1
F ( Disk Drive ) 5 R2
F ( Printer ) 12 R3
Process can request R1, R2, R3 in order, but cant request as follow.
1. R1, R3, R2 or
2. R3, R2, R1 or
3. R2, R3, R1
i.e.., process can request resource only if F (R j) > F (R i)
At time To, the system is in a safe state. The sequence < P1, P0, P2 > satisfies the safety condition.
i.e.., After allocating tape drives for each process at time To. The remaining tape drives is 3. So at time T1 we
can immediately allocate the 2 tape drives for process P1, after P1 releases its tape drives i.e.., 4, we can
allocate tape drives to P0 which is 5 then finally when P0 releases its tape drives we can allocate tape drives to
P2. i.e.., 7
A system can go to unsafe state when process P2 allocates, tape drives at T1, along with P1. This is
because when P1 releases tape drive then there will be only 4 tape drive and not 5. Hence P0 and P1 will enter
in to a deadlock as they require more than 4 tape drive to release tape drive which they have holded.
Hence in safe state algorithm, the system will grant the request only if the allocation leaves the system
in a safe state.
Safety Algorithm
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i - 1,3, , n.
2. Find and i such that both:
(a) Finish [i] = false
(b) Needi Work
If no such i exists, go to step 4.
3. Work = Work + Allocationi
Finish[i] = true
go to step 2.
4. If Finish [i] == true for all i, then the system is in a safe state.
Example: consider the following RAG and its corresponding wait for graph is:
This algorithm requires an order of m x n 2 operations to detect whether the system is in a deadlocked state.
Deadlock Algorithm Usage:
When, and how often, to invoke depends on:
o How often a deadlock is likely to occur?
o How many processes will need to be rolled back?
one for each disjoint cycle
If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we
would not be able to tell which of the many deadlocked processes caused the deadlock.
Process Termination:
In order to eliminate the deadlock by aborting a process, two methods can be utilized.
a. Abort all deadlocked processes:
This method clearly will break the deadlock cycle but at great expense, since these processes
may have computed for a long period of time.
b. Abort one process at a time until the deadlock cycle is eliminated:
This method is considered as overhead as detection algorithm must be invoked after each
process is aborted to determine whether deadlock is still exists.
c. In which order should we choose to abort?
a. Priority of the process.
b. How long process has computed, and how much longer to completion.
c. Resources the process has used.
d. Resources process needs to complete.
e. How many processes will need to be terminated?
f. Is process interactive or batch?
Resource Pre-emption:
We may pre-empt some of the resources for recovery from the deadlock state and allocate these resource to
other processes until the deadlock cycle is broken.
If pre-emption is required to deal with deadlocks, then 3 issues need to be addressed.
a. Selecting a Victim:
The selection of which resource and which processes are to be pre-empted, depends on cost
factors.
b. Rollback:
The simplest solution of resource pre-emption is total rollback, abort the process and then
restart it. However it is more effective to rollback. The process only as far as necessary to break the
deadlock. This method however, requires the system to keep more information about the state of all the
running processes.
c. Starvation:
Sometimes if selection of process is dependent upon the cost factor, then same process may be
selected again and again. This process never completes its designated task. This situation is called as
Starvation. So process can be picked up victim only for finite number of times.