Lec12 - Computer Science Lecture 12, page 1 Computer...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science Lecture 12, page 1 Computer Science 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 Computer Science CS377: Operating Systems Today Deadlock Avoidance: Banker's algorithm Synchronization wrap-up Exam review Computer Science Lecture 12, page 3 Computer Science 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 Computer Science 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 Computer Science 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 Computer Science 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]> } } Computer Science Lecture 12, page 7 Computer Science CS377: Operating Systems Banker's Algorithm: Safety Check...
View Full Document

Page1 / 13

Lec12 - Computer Science Lecture 12, page 1 Computer...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online