Preventing Circular Waits: Understanding Deadlocks in Computer Systems, Slides of Computer Numerical Control

The concept of deadlocks in computer systems, where processes are blocked due to mutual resource dependencies. Various aspects of deadlocks, including the dining philosophers problem, deadlock conditions, resource-allocation graphs, and methods for handling deadlocks. Prevention, avoidance, and detection techniques are discussed, providing a comprehensive understanding of this important topic.

Typology: Slides

2010/2011

Uploaded on 10/07/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Deadlocks
Arvind Krishnamurthy
Spring 2004
The deadlock problem
nA set of blocked processes each holding a resource and
waiting to acquire a resource held by another process.
nExample
nlocks Aand B
P0P1
lock (A); lock (B)
lock (B); lock (A)
nExample
nSystem has 2 tape drives.
nP1and P2each hold one tape drive and each needs another one.
nDeadlock implies starvation (opposite not true)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Preventing Circular Waits: Understanding Deadlocks in Computer Systems and more Slides Computer Numerical Control in PDF only on Docsity!

Deadlocks

Arvind Krishnamurthy

Spring 2004

The deadlock problem

n A set of blocked processes each holding a resource and

waiting to acquire a resource held by another process.

n Example

n locks A and B

P 0 P 1

lock (A); lock (B)

lock (B); lock (A)

n Example

n System has 2 tape drives. n P 1 and P 2 each hold one tape drive and each needs another one.

n Deadlock implies starvation (opposite not true)

The dining philosophers problem

n Five philosophers around a table --- thinking or eating

n Five plates of food + five forks (placed between each

plate)

n Each philosopher needs two forks to eat

void philosopher (int i) { while (TRUE) { think(); take_fork (i); take_fork ((i+1) % 5); eat(); put_fork (i); put_fork ((i+1) % 5); } }

Deadlock Conditions

n Deadlock can arise if four conditions hold simultaneously:

n Mutual exclusion: limited access to limited resources

n Hold and wait

n No preemption: a resource can be released only voluntarily

n Circular wait

Deadlocks with multiple resources

P1 is waiting for P2, P2 is waiting for P3, P3 is waiting for P1 or P

Is this a deadlock scenario?

Another example

n P1 is waiting for P2 or P3, P3 is waiting for P1 or P

n Is there a deadlock situation here?

Announcements

n Midterm exam on Feb. 27th^ (Friday)

n Duration: 1:30 – 3:30??

Methods for handling deadlocks

n Question: what options do we have in dealing with

deadlocks?

Banker’s algorithm: Deadlock Avoidance

n More efficient than obtaining all resources at startup

n State maximum resources needed in advance

n Allocate resources dynamically when needed n Wait if granting request would lead to deadlock n Request can be granted if some sequential ordering of threads is deadlock free

n Sum of maximum resource needs can be greater than the total resources

n There just needs to be some way for all the threads to finish

n For example, allow a thread to proceed if: total available resources – # allocated >= max remaining needs of thread

Example

n Dining Philosophers: put forks in the middle of the table

n Rules:

n If not last fork, grab it n If last fork and requesting thread needs only one more fork, let the thread grab the fork n Else, wait

n Another set of rules:

n If not last fork, grab it n If last fork and requesting thread needs only one more fork, let the thread grab the fork n If last fork, but there is some thread who has two forks, let the requesting thread grab it n Else, wait

n Max : n x m matrix. If Max [ i,j ] = k , then process Pi may request at most k instances of resource type Rj.

n Allocation : n x m matrix. If Allocation[ i,j ] = k then Pi is currently allocated k instances of Rj.

Need [ i,j] = Max [ i,j ] – Allocation [ i,j ].

n Need : n x m matrix. If Need [ i,j ] = k , then Pi may need k more instances of Rj to complete its task.

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

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

Data Structures for Banker’s Algo

Requesti = request vector for process Pi. If Requesti [ j ] = k then process Pi wants k instances of resource type Rj.

  1. If RequestiNeedi go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim.
  2. If RequestiAvailable , 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 := NeediRequesti;;

• If safe ⇒ the resources are allocated to Pi.

• If unsafe ⇒ P i must wait, and the old resource-allocation state

is restored

Resource Request

Scenario

n P1 requests [ 1, 1 ]

n Grant it. Available: [ 2, 2]

n P2 requests [ 1, 1 ]

n Request granted. Available: [ 1, 1 ]

n P3 requests [ 1, 1 ]

n Request is not granted. P3 just blocks

n P2 requests [ 1, 0 ]

n Request is granted. One safe sequence: P2 requests [ 0, 1 ], exhausts needs, finishes execution, releases resources, …

n P1 requests [ 0, 1 ]

n Request is not granted. P1 just blocks. System waits for P2 to request, finish & release resources

Deadlock detection and recovery

n Allow system to enter deadlock state

n Detection algorithm

n use wait-for graph if single instance of each resource type n Nodes are processes. n PiPj if Pi is waiting for Pj. n periodically searches for a cycle in the graph; when and how often depends on: n How often a deadlock is likely to occur? n How many processes will need to be rolled back n harder if multiple instances of each resource type

n Recovery scheme

n process termination n resource preemption, roll-back (used in databases)

n Combine the three basic approaches

n prevention n avoidance n detection

allowing the use of the optimal approach for each of

resources in the system.

n Partition resources into hierarchically ordered classes.

n Use most appropriate technique for handling deadlocks

within each class.

Combined Approach