Chapter 1 Deadlock and Starvation-Sum-W1
Chapter 1 Deadlock and Starvation-Sum-W1
Chapter 6
7
Agenda
• Principles of Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• An Integrated Deadlock Strategy
• Dining Philosophers Problem
• Linux Kernel Concurrency
Mechanisms
8
Deadlock
• Permanent blocking of a set of processes
that either compete for system resources or
communicate with each other
• No efficient solution
• Involve conflicting needs for resources by
two or more processes
9
• A common example is the traffic deadlock.
10
11
• In particular, if all four cars wish to go straight
through the intersection, the resource
requirements are as follows:
• Car 1, traveling north, needs quadrants a and
b.
• Car 2 needs quadrants b and c.
• Car 3 needs quadrants c and d.
• Car 4 needs quadrants d and a.
12
• Joint progress diagram , illustrates the
progress of two processes competing for
two resources. Each process needs exclusive
use of both resources for a certain period of
time.
• Two processes, P and Q, have the following
general form:
13
14
• The figure shows six different execution paths.
These can be summarized as follows:
15
• The gray-shaded area of Figure which can be
referred to as a fatal region , applies to the
commentary on paths 3 and 4.
•
• If an execution path enters this fatal region, then
deadlock is inevitable. Note that the existence of a
fatal region depends on the logic of the two
processes.
16
• Whether or not deadlock occurs depends on both
the dynamics of the execution and on the details
of the application.
17
This situation is reflected in this Figure.
Some thought should convince you that regardless of the relative timing of
the two processes, deadlock cannot occur.
18
19
Reusable Resources
• Used by only one process at a time and not
depleted by that use
• Processes obtain resources that they later
release for reuse by other processes
• Examples of reusable resources are Processors,
I/O channels, main and secondary memory,
devices, and data structures such as files,
databases, and semaphores
• Deadlock occurs if each process holds one
resource and requests the other
20
As an example of deadlock involving reusable resources,
consider two processes that compete for exclusive access to a
disk file D and a tape drive T. One strategy for dealing with such a deadlock is to
impose system design constraints concerning the order in which resources
can be
requested.
P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;
P1 P2
... ...
Receive(P2); Receive(P1);
... ...
Send(P2, M1); Send(P1, M2);
25
26
Resource Allocation Graphs
• Directed graph that depicts a state of the
system of resources and processes
27
Within a resource node, a
dot is shown for each
instance of that resource.
28
-------- 29
Agenda
Principles of Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• An Integrated Deadlock Strategy
• Dining Philosophers Problem
• Linux Kernel Concurrency
Mechanisms
30
Conditions for Deadlock
• Mutual exclusion
– Only one process may use a resource at a time
• Hold-and-wait
– A process may hold allocated resources while
awaiting assignment of others
• No preemption
– No resource can be forcibly removed form a
process holding it
31
• 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
32
Three general approaches exist for dealing with deadlock.
First, one can prevent deadlock by adopting a policy that eliminates one
of the conditions (conditions 1 through 4).
35
Deadlock Prevention
• No Preemption
– Process must release resource and request
again
– Operating system may preempt a process to
require it releases its resources
• Circular Wait
– The circular-wait condition can be prevented by
Defineing a linear ordering of resource types.
(If a process has been allocated resources of type R, then it may
subsequently request only those resources of types following R in
the ordering.) 36
Deadlock Prevention
37
38
Deadlock Avoidance
• In deadlock prevention , we constrain resource
requests to prevent at least one of the four
conditions of deadlock.
41
Resource Allocation Denial
• The strategy of resource allocation denial, referred
to as the banker’s algorithm.
43
safe.
Consider a system of n processes and m different types
of resources.
Let us define the following vectors and matrices:
44
The following relationships hold:
45
• The following example illustrates these concepts.
Next Figure-a shows the state of a system
consisting of four processes P and three resources
R.
48
Determination of a Safe State
P1 Runs to Completion
49
Determination of a Safe State
P3 Runs to Completion
50
Determination of an Unsafe State
Suppose that P1 makes the request for one additional unit each of
R1 and R3; if we assume that the request is granted, we are left in
the state of Figure b.
51
Is this a safe state?
The answer is no, because each process will need at least one additional
unit of R1, and there are none available.
52
Deadlock Avoidance Logic
53
Deadlock Avoidance Logic
54
Agenda
Principles of Deadlock
Deadlock Prevention
Deadlock Avoidance
• Deadlock Detection
• An Integrated Deadlock Strategy
• Dining Philosophers Problem
• Linux Kernel Concurrency
Mechanisms
55
Deadlock Detection
•Deadlock prevention strategies are very
conservative; they solve the problem of deadlock by
limiting access to resources and by imposing
restrictions on processes.
56
Deadlock Detection
•With deadlock detection, requested resources are
granted to processes whenever possible.
57
A common algorithm for deadlock detection
59
The strategy in this algorithm is to find a process
whose resource requests can be satisfied with the
available resources, and then assume that those
resources are granted and that the process runs to
completion and releases all of its resources.
62
Selection Criteria Deadlocked Processes for
strategies 3 and 4
63
64
Strengths and Weaknesses of the Strategies
65
An Integrated Deadlock Strategy
Rather than attempting to design an OS facility that
employs only one of these strategies, it might be
more efficient to use different strategies in different
situations.
66
67
Example: Dining Philosophers
• Problem statement:
1. Five philosophers sit around a circular table.
2. Each leads a simple life alternating between
thinking and eating rice (spaghetti).
3. In front of each philosopher is a bowl of rice that
is constantly replenished by a dedicated wait staff.
4. There are exactly five chopsticks on the table,
one between each adjacent pair of philosophers.
5. Eating rice (in the most proper manner) requires
that a philosopher use both adjacent chopsticks
(forks) (simultaneously).
Develop a concurrent program free of deadlock
and imprecise delay that models the activities of
the philosophers.
68
Dining Philosophers Problem
69
Dining Philosophers Problem
70
Dining Philosophers Problem
71
Dining Philosophers Problem
72
Dining Philosophers Problem
73
Linux Kernel Concurrency Mechanisms
Linux includes all of the concurrency mechanisms
found in other UNIX systems, such as SVR4,
including pipes, messages, shared memory, and
signals.
•Sheet -1
75
Sheet - 1
Review Questions
6.2 What are the three conditions that must be present for deadlock to be possible?
6.5 List two ways in which the no-preemption condition can be prevented.
6.7 What is the difference among deadlock avoidance, detection, and prevention?
76
Problems
6.1 Show that the four conditions of deadlock apply to Figure 6.1a .
6.2 Show how each of the techniques of prevention, avoidance, and detection can be
applied to Figure 6.1 .
6.3 For Figure 6.3 , provide a narrative description of each of the six depicted paths,
similar to the description of the paths of Figure 6.2 provided in Section 6.1 .
77
78
=======