Docsity

Deadlocks in Operating Systems: Necessary Conditions, Strategies, and Prevention, Study notes of Computer Science

Partial preview of the text

Download Deadlocks in Operating Systems: Necessary Conditions, Strategies, and Prevention and more Study notes Computer Science in PDF only on Docsity!

Operating SystemsOperating Systems

CMPSC 473 CMPSC 473

Deadlocks Deadlocks

February 28, 2008 - Lecture February 28, 2008 - Lecture 1313

Instructor: Trent Jaeger Instructor: Trent Jaeger

  • Last class:
    • Synchronization
  • Today:
    • Deadlocks

Necessary Conditions

for a Deadlock

  • Mutual exclusion: The requesting process is delayed until the

resource held by another is released.

  • Hold and wait: A process must be holding at least 1 resource

and must be waiting for 1 or more resources held by others.

  • No preemption: Resources cannot be preempted from one

and given to another.

  • Circular wait: A set (P0,P1,…Pn) of waiting processes must

exist such that P0 is waiting for a resource held by P1, P1 is

waiting for …. by P2, … Pn is waiting for … held by P0.

Resource Allocation Graph

• Vertices (V) = Processes (Pi) and Resources (Rj)

• Edges (E) = Assignments (Rj->Pi, Rj is allocated to

Pi) and Request (Pi->Rj, Pi is waiting for Rj).

• For each Resource Rj, there could be multiple

instances.

• A requesting process can be granted any one of

those instances if available.

A deadlock P1 P2 P R R R2 R

If there is a deadlock, there will be a cycle

(Necessary Condition).

A cycle is NOT a sufficient condition

  • P1 P2 P
    • R - R
      • R2 R
  • P - P - P
    • R
      • R
        • P

Deadlock Prevention

  • Note that all 4 necessary conditions need to hold for

deadlock to occur.

  • We can try to disallow one of them from happening:
    • Mutual exclusion: This is usually not possible to avoid with many resources.
    • No preemption: This is again not easy to address with many resources. Possible for some resources (e.g. CPU)
    • Hold and Wait:
      • Allow at most 1 resource to be held/requested at any time
      • Make sure all requests are made at the same time.
      • …
  • Circular Wait
    • Number the resources, and make sure requests are

always made in increasing/decreasing order.

  • Or make sure you are never holding a lower

numbered resource when requesting a higher

numbered resource.

Safe Unsafe Deadlocked Start End

State Transitions

Safe State

P2 9 2 7

P1 4 2 2

P0 10 5 5

Still Needs Currently Allocated Max. Needs

1 resource with 12 units of that resource available.

Current State: Free = (12 – (5 + 2 + 2)) = 3 This state is safe because, there is a sequence (P followed by P0 followed by P2) by which max needs of each process can be satisfied. This is called the reduction sequence. Free = 3 After reducing P1, Free = 5 After reducing P0, Free = 10 Then reduce P2.

  • Deadlock avoidance essentially allows requests to be satisfied only when the allocation of that request would lead to a safe state.
  • Else do not grant that request immediately.

Banker’s algorithm for deadlock avoidance

  • When a request is made, check to see if after the request is satisfied, there is a (at least one!) sequence of moves that can satisfy all possible requests. ie. the new state is safe.
  • If so, satisfy the request, else make the request wait.

An example

5 processes, 3 resource types A (10 instances), B (5 instances), C (7 instances) P4 4 3 3

P3 2 2 2

P2 9 0 2

P1 3 2 2

P0 7 5 3

A B C

P4 0 0 2

P3 2 1 1

P2 3 0 2

P1 2 0 0

P0 0 1 0

A B C

P4 4 3 1

P3 0 1 1

P2 6 0 0

P1 1 2 2

P0 7 4 3

A B C

A B C

MaxNeeds Allocated StillNeeds Free This state is safe, because there is a reduction sequence <P1, P3, P4, P2, P0> that can satisfy all the requests. Exercise: Formally go through each of the steps that update these matrices for the reduction sequence.

If P1 requests 1 more instance of A and 2 more instances of C can we safely allocate these? – Note these are all allocated together! and we denote this set of requests as (1,0,2) If allocated the resulting state would be: P4 4 3 3

P3 2 2 2

P2 9 0 2

P1 3 2 2

P0 7 5 3

A B C

P4 0 0 2

P3 2 1 1

P2 3 0 2

P1 3 0 2

P0 0 1 0

A B C

P4 4 3 1

P3 0 1 1

P2 6 0 0

P1 0 2 0

P0 7 4 3

A B C

A B C

MaxNeeds Allocated StillNeeds Free This is still safe since there is a reduction sequence <P1,P3,P4,P0,P2> to satisfy all the requests. (work this out!) Hence the requested allocations can be made.