0% found this document useful (0 votes)
38 views72 pages

Chapter 1 Deadlock and Starvation-Sum-W1

Uploaded by

www.omarehab3710
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)
38 views72 pages

Chapter 1 Deadlock and Starvation-Sum-W1

Uploaded by

www.omarehab3710
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
You are on page 1/ 72

Concurrency:

Deadlock and Starvation

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.

• Next Figure-a shows a situation in which


four cars have arrived at a four-way stop
intersection at approximately the same time.

• The four quadrants of the intersection are


the resources over which control is needed.

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.

• However, deadlock is only inevitable if the joint


progress of the two processes creates a path that
enters the fatal region.

16
• Whether or not deadlock occurs depends on both
the dynamics of the execution and on the details
of the application.

• For example, suppose that P does not need both


resources at the same time so that the two
processes have the following form:

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.

Deadlock occurs if each process holds one resource and


requests the other. For example, deadlock occurs if the
multiprogramming system interleaves the execution of the
two processes as follows: 21
Another Example of Deadlock

• Space is available for allocation of 200Kbytes,


and the following sequence of events occur

P1 P2
... ...
Request 80 Kbytes; Request 70 Kbytes;
... ...
Request 60 Kbytes; Request 80 Kbytes;

• Deadlock occurs if both processes progress


to their second request
If the
amount of memory to be requested is not known ahead of time, it is difficult to deal
with this type of deadlock by means of system design constraints. The best way to 22
deal with this particular problem is, in effect, to eliminate the possibility by using
virtual memory, which is discussed in Chapter 8.
23
Consumable Resources
• Created (produced) and destroyed
(consumed)
• Examples of consumable resources are
Interrupts, signals, messages, and
information in I/O buffers
• Deadlock may occur if a Receive message
is blocking
• May take a rare combination of events to
cause deadlock
24
Example of Deadlock

• Deadlock occurs if receive is blocking

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.

Examples of resource types


that may have multiple
instances are I/O devices
that are allocated by a
resource management
module in the OS.

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).

Second, one can avoid deadlock by making the appropriate


dynamic choices based on the current state of resource allocation.

Third, one can attempt to detect the presence of deadlock (conditions 1


through 4 hold) and take action to recover.
Deadlock Prevention
• We can view deadlock prevention methods
as falling into two classes.
• An indirect method of deadlock prevention
is to prevent the occurrence of one of the
three necessary conditions listed previously
(items 1 through 3).
• A direct method of deadlock prevention is to
prevent the occurrence of a circular wait
(item 4).
34
Deadlock Prevention
• Mutual Exclusion
– Must be supported by the operating system

• Hold and Wait


– Require a process request all of its required
resources at one time

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.

• This is either done indirectly, by preventing one of


the three necessary policy conditions (mutual
exclusion, hold and wait, no preemption),
or directly, by preventing circular wait.

• This leads to inefficient use of resources and


inefficient execution of processes. 39
Deadlock Avoidance
• Deadlock avoidance , on the other hand, allows
the three necessary conditions but
makes wise choices to assure that the deadlock
point is never reached.

• As such, avoidance allows more concurrency than


prevention.
• With deadlock avoidance, A decision is made
dynamically whether the current resource
allocation request will, if granted, potentially lead to
a deadlock.
• Requires knowledge of future process requests
Two Approaches to Deadlock Avoidance

• Do not start a process if its demands might


lead to deadlock

• Do not grant an incremental resource


request to a process if this allocation might
lead to deadlock

41
Resource Allocation Denial
• The strategy of resource allocation denial, referred
to as the banker’s algorithm.

• Let us begin by defining the concepts of state and


safe state.
• Consider a system with a fixed number of
processes and a fixed number of resources.

• At any time a process may have zero or more


resources allocated to it.
42
• 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, defined earlier.
Imagine you have:

Three processes: P1, P2, and P3.


Ten instances of a resource, R, in total.
Now, suppose:

• A safe state is one in which there is at least one


P1 needs a maximum of 5 instances.
P2 needs a maximum of 4 instances.
P3 needs a maximum of 7 instances.
If you initially allocate resources like this:

sequence of resource allocations to processes that


P1 has 2 instances.
P2 has 2 instances.
P3 has 2 instances.
Then you still have 4 instances of R
available.

