{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Lec12 - Last Class Deadlocks Necessary conditions for...

This preview shows pages 1–5. Sign up to view the full content.

Computer Science Lecture 12, page 1 CS377: Operating Systems Last Class: Deadlocks Necessary conditions for deadlock: Mutual exclusion Hold and wait No preemption Circular wait Ways of handling deadlock Deadlock detection and recovery Deadlock prevention Deadlock avoidance Computer Science Lecture 12, page 2 CS377: Operating Systems Today Deadlock Avoidance: Banker's algorithm Synchronization wrap-up Exam review

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computer Science Lecture 12, page 3 CS377: Operating Systems Deadlock Avoidance Claim edges: an edge from a thread to a resource that may be requested in the future Satisfying a request results in converting a claim edge to an allocation edge and changing its direction. A cycle in this extended resource allocation graph indicates an unsafe state. If the allocation would result in an unsafe state, the allocation is denied even if the resource is available. The claim edge is converted to a request edge and the thread waits. This solution does not work for multiple instances of the same resource. Computer Science Lecture 12, page 4 CS377: Operating Systems Banker's Algorithm This algorithm handles multiple instances of the same resource. Force threads to provide advance information about what resources they may need for the duration of the execution. The resources requested may not exceed the total available in the system. The algorithm allocates resources to a requesting thread if the allocation leaves the system in a safe state. Otherwise, the thread must wait.
Computer Science Lecture 12, page 5 CS377: Operating Systems Preventing Deadlock with Banker's Algorithm class ResourceManager { int n; // # threads int m; // # resources int avail[m], // # of available resources of each type max[n,m], // # of each resource that each thread may want alloc[n,m], //# of each resource that each thread is using need[n,m], // # of resources that each thread might still request Computer Science Lecture 12, page 6 CS377: Operating Systems Banker's Algorithm:Resource Allocation public void synchronized allocate (int request[m], int i) { // request contains the resources being requested // i is the thread making the request if (request > need[i]) //vector comparison error(); // Can't request more than you declared else while (request[i] > avail) wait(); // Insufficient resources available // enough resources exist to satisfy the requests // See if the request would lead to an unsafe state avail = avail - request; // vector additions alloc[i] = alloc[i] + request; need[i] = need[i] - request; while ( !safeState () ) { // if this is an unsafe state, undo the allocation and wait <undo the changes to avail, alloc[i], and need[i]> wait (); <redo the changes to avail, alloc[i], and need[i]> } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computer Science Lecture 12, page 7 CS377: Operating Systems Banker's Algorithm: Safety Check private boolean safeState () { boolean work[m] = avail[m]; // accommodate all resources boolean finish[n] = false; // none finished yet // find a process that can complete its work now while (find i such that finish[i] == false and need[i] <= work) { // vector operations work = work + alloc[i] finish[i] = true; }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 13

Lec12 - Last Class Deadlocks Necessary conditions for...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online