Rrit Os M3
Rrit Os M3
MODULE 3
SYNCHRONIZATION
What is synchronization?
Synchronization is the method which ensures the orderly execution of cooperating processes that
share logical address space (ie: code and data) or share data through files or messages through
threads so that data consistency is maintained.
The concurrent-access to shared-data may result in data-inconsistency. To maintain data-
consistency: the orderly execution of co-operating processes is necessary.
Suppose that we wanted to provide a solution to producer-consumer problem that fills
all full buffers. We can do so by having a variable counter that keeps track of the no. of
full buffers
Initially, counter=0.
counter is incremented by the producer after it produces a new item to buffer.
counter is decremented by the consumer after it consumes an item from buffer.
Shared-data:
Consider this execution interleaving with counter = 5 initially: The value of counter may be either
4 or 6, where the correct result should be 5. This is an example for race condition. To prevent
race conditions, concurrent-processes must be synchronized.
CRITICAL-SECTION PROBLEM
What is critical section? Explain the requirements to be satisfied for critical section problem
Critical-section is a segment-of-code in which a process may be changing common (shared)
variables or updating a table or writing a file. Each process has a critical-section in which the
shared-data is accessed.
General structure of a typical process has following:
1) Entry-section
2) Critical-section
Mutually exclusive in time i.e. no other process can execute in its critical-section.
3) Exit-section
4) Remainder-section
wish to enter their critical section, then the selection of the processes that will enter the critical
section next cannot be postponed indefinitely.
3. Bounded Waiting: There must be a bound 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 the request is granted.
Illustrate with examples the Peterson’s solution for critical section problem and prove that
mutual exclusion property is preserved.
OR
Discuss an efficient algorithm which can meet all the requirements to solve the critical
section problem.
Where variable turn indicates whose turn is to enter its critical-section. i.e., if turn== i, then
process Pi is allowed to execute in its critical-section. The flag array is used to indicate if a
process is ready (interested) to enter its critical-section. i.e. if flag[i]=true, then Pi is ready to enter
its critical-section.
while (true)
{
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
}
To prove Mutual exclusion property, let us consider two processes Pi = P0 and Pj = P1: we note
that process Pi (P0) enters its critical section only if either flag[j] = = flag[1]= = false or turn = = i
= 0 ( P1 is not interested or next turn to enter CS is P0)
If both processes can be executing in their critical sections at the same time, then flag[0] = =
flag[1] = = true.
These two observations imply that P0 and P1 could not have successfully executed their while
statements at about the same time, since the value of turn can be either 0 or 1 but cannot be both.
Hence one of the process say Pi (P0) must have successfully executed the while statement and
enters into critical section. Whereas Pj (P1) has to execute at least one additional statement turn =
= i (0). However, since at that time, falg[i] = flag[0] =true, and turn = = i (0), this condition(P1 is
in trap state) will persist as long as P0 is in critical section. Thus Mutual Exclusion is preserved.
To prove Progress and bounded wait property, let us consider two processes Pi = P0 and Pj = P1
TestAndSet( ): instruction is used to test & modify the content of a word atomically. An atomic-
operation is an operation that completes in its entirety without interruption.
The definition of TestAndSet( ) is:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
Critical Section
// remainder section
}
Suppose P0 is the process interested to enter CS. Initially lock =false, so that while(TestAndSet
(&lock )) statement results in false. P 0 enters CS. When P0 is inside CS, P1 attempts to enter CS,
but P1 is blocked in the entry section of CS itself, since the value of lock = true.
SWAP( )
Definition of Swap( ) is as follows:
void swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
This instruction is executed atomically. If the machine supports the Swap(), then mutual-
Advantages of semaphore
What are the advantages of semaphore?
1. Semaphores allow only one process into the critical section.
2. Semaphores follow mutual exclusion principle strictly.
3. Semaphores are much more efficient than some other methods of synchronization
SEMAPHORE IMPLEMENTATION
The main disadvantage of semaphore is Busy waiting.
What is busy waiting in critical section concept?
OR
What is spinlock?
While a process is in its critical-section, any other process that tries to enter its critical-
section must loop continuously in the entry-code, and this situation in critical section
problem is called busy waiting.
Busy waiting wastes CPU cycles that some other process might be able to useproductively.
This type of semaphore is also called a spinlock (because the process "spins" while waiting
for the lock).
IMPLEMENTATION OF SEMAPHORE
Explain implementation of semaphore
To overcome busy waiting, we can modify the definition of the wait() and signal() as follows:
When a process executes the wait() and finds that the semaphore-value is not positive, it must
wait. However, rather than engaging in busy waiting, the process can blockitself.
A process that is blocked (waiting on a semaphore S) should be restarted when someother process
executes a signal(). The process is restarted by awakeup().
We assume 2 simple operations:
1) block() suspends the process that invokes it.
Implementation of wait( )
wait (S)
{
value--;
if (value <0)
{
add this process to waiting queue
block();
}
}
Implementation of signal( )
signal(S)
{
value++;
if (value <=0)
{
Remove a process P from the waiting queue
wakeup(P);
}
}
NOTE:
The (critical-section) problem can be solved in two ways:
1) In a uni-processor environment
Suppose that Po executes wait(S) and then P1 executes wait(Q). When Po executes wait(Q), it
must wait until P1 executes signal(Q). Similarly, when P1 executes wait(S), it must wait until Po
executes signal(S). Since these signal() operations cannot be executed, Po & P1 are deadlocked.
Starvation (indefinite blocking) is another problem related to deadlocks.
Starvation is a situation in which processes wait indefinitely within the semaphore. Indefinite
blocking may occur if we remove processes from the list associated with a semaphore in LIFO
(last-in, first-out)order.
BOUNDED-BUFFER PROBLEM
Give a solution to the bounded buffer problem using semaphores. Write the structure of
producer and consumer processes
The bounded-buffer problem is related to the producer consumer problem. There is a pool of n
buffers, each capable of holding one item.
Shared data:
The mutex semaphores provide mutual exclusion for access to the buffer pool and is
initialized to the value 1
empty and full semaphores are used to count the number of empty and full items in buffer
respectively.
Initially empty = n; full=0
The symmetry between the producer and the consumer is ensured by the producer producing full
items in buffers for the consumer and the consumer produces empty buffers for the producer.
The structure of the producer process
while (true)
{
// produce an item
wait (empty);
wait (mutex);
// add the item to the buffer
signal (mutex);
signal (full);
}
The structure of the consumer process
while (true)
{
wait (full);
wait (mutex);
// remove an item from buffer
signal (mutex);
signal (empty);
// consume the removed item
}
The actual problem in Readers Writers problem is, if 2 readers can access the shared-DB
simultaneously without any problems. However, if a writer & other process (either a reader or a
writer) access the shared-DB simultaneously, problems may arise.
Solution: The writers must have exclusive access to the shared-DB while writing to the DB.
Shared-data
Where,
mutex is used to ensure mutual-exclusion when the variable readcount is updated.
wrt is common to both reader and writer processes.
wrt is used as a mutual-exclusion semaphore for the writers. Also wrt is used by the
first/last reader that enters/exits the critical-section.
readcount counts the number of processes currently reading the object.
Initialization
mutex = 1, wrt = 1, readcount = 0
The structure of a writer process
while (true)
{
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
The table has a bowl of rice in the center and 5 single chopsticks.
From time to time, a philosopher gets hungry and tries to pick up the 2
Obviously, one cannot pick up a chopstick that is already in the hand of a neighbor
philosopher.
When hungry philosopher has both her chopsticks at the same time, she eats
again.
Problem objective: To allocate several resources among several processes in a deadlock-free &
starvation-free manner.
Solution using semaphore:
Represent each chopstick with a semaphore; chopstick[5]
The philosopher releases her chopsticks by executing the signal( ) on the semaphores.
Shared-data:
semaphore chopstick[5];
Initialization
chopstick[5]={1,1,1,1,1}
2) Allow a philosopher to pick up her chopsticks only if both chopsticks are available.
3) Use an asymmetric solution; i.e. an odd philosopher picks up first her left
chopstick and then her right chopstick, whereas an even philosopher picks up her
right chopstick and then her left chopstick.
2.1 Deadlocks
2.2 System Model
2.3 Deadlock Characterization
2.3.1 Necessary Conditions
2.3.2 Resource Allocation Graph
2.4 Methods for Handling Deadlocks
2.5 Deadlock Prevention
2.5.1 Mutual Exclusion
2.5.2 Hold and Wait
2.5.3 No Preemption
2.5.4 Circular Wait
2.6 Deadlock Avoidance
2.6.1 Safe State
2.6.2 Resource Allocation Graph Algorithm
2.6.3 Banker's Algorithm
2.6.3.1 Safety Algorithm
2.6.3.2 Resource Request Algorithm
3.6 3.3 An Illustrative Example
MODULE 3: DEADLOCKS
3.1 Deadlocks
• Deadlock is a situation where a set of processes are blocked because each process is
→ holding a resource and
→ waiting for another resource held by some other process.
• Real life example:
When 2 trains are coming toward each other on same track and there is only one track, none of
the trains can move once they are in front of each other.
• Similar situation occurs in operating systems when there are two or more processes hold some
resources and wait for resources held by other(s).
• Here is an example of a situation where deadlock can occur (Figure 3.1).
3-2
OPERATING SYSTEMS
3.3 Deadlock Characterization
• In a deadlock, processes never finish executing, and system resources are tied up, preventing other
jobs from starting.
3-3
OPERATING SYSTEMS
3.3.2 Resource-Allocation-Graph
• The resource-allocation-graph (RAG) is a directed graph that can be used to describe the deadlock
situation.
• RAG consists of a
→ set of vertices (V) and
→ set of edges (E).
• V is divided into two types of nodes
1) P={P1,P2 ........ Pn} i.e., set consisting of all active processes in the system.
2) R={R1,R2 ......... Rn} i.e., set consisting of all resource types in the system.
• E is divided into two types of edges:
1) Request Edge
A directed-edge Pi → Rj is called a request edge.
Pi → Rj indicates that process Pi has requested a resource Rj.
2) Assignment Edge
A directed-edge Rj → Pi is called an assignment edge.
Rj → Pi indicates that a resource Rj has been allocated to process Pi.
• Suppose that process Pi requests resource Rj.
Here, the request for Rj from Pi can be granted only if the converting request-edge to
assignment-edge do not form a cycle in the resource-allocation graph.
• Pictorially,
→ We represent each process Pi as a circle.
→ We represent each resource-type Rj as a rectangle.
• As shown in below figures, the RAG illustrates the following 3 situation (Figure 3.3):
1) RAG with a deadlock
2) RAG with a cycle and deadlock
3) RAG with a cycle but no deadlock
(a) Resource allocation Graph (b) With a deadlock (c) with cycle but no deadlock
Figure 3.3 Resource allocation graphs
Conclusion:
1) If a graph contains no cycles, then the system is not deadlocked.
2) If the graph contains a cycle then a deadlock may exist.
Therefore, a cycle means deadlock is possible, but not necessarily present.
3-4
OPERATING SYSTEMS
3.4 Methods for Handling Deadlocks
• There are three ways of handling deadlocks:
1) Deadlock prevention or avoidance - Do not allow the system to get into a deadlocked state.
2) Deadlock detection and recovery - Abort a process or preempt some resources when
deadlocks are detected.
3) Ignore the problem all together - If deadlocks only occur once a year or so, it may be better
to simply let them happen and reboot the system.
• In order to avoid deadlocks, the system must have additional information about all processes.
• In particular, the system must know what resources a process will or may request in the future.
• Deadlock detection is fairly straightforward, but deadlock recovery requires either aborting processes
or preempting resources.
• If deadlocks are neither prevented nor detected, then when a deadlock occurs the system will
gradually slow down.
3.5 Deadlock-Prevention
• Deadlocks can be eliminated by preventing at least one of the four required conditions:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption
4) Circular-wait.
3-5
OPERATING SYSTEMS
3.5.3 No Preemption
• To prevent this condition: the resources must be preempted.
• There are several solutions to this problem.
Protocol-1
• If a process is holding some resources and requests another resource that cannot be immediately
allocated to it, then all resources currently being held are preempted.
• The preempted resources are added to the list of resources for which the process is waiting.
• The process will be restarted only when it regains the old resources and the new resources that it is
requesting.
Protocol-2
• When a process request resources, we check whether they are available or not.
If (resources are available)
then
{
allocate resources to the process
}
else
{
If (resources are allocated to waiting process)
then
{
preempt the resources from the waiting process
allocate the resources to the requesting-process
the requesting-process must wait
}
}
• These 2 protocols may be applicable for resources whose states are easily saved and restored, such
as registers and memory.
• But, these 2 protocols are generally not applicable to other devices such as printers and tape drives.
3.5.4 Circular-Wait
• Deadlock can be prevented by using the following 2 protocol:
Protocol-1
Assign numbers all resources.
Require the processes to request resources only in increasing/decreasing order.
Protocol-2
Require that whenever a process requests a resource, it has released resources with a lower
number.
• One big challenge in this scheme is determining the relative ordering of the different resources.
3-6
OPERATING SYSTEMS
3.6 Deadlock Avoidance
• The general idea behind deadlock avoidance is to prevent deadlocks from ever happening.
• Deadlock-avoidance algorithm
→ requires more information about each process, and
→ tends to lead to low device utilization.
• For example:
1) In simple algorithms, the scheduler only needs to know the maximum number of each
resource that a process might potentially use.
2) In complex algorithms, the scheduler can also take advantage of the schedule of exactly
what resources may be needed in what order.
• A deadlock-avoidance algorithm dynamically examines the resources allocation state to ensure that a
circular-wait condition never exists.
• The resource-allocation state is defined by
→ the number of available and allocated resources and
→ the maximum demand of each process.
3-7
OPERATING SYSTEMS
3.6.2 Resource-Allocation-Graph Algorithm
• If resource categories have only single instances of their resources, then deadlock states can be
detected by cycles in the resource-allocation graphs.
• In this case, unsafe states can be recognized and avoided by augmenting the resource-allocation
graph with claim edges (denoted by a dashed line).
• Claim edge Pi → Rj indicated that process Pi may request resource Rj at some time in future.
• The important steps are as below:
1) When a process Pi requests a resource Rj, the claim edge Pi → Rj is converted to a request
edge.
2) Similarly, when a resource Rj is released by the process Pi, the assignment edge Rj → Pi is
reconverted as claim edge Pi → Rj.
3) The request for Rj from Pi can be granted only if the converting request edge to assignment
edge do not form a cycle in the resource allocation graph.
• To apply this algorithm, each process Pi must know all its claims before it starts executing.
• Conclusion:
1) If no cycle exists, then the allocation of the resource will leave the system in a safe state.
2) If cycle is found, system is put into unsafe state and may cause a deadlock.
• For example: Consider a resource allocation graph shown in Figure 3.5(a).
Suppose P2 requests R2.
Though R2 is currently free, we cannot allocate it to P2 as this action will create a cycle in the
graph as shown in Figure 3.5(b).
This cycle will indicate that the system is in unsafe state: because, if P1 requests R2 and P2
requests R1 later, a deadlock will occur.
• Problem:
The resource-allocation graph algorithm is not applicable when there are multiple instances
for each resource.
• Solution:
Use banker's algorithm.
3-8
OPERATING SYSTEMS
3.6.3 Banker's Algorithm
• This algorithm is applicable to the system with multiple instances of each resource types.
• However, this algorithm is less efficient then the resource-allocation-graph algorithm.
• When a process starts up, it must declare the maximum number of resources that it may need.
• This number may not exceed the total number of resources in the system.
• When a request is made, the system determines whether granting the request would leave the
system in a safe state.
• If the system in a safe state,
the resources are allocated;
else
the process must wait until some other process releases enough resources.
• Assumptions:
Let n = number of processes in the system
Let m = number of resources types.
• Following data structures are used to implement the banker’s algorithm.
1) Available [m]
This vector indicates the no. of available resources of each type.
If Available[j]=k, then k instances of resource type Rj is available.
2) Max [n][m]
This matrix indicates the maximum demand of each process of each resource.
If Max[i,j]=k, then process Pi may request at most k instances of resource type Rj.
3) Allocation [n][m]
This matrix indicates no. of resources currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
4) Need [n][m]
This matrix indicates the remaining resources need of each process.
If Need[i,j]=k, then Pi may need k more instances of resource Rj to complete its task.
So, Need[i,j] = Max[i,j] - Allocation[i]
• The Banker’s algorithm has two parts: 1) Safety Algorithm
2) Resource – Request Algorithm
Step 1:
Let Work and Finish be two vectors of length m and n respectively.
Initialize:
Work = Available
Finish[i] = false for i=1,2,3,…….n
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Need i <= Work.
If no such i exist, then go to step 4
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2
Step 4:
If Finish[i] = true for all i, then the system is in safe state.
3-9
OPERATING SYSTEMS
3.6.3.2 Resource-Request Algorithm
• This algorithm determines if a new request is safe, and grants it only if it is safe to do so.
• When a request is made ( that does not exceed currently available resources ), pretend it has been
granted, and then see if the resulting state is a safe one. If so, grant the request, and if not, deny the
request.
• Let Request(i) be the request vector of process Pi.
• If Request(i)[j]=k, then process Pi wants K instances of the resource type Rj.
Step 1:
If Request(i) <= Need(i)
then
go to step 2
else
raise an error condition, since the process has exceeded its maximum claim.
Step 2:
If Request(i) <= Available
then
go to step 3
else
Pi must wait, since the resources are not available.
Step 3:
If the system want to allocate the requested resources to process Pi then modify the
state as follows:
Available = Available – Request(i)
Allocation(i) = Allocation(i) + Request(i)
Need(i) = Need(i) – Request(i)
Step 4:
If the resulting resource-allocation state is safe,
then i) transaction is complete and
ii) Pi is allocated its resources.
Step 5:
If the new state is unsafe,
then i) Pi must wait for Request(i) and
ii) old resource-allocation state is restored.
3-10
OPERATING SYSTEMS
3.6.3.3 An Illustrative Example
Question: Consider the following snapshot of a system:
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 3 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =3 3 2
……P0………P1……..P2……..P3……P4…..
Finish = | false | false | false | false | false |
3-11
OPERATING SYSTEMS
3-12
OPERATING SYSTEMS
Step 2: For i=0
Finish[P0] = false and Need[P0]<=Work i.e. (7 4 3)<=(2 3 0) false
So P0 must wait.
3-13
OPERATING SYSTEMS
3.7 Deadlock Detection
• If a system does not use either deadlock-prevention or deadlock-avoidance algorithm then a
deadlock may occur.
• In this environment, the system must provide
1) An algorithm to examine the system-state to determine whether a deadlock has occurred.
2) An algorithm to recover from the deadlock.
• A deadlock exists in the system if and only if the wait-for-graph contains a cycle.
• To detect deadlocks, the system needs to
→ maintain the wait-for-graph and
→ periodically execute an algorithm that searches for a cycle in the graph.
3-14
OPERATING SYSTEMS
3.7.2 Several Instances of a Resource Type
• The wait-for-graph is applicable to only a single instance of a resource type.
• Problem: However, the wait-for-graph is not applicable to a multiple instance of a resource type.
• Solution: The following detection-algorithm can be used for a multiple instance of a resource type.
• Assumptions:
Let ‘n’ be the number of processes in the system
Let ‘m’ be the number of resources types.
• Following data structures are used to implement this algorithm.
1) Available [m]
This vector indicates the no. of available resources of each type.
If Available[j]=k, then k instances of resource type Rj is available.
2) Allocation [n][m]
This matrix indicates no. of resources currently allocated to each process.
If Allocation[i,j]=k, then Pi is currently allocated k instances of Rj.
3) Request [n][m]
This matrix indicates the current request of each process.
If Request [i, j] = k, then process Pi is requesting k more instances of resource type Rj.
Step 1:
Let Work and Finish be vectors of length m and n respectively.
a) Initialize Work = Available
b) For i=0,1,2 ......... n
if Allocation(i) != 0
then
Finish[i] = false;
else
Finish[i] = true;
Step 2:
Find an index(i) such that both
a) Finish[i] = false
b) Request(i) <= Work.
If no such i exist, goto step 4.
Step 3:
Set:
Work = Work + Allocation(i)
Finish[i] = true
Go to step 2.
Step 4:
If Finish[i] = false for some i where 0 < i < n, then the system is in a deadlock state.
3-15
OPERATING SYSTEMS
3.8 Recovery from deadlock
• Three approaches to recovery from deadlock:
1) Inform the system-operator for manual intervention.
2) Terminate one or more deadlocked-processes.
3) Preempt(or Block) some resources.
3-16
OPERATING SYSTEMS
Exercise Problems
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 0 0 2
P1 1 0 1
P2 0 0 2
P3 2 1 0
P4 0 1 4
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =1 0 2
……P0………P1……..P2……..P3……P4…..
Finish = | false | false | false | false | false |
3-17
OPERATING SYSTEMS
3-18
OPERATING SYSTEMS
3-19
2) For the following snapshot, find the safe sequence using Banker's algorithm:
The number of resource units is (A, B, C) which are (7, 7, 10) respectively.
Allocation Max Available
A B C A B C A B C
P1 2 2 3 3 6 8 7 7 10
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4
Solution:
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
•
A B C
P1 1 4 5
P2 2 3 0
P3 2 2 0
Applying the Safety algorithm on the given
system, Step 1: Initialization
Here, m=3, n=3
Work = Available i.e. Work =7 7 10
….P1………..P2 ........ P3…
Finish = | false | false | false |
Step 2: For i=1
Finish[P1] = false and Need[P1]<=Work i.e. (1 4 5)<=(7 7 10) true
So P1 must be kept in safe sequence.
Step 3: Work = Work + Allocation[P1] =(7 7 10)+(2 2 3)=(9 9 13)
……P1……P2……….P3….
Finish = | true | false | false |
3-20
3) Consider the following snapshot of resource-allocation at time t1.
Allocation Max Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
i) What is the content of the matrix need?
ii) Show that the system is not deadlock by generating one safe sequence
iii) At instance t, P2 makes one additional for instance of type C. Show that the system is deadlocked
if the request is granted. Write down deadlocked-processes.
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C
P0 0 0 0
P1 0 0 2
P2 0 0 0
P3 0 0 0
P4 0 0 0
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =0 0 0
……P0………P1…….P2.......... P3……P4…
Finish = | false | false | false | false | false |
3-21
Step 2: For i=4
Finish[P4] = false and Need[P4]<=Work i.e. (0 0 0)<=(5 2 4) true
So P4 must be kept in safe sequence.
Step 3: Work = Work + Allocation[P4] =(5 2 4)+(0 0 2)=(5 2 6)
....P0……P1…….P2…….P3…….P4….
Finish = | true | false | true | true | true |
3-22
4) For the given snapshot :
Solution (i):
• The content of the matrix Need is given by
Need = Max - Allocation
• So, the content of Need Matrix is:
Need
A B C D
P1 0 0 0 0
P2 0 7 5 2
P3 1 0 0 2
P4 0 0 2 0
P5 0 6 4 2
Solution (ii):
• Applying the Safety algorithm on the given system,
Step 1: Initialization
Work = Available i.e. Work =1 5 2 0
....P1………P2…….P3……….P4…..P5…..
Finish = | false | false | false | false | false |
3-23
Step 2: For i=5
Finish[P5] = false and Need[P5]<=Work i.e. (0 6 4 2)<=(2 14 11 8) true
So P5 must be kept in safe sequence.
Step 3: Work = Work + Allocation[P5] =(2 14 11 8)+(0 0 1 4)=(2 14 12 12)
....P1………P2…….P3…….P4……P5…
Finish = | true | false | true | true | true |
3-24
Step 3: Work = Work + Allocation[P1] =(1 1 0 0)+(0 0 1 2)=(1 1 1 2)
....P1………P2…….P3……...P4 ........ P5…
Finish = | true | false | false | false | false |
3-25
5) Consider a system containing ‘m’ resources of the same type being shared by ‘n’ processes.
Resources can be requested and released by processes only one at a time. Show that the system is
deadlock free if the following two conditions hold:
i) The maximum need of each process is between 1 and m resources
ii) The sum of all maximum needs is less than m+n.
Ans:
• Suppose N = Sum of all Needi
A = Sum of all Allocationi
M = Sum of all Maxi.
• Use contradiction to prove: Assume this system is not deadlock free.
• If there exists a deadlock state, then A=m because there's only one kind of resource and resources
can be requested and released only one at a time.
• From condition (ii), N+A = M<m+n
• So we get N+m <m +n.
• So we get N < n.
• It shows that at least one process i that Needi=0.
• From condition (i), Pi can release at least one resource.
• So, there are n-1 processes sharing ‘m’ resources now, condition (i) and (ii) still hold.
• Go on the argument, no process will wait permanently, so there's no deadlock.
6) Consider the traffic deadlock depicted in the figure given below, explain that the four necessary
conditions for dead lock indeed hold in this examples.
Ans:
• The four necessary conditions for a deadlock are:
1) Mutual exclusion
2) Hold-and-wait
3) No preemption and
4) Circular-wait.
• The mutual exclusion condition holds since only one car can occupy a space in the roadway.
• Hold-and-wait occurs where a car holds onto its place in the roadway while it waits to advance in the
roadway.
• A car cannot be removed (i.e. preempted) from its position in the roadway.
• Lastly, there is indeed a circular-wait as each car is waiting for a subsequent car to advance.
• The circular-wait condition is also easily observed from the graphic.
3-26