does not result in a deadlock (i.e., all of the


The system is in a safe state if there’s an
allocation sequence (like P2 P1 P3) that
allows each process to finish using the
resources available and then release

processes can be run to completion).


them. For example:

Allocate remaining resources to P2 (it


needs 2 more to complete).
P2 completes, releasing its 4 instances.
Now P1 can receive enough resources to
finish and release its resources.
Finally, P3 can finish using the resources
that are freed up.
Since this sequence exists, it shows that
all processes can eventually complete

• An unsafe state is, of course, a state that is not


without leading to a deadlock. Therefore,
the system is in a safe state.

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:

With these quantities defined, we can define a deadlock


avoidance policy that refuses to start a new process if its
resource requirements might lead to deadlock.
Start a new process Pn+1 only if

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.

• The total amount of resources R1, R2, and R3 are


9, 3, and 6 units, respectively.

• In the current state allocations have been made to


the four processes, leaving
1 unit of R2 and 1 unit of R3 available.
Is this a safe state? 46
Determination of a Safe State Initial State

To answer this question, we ask an intermediate question: Can


any of the four processes be run to completion with the
resources available?

That is, can the difference between the maximum requirement


and current allocation for any process be met with the available
resources? 47
Determination of a Safe State
P2 Runs to Completion

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

Consider the state defined in Figure a.

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.

Thus, on the basis of deadlock avoidance, the request by P1 should be


denied and P1 should be blocked

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.

•At the opposite extreme, deadlock detection


strategies do not limit resource access or restrict
process actions.

56
Deadlock Detection
•With deadlock detection, requested resources are
granted to processes whenever possible.

Periodically, the OS performs an algorithm that


allows it to detect the circular wait condition described
earlier in condition (4).

57
A common algorithm for deadlock detection

The Allocation matrix and Available vector


described in the previous part are used.
In addition, a request matrix Q is defined such that
Qij represents the amount of resources of type j
requested by process i.

The algorithm proceeds by marking processes that


are not deadlocked.
Initially, all processes are unmarked.
Then the following steps are performed:
58
•A deadlock exists if and only if there are unmarked
processes at the end of the algorithm.

•Each unmarked process is deadlocked.

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.

The algorithm then looks for another process to


satisfy.
Note that this algorithm does not guarantee to
prevent deadlock; that will depend on the order in
which future requests are granted.

All that it does is determine if deadlock currently


exists. 60
Deadlock Detection Example

We can use this Figure to illustrate the deadlock detection


algorithm.
The algorithm proceeds as follows:
1. Mark P4, because P4 has no allocated resources.
2. Set W = (0 0 0 0 1).
3. 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).
4. No other unmarked process has a row in Q that is less than or equal to W.

Therefore, terminate the algorithm.


The algorithm concludes with P1 and P2 unmarked, indicating61
that these processes are deadlocked.
Strategies once Deadlock Detected
Once deadlock has been detected, some
strategy is needed for recovery.
1. Abort all deadlocked processes
2. Back up each deadlocked process to some
previously defined checkpoint, and restart
all process
3. Successively abort deadlocked processes
until deadlock no longer exists
4. Successively preempt resources until
deadlock no longer exists

62
Selection Criteria Deadlocked Processes for
strategies 3 and 4

• Least amount of processor time consumed so far


• Least number of lines of output produced so far
• Most estimated time remaining
• Least total resources allocated so far
• Lowest priority

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.

[HOWA73] suggests one approach: page 278


7th ed.

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.

In addition, Linux 2.6 includes a rich set of


concurrency mechanisms specifically intended for use
when a thread is executing in kernel mode.

That is, these are mechanisms used within the kernel


to provide concurrency in the execution of kernel
code.
74
Task

•Examine the Linux kernel concurrency mechanisms.

•Sheet -1

75
Sheet - 1
Review Questions

6.1 Give examples of reusable and consumable resources.

6.2 What are the three conditions that must be present for deadlock to be possible?

6.3 What are the four conditions that create deadlock?

6.4 How can the hold-and-wait condition be prevented?

6.5 List two ways in which the no-preemption condition can be prevented.

6.6 How can the circular wait condition 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
=======

You might also like