Tải bản đầy đủ (.pdf) (54 trang)

Lecture Operating system principles - Chapter 6: Concurrency: Deadlock and starvation

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (541.96 KB, 54 trang )

Chapter 6
Concurrency: Deadlock and Starvation
• Principals of Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Dining Philosophers Problem

1


Deadlock
• A set of processes is deadlocked when
each process in the set is blocked awaiting
an event that can only be triggered by
another blocked process in the set
– Typically involves processes competing for
the same set of resources
– The event is typically the freeing up of some
requested resources

• No efficient solution
2


Potential Deadlock
I need
quad C
and D

I need


quad D
and A

The necessary resources are available
for any of the cars to proceed

I need
quad B and
C

I need
quad A and
B

3


Actual Deadlock
HALT until
D is free

HALT until
A is free

HALT until
C is free

HALT until
B is free


4


Two Processes P and Q
• Consider two
processes P and Q in
a uniprocessor system.
• Each needs exclusive
access to a resource A
and B for a period of
time.

5


Joint Progress
Diagram of Deadlock
Deadlock is only
inevitable if the
joint progress of
the two processes
creates a path
that enters the
fatal region.

6


Alternative logic
• Whether or not deadlock

occurs depends on both
the dynamics of the
execution and on the
details of the application.
• Suppose that P does not
need both resources at
the same time.
7


Diagram of
alternative logic

Deadlock
cannot occur

8


Resource Categories
Two general categories of resources:
• Reusable
– can be safely used by only one process at a time
and is not depleted by that use.
– examples: processors, main and secondary memory,
devices, and data structures such as files,
databases, and semaphores

• Consumable
– can be created (produced) and destroyed

(consumed)
– examples: interrupts, signals, messages, and
information in I/O buffers
9


Reusable Resources
Example
• Consider two processes that compete for
exclusive access to a disk file D and a
tape drive T.

10


Reusable Resources
Example
• Deadlock occurs if each process holds
one resource and requests the other.
• Example:
– If the multiprogramming system interleaves
the execution of the two processes as follows
p0 p1 q0 q1 p2 q2

11


Reusable Resources Example 2:
Memory Request
• Space is available for allocation is

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
12


Consumable Resources
Example
• Consider a pair of processes, in which each process

attempts to receive a message from the other process
and then send a message to the other process.
• Deadlock may occur if the Receive is blocking (i.e., the
receiving process is blocked until the message is
received).

13


Resource Allocation
Graphs
• Directed graph that depicts a state of the
system of resources and processes

an instance of
a resource

14


Conditions for
possible 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 pre-emption
– No resource can be forcibly removed from a
process holding it
15


Actual Deadlock
Requires …
Given that the first 3 conditions exist, a
sequence of events may occur that lead to
the following fourth condition:
• 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

16


Resource Allocation
Graphs of deadlock

17


Resource Allocation
Graphs

(the traffic deadlock shown in slide 4)
18



Dealing with Deadlock
• Three general approaches exist for
dealing with deadlock.
– Prevent deadlock
• by adopting a policy that eliminates one of the
conditions

– Avoid deadlock
• by making the appropriate dynamic choices based
on the current state of resource allocation

– Detect Deadlock
• by checking whether conditions 1 through 4 hold
and take action to recover
19


Roadmap
• Principals of Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Dining Philosophers Problem

20


Deadlock Prevention

Strategy
• Design a system in such a way that the
possibility of deadlock is excluded.
• Two main methods
– Indirect: prevent the occurrence of one of the
three necessary conditions
– Direct: prevent circular waits

21


Deadlock Prevention
Conditions 1 & 2
• Mutual Exclusion
– Cannot be disallowed and must be supported
by the OS

• Hold and Wait
– Require a process request all of its required
resources at one time
–  Inefficient and may be impractical
22


Deadlock Prevention
Condition 3
• No Preemption
– If a process is denied a further request, it
must release its resource and request again.
– OS may preempt a process to require it

releases its resources to another process.
– Practical only for resources whose state can
be easily saved and restored later, e.g.,
processor.
23


Deadlock Prevention
Condition 4
• Circular Wait
– Define 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.

–  Inefficient, slowing down processes and
denying resource access unnecessarily
24


Roadmap
• Principals of Deadlock
• Deadlock Prevention
• Deadlock Avoidance
• Deadlock Detection
• Dining Philosophers Problem

25



×