0% found this document useful (0 votes)
72 views17 pages

06 Deadlock and Starvation 1

The document discusses the concepts of deadlock and starvation in operating systems, defining deadlock as a permanent blocking of processes competing for resources. It details various approaches to deadlock detection, prevention, and avoidance, including the use of resource allocation graphs and the banker’s algorithm. Additionally, it presents the dining philosophers problem as a classic example of synchronization issues that can lead to deadlock and starvation, along with potential solutions.

Uploaded by

73iibrahim45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views17 pages

06 Deadlock and Starvation 1

The document discusses the concepts of deadlock and starvation in operating systems, defining deadlock as a permanent blocking of processes competing for resources. It details various approaches to deadlock detection, prevention, and avoidance, including the use of resource allocation graphs and the banker’s algorithm. Additionally, it presents the dining philosophers problem as a classic example of synchronization issues that can lead to deadlock and starvation, along with potential solutions.

Uploaded by

73iibrahim45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEADLOCK

AND STARVATION

Lecture 5
Dr. Subhi A.Rahim
Bahudaila
OS

PRINCIPLES OF DEADLOCK
2 Lecture 5

Deadlock can be defined as the permanent blocking of a set of


processes that either compete for system resources or
communicate with each other. Deadlock is permanent because
none of the events is ever triggered.

All deadlocks involve conflicting needs for resources by two or


more processes.
The Traffic Deadlock Example
3 Lecture 5
• Car 1 needs quadrants a and b.
If all four cars wish to go straight • Car 2 needs quadrants b and c.
through the intersection, the resource • Car 3 needs quadrants c and d.
requirements are as follows: • Car 4 needs quadrants d and a.

Resource Allocation Graphs


4 Lecture 5

A useful tool in characterizing the allocation of resources to processes is the


resource allocation graph. Examples of Resource Allocation Graphs:
 Resource Allocation Graphs
5 Lecture 5

Circular wait: a closed chain of processes exists, such that each process
holds at least one resource needed by the next process in the chain.

 The resource allocation graph of Traffic


Deadlock
6 Lecture 5
Deadlock Detection, Prevention, and
Avoidance Approaches for Operating Systems
7 Lecture 5

The table summarizes the key elements of the most important approaches
that have been developed for OS: prevention, avoidance, and detection.

Deadlock Detection, Prevention, and


Avoidance Approaches for Operating Systems
8 Lecture 5
Deadlock Avoidance
9 Lecture 5

Deadlock avoidance makes judicious choices to assure that the


deadlock point is never reached, thus requires knowledge of
future process resource requests.

 Process Initiation Denial:


Do not start a process if its initiation leads to a possible deadlock,
suppose a system with n processes and m resources.

 Deadlock Avoidance
10 Lecture 5
 Deadlock Avoidance
11 Lecture 5

The following conditions can be holding:


for all i: all resources available or
allocated (non available)

for all k, i: No process can claim more that


what is available

for all k, i: No process is allocated more resources


more than its original claim

With these condition a process is only started if the following condition


is satisfied: Which essentially means that a process
can only be started if the maximum claim
of all current processes and that of the
new process can be satisfied.

 Deadlock Avoidance
12 Lecture 5

The strategy of resource allocation denial, referred to as the


banker’s algorithm.

 Multiple instances

 Each process must a priori claim maximum use

 When a process requests a resource it may have to wait

 When a process gets all its resources it must return them in a


finite amount of time
→ Deadlock Avoidance
13 Lecture 5

 The state of the system reflects the current allocation of resources to


processes. Thus, the state consists of the two vectors, Resource and
Available, and the two matrices, Claim and Allocation.
 A safe state means that all of the processes can be run to completion.
 An unsafe state is a state that is not safe.

 Determination of a Safe State


14 Lecture 5
 Determination of a Safe State
15 Lecture 5

 Determination of a Safe State


16 Lecture 5
→ Deadlock Avoidance
Data Structures for the Banker’s Algorithm
17 Lecture 5

Let n = number of processes, and m = number of resources


types.
 Available: Vector of length m. If available [j] = k, there are k instances
of resource type Rj available

 Max: n x m matrix. If Max [i,j] = k, then process Pi may request at


most k instances of resource type Rj

 Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently


allocated k instances of Rj

 Need: n x m matrix. If Need[i,j] = k, then Pi may need k more


instances of Rj to complete its task
 Need [i,j] = Max[i,j] – Allocation [i,j]
→ Deadlock Avoidance
Safety Algorithm
18 Lecture 5

1. Let Work and Finish be vectors of length m and


n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2. Find an 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
→ Deadlock Avoidance
Resource-Request Algorithm for Process Pi
19 Lecture 5

Requesti = request vector for process Pi. If


Requesti [j] = k then process Pi wants k
instances of resource type Rj
1.If Requesti  Needi go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim
2.If Requesti  Available, go to step 3. Otherwise Pi must
wait, since resources are not available
3.Pretend to allocate requested resources to Pi by modifying
the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
 If safe  the resources are allocated to Pi
 If unsafe  Pi must wait, and the old resource-allocation state is
restored

→ Deadlock Avoidance
Example of Banker’s Algorithm
20 Lecture 5


5 processes P0 through P4;
3 resource types:
A (10 instances), B (5instances), and C (7
instances)

Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 200 322
P2 302 902
P3 211 222
P4 002 433
→ Deadlock Avoidance
Example (Cont.)
21 Lecture 5


The content of the matrix Need is defined to be Max –
Allocation

Need
ABC
P0 7 4 3
P1 122
P2 600
P3 011
P4 431


The system is in a safe state since the sequence < P1, P3,
P4, P2, P0> satisfies safety criteria

→ Deadlock Avoidance
Example: P1 Request (1,0,2)
22 Lecture 5


Check that Request  Available (that is, (1,0,2)  (3,3,2)  true
AllocationNeed Available
ABC ABC ABC
P0 0 1 0 7 4 3 2 3 0
P1 302 020
P2 3 0 2 600
P3 2 1 1 011
P4 0 0 2 431


Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement


Can request for (3,3,0) by P4 be granted?


Can request for (0,2,0) by P0 be granted?
DEADLOCK DETECTION
23 Lecture 5

 In Deadlock detection resources are allocated when ever demanded.


Periodically, o/s checks for circular wait condition using some sort of
algorithm for detection.

 Deadlock Detection Algorithm:


The frequency of detection could be as frequent as with each resource,
which has the advantage of early detection and simple strategy. But on
the other hand this frequent detection consumes processor time. Less
frequent check could be useful in terms of processor time but may have
other consequences. A more common strategy uses similar kinds of
matrices and vectors used in the previous section for deadlock
avoidance with an additional request matrix Q.

 Deadlock Detection Algorithm


24 Lecture 5

 Following are the steps with the assumption that all processes are
marked initially indicating not deadlocked:
1. Mark each process for which all the elements are zeros in the
allocation matrix.
2. Initialize a matrix W equal to the Available matrix.
3. Recursively, find an index i, such that process i is currently
unmarked and the ith row of Q is less than or equal to W meaning:

If true:

If false: Terminate
 Deadlock Detection Example
25 Lecture 5

 Let us see an example to demonstrate deadlock detection strategy:

 Deadlock Detection Example: Step 1


26 Lecture 5

 Mark P4, because P4 has no allocated resources.

Step 1
 Deadlock Detection Example: Step 2
27 Lecture 5

 set W = (0 0 0 0 1).

Step
1
Step 2

 Deadlock Detection Example: Step 3


28 Lecture 5

 The request of process P3 is less than or equal to W, so mark P3 and set


W = W + (0 0 0 1 0) = (0 0 0 1 1).

Step 3
 Deadlock Detection Example: Step 4
29 Lecture 5

 No other unmarked process has a row in Q that is less than or equal to


W. Therefore, terminate the algorithm.

P1 & P2 are
deadlocked.

DINING PHILOSOPHERS PROBLEM


30 Lecture 5

Dinning philosophers problem is very important not only in mutual exclusion


strategy to avoid deadlock and starvation but also in coordination of shared
resources. This problem is standard test for evaluating approaches to
synchronization:
 First attempt could be that each philosopher picks up the fork on his left
and then picks the right fork, after eating he puts both forks on the table.
Imagine if all the philosophers gets hungry at the same time it, they will sit
and each one of them will pick the fork on the left and hence all of them will
starve as no second fork.
 So we can spend more money and bring another set of five forks (expensive
solution).
 Why not provide training so they should be able to eat with just one fork.
 Another solution could be to have a guard posted at the door who only
allows maximum of 4 philosophers at one time, which at least ensure that
one of the philosopher will be able to eat. Which ensures deadlock and
starvation solution.
31 Lecture 5

Solutions to the Dinning Philosophers Problem


32 Lecture 5

A first solution
to the Dining
Philosophers
problem leads
to deadlock.
Solutions to the Dinning Philosophers Problem
33 Lecture 5

A Second solution to
the Dining
Philosophers problem
is free of deadlock.
That is only allows
maximum of 4
philosophers at one
time, which at least
ensure that one of the
philosopher will be
able to eat.

Exercises
34 Lecture 5

1. Apply with showing the results for all steps of the deadlock detection
algorithm for the following data:

You might also like