Module 4 Deadlock in Operating System

100 %
0 %
Information about Module 4 Deadlock in Operating System
Education

Published on February 20, 2014

Author: hemangkothari

Source: slideshare.net

Module 4 - Deadlock Hemang Kothari Assistant Professor Computer Engineering Department MEFGI, Rajkot. Email: hemang.kothari@marwadieducation.edu.in Slides: http://www.slideshare.net/hemangkothari 2/20/2014 Computer Engineering Department - MEFGI 1

Content • • • • • • Deadlock Problem Deadlock Characterization Deadlock Detection Deadlock recovery Deadlock avoidance Deadlock Prevention. 2/20/2014 Computer Engineering Department - MEFGI 2

Motivation 2/20/2014 Computer Engineering Department - MEFGI 3

Technical Issue 2/20/2014 Computer Engineering Department - MEFGI 4

What is Deadlock • A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set. • Example – System has 2 tape drives. – P1 and P2 each hold one tape drive and each needs another one. 2/20/2014 Computer Engineering Department - MEFGI 5

Deadlock Continue • Traffic only in one direction. • Each section of a bridge can be viewed as a resource. • If a deadlock occurs, it can be resolved if one car backs up (preempt resources and rollback). • Several cars may have to be backed up if a deadlock occurs. • Starvation is possible. 2/20/2014 Computer Engineering Department - MEFGI 6

Technical Terms • Formal definition: A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause – Usually the event is release of a currently held resource • None of the processes can … – run – release resources 2/20/2014 Computer Engineering Department - MEFGI 7

Deadlock Characterization • Mutual exclusion: only one process at a time can use a resource. Four conditions • Hold and wait: a process holding at least one resource is waiting to acquire hold additional resources held by other simultaneously processes. then and then only • No preemption: a resource can be released only voluntarily deadlock arise by the process holding it, after that process has completed its task. • Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0. 2/20/2014 Computer Engineering Department - MEFGI 8

System Model • Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices • Each resource type Ri has Wi instances. • Each process utilizes a resource as follows: – request – use – release 2/20/2014 Computer Engineering Department - MEFGI 9

Deadlock Modeling • Modeled with directed graphs – resource R assigned to process A – process B is requesting/waiting for resource S – process C and D are in deadlock over resources T and U 2/20/2014 Computer Engineering Department - MEFGI 10

Modeling  Strategies for dealing with Deadlocks • Just ignore the problem altogether • Detection and recovery • Dynamic avoidance – Careful resource allocation • Prevention – negating one of the four necessary conditions 2/20/2014 Computer Engineering Department - MEFGI 11

How it Occurs 2/20/2014 Computer Engineering Department - MEFGI 12

How can we avoid 2/20/2014 Computer Engineering Department - MEFGI 13

Example of a Resource Allocation Graph 2/20/2014 Computer Engineering Department - MEFGI 14

Resource Allocation Graph With A Deadlock 2/20/2014 Computer Engineering Department - MEFGI 15

Resource Allocation Graph With A Cycle But No Deadlock 2/20/2014 Computer Engineering Department - MEFGI 16

Moral of the Story • If graph contains no cycles no deadlock. • If graph contains a cycle – if only one instance per resource type, then deadlock. – if several instances per resource type, possibility of deadlock. 2/20/2014 Computer Engineering Department - MEFGI 17

Methods to handle Deadlock • Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX. • Ensure that the system will never enter a deadlock state. • Allow the system to enter a deadlock state and then recover. 2/20/2014 Computer Engineering Department - MEFGI 18

st 1 Solution - The Ostrich Algorithm • Pretend there is no problem • Reasonable if – deadlocks occur very rarely – cost of prevention is high • UNIX and Windows takes this approach • It is a trade off between – convenience – correctness 19

2nd Solution - Deadlock Prevention Restrain the ways request can be made • Mutual Exclusion – not required for sharable resources; must hold for non-sharable resources. • Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. – Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. – Low resource utilization; starvation possible. 2/20/2014 Computer Engineering Department - MEFGI 20

Deadlock Prevention (cont) • No Preemption – – If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. – Preempted resources are added to the list of resources for which the process is waiting. – Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. • Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 2/20/2014 Computer Engineering Department - MEFGI 21

3rd Deadlock Avoidance • If we have future information – Max resource requirement of each process before they execute • Can we guarantee that deadlocks will never occur? • Avoidance Approach: – Before granting resource, check if state is safe – If the state is safe ? no deadlock! 2/20/2014 Computer Engineering Department - MEFGI 22

Safe State • When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. • System is in safe state if there exists a safe sequence of all processes. • Sequence <P1, P2, …, Pn> is safe if for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j<I. • If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished. • When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate. • When Pi terminates, Pi+1 can obtain its needed resources, and so on. 2/20/2014 Computer Engineering Department - MEFGI 23

Example of Safe State • Suppose there are 12 tape drives p0 p1 p2 max need 10 4 9 current usage 5 2 2 3 drives remain could ask for 5 2 7 • Current state is safe because a safe sequence exists: <p1,p0,p2> p1 can complete with current resources p0 can complete with current+p1 p2 can complete with current +p1+p0 • If p2 requests 1 drive, then it must wait to avoid unsafe state. 2/20/2014 Computer Engineering Department - MEFGI 24

2/20/2014 Computer Engineering Department - MEFGI 25

Banker’s Algorithm • Suppose we know the “worst case” resource needs of processes in advance – A bit like knowing the credit limit on your credit cards. (This is why they call it the Banker’s Algorithm) • Observation: Suppose we just give some process ALL the resources it could need… – Then it will execute to completion. – After which it will give back the resources. • So… – A process pre-declares its worst-case needs – Then it asks for what it “really” needs, a little at a time – The algorithm decides when to grant requests • It delays a request unless: – It can find a sequence of processes such that it could grant their outstanding need. So they would terminate. letting it collect their resources and in this way it can execute everything to completion 2/20/2014 Computer Engineering Department - MEFGI 26

More Technically • Decides whether to grant a resource request. • Data structures: n: integer # of processes m: integer # of resources available[1..m] - available[i] is # of avail resources of type i max[1..n,1..m] - max demand of each Pi for each Ri allocation[1..n,1..m] - current allocation of resource Rj to Pi need[1..n,1..m] max # resource Rj that Pi may still request let request[i] be vector of # of resource Rj Process Pi wants 2/20/2014 Computer Engineering Department - MEFGI 27

Implementation Code 1. If request[i] > need[i] then error (asked for too much) 2. If request[i] > available[i] then wait (can’t supply it now) 3. Resources are available to satisfy the request Let’s assume that we satisfy the request. Then we would have: available = available - request[i] allocation[i] = allocation [i] + request[i] need[i] = need [i] - request [i] Now, check if this would leave us in a safe state: if yes, grant the request, if no, then leave the state as is and cause process to wait. 2/20/2014 Computer Engineering Department - MEFGI 28

Safety Check free[1..m] = available /* how many resources are available */ finish[1..n] = false (for all i) /* none finished yet */ Step 1: Find an i such that finish[i]=false and need[i] <= work /* find a proc that can complete its request now */ if no such i exists, go to step 3 /* we’re done */ Step 2: Found an i: finish [i] = true /* done with this process */ free = free + allocation [i] /* assume this process were to finish, and its allocation back to the available list */ go to step 1 Step 3: If finish[i] = true for all i, the system is safe. Else Not

Example of Banker’s Algorithm • 5 processes P0 through P4; 3 resource types A (10 instances), B (5instances, and C (7 instances). • Snapshot at time T0: Allocation Max Available ABC ABC ABC P0 0 1 0 7 5 3 3 3 2 P1 2 0 0 3 2 2 P2 3 0 2 9 0 2 P3 2 1 1 2 2 2 P4 0 0 2 4 3 3 Operating System Concepts

Example (Cont.) • The content of the matrix. Need is defined to be Max – Allocation. Need ABC P0 7 4 3 P1 1 2 2 P2 6 0 0 P3 0 1 1 P4 4 3 1 • The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. Operating System Concepts

Example P1 Request (1,0,2) (Cont.) • Check that Request Available (that is, (1,0,2) (3,3,2) true. Allocation Need Available ABC ABC ABC P0 0 1 0 743 230 P1 3 0 2 020 P2 3 0 1 600 P3 2 1 1 011 P4 0 0 2 431 • Executing safety algorithm shows that sequence <P1, P3, P4, P0, P2> satisfies safety requirement. • Can request for (3,3,0) by P4 be granted? • Can request for (0,2,0) by P0 be granted?

Review • A system has four processes and five allocatable resources. The current allocation and maximum needs are as follows: Allocated Maximum Available Process A 1 0 2 1 1 11212 00x11 Process B 2 0 1 1 0 22210 Process C 1 1 0 1 0 21310 Process D 1 1 1 1 0 11221 • What is the smallest value of x for which this is a safe state? 2/20/2014 Computer Engineering Department - MEFGI 33

Solution • The needs matix is as follows: 01001 02100 10300 00111 • If x is 0, we have a deadlock immediately. • If x is 1, process D can run to completion. When it is finished, the available vector is 1 1 2 2 1. • Now A can run to complete, the available vector then becomes 2 1 4 3 2. • Then C can run and finish, return the available vector as 3 2 4 4 2. • Then B can run to complete. Safe sequence D A C B. 2/20/2014 Computer Engineering Department - MEFGI 34

Deadlock Detection & Recovery • Allow system to enter deadlock state • Detection algorithm • Recovery scheme 2/20/2014 Computer Engineering Department - MEFGI 35

Resource-Allocation Graph and Wait-for Graph Resource-Allocation Graph Corresponding wait-for graph

Recovery • Recovery through preemption – take a resource from some other process – depends on nature of the resource • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked 2/20/2014 Computer Engineering Department - MEFGI 37

Recovery • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources – choose process that can be rerun from the beginning 2/20/2014 Computer Engineering Department - MEFGI 38

1. Create the need matrix (max-allocation) 2. Use the safety algorithm to test if the system is in a safe state. 3. If the system is in a safe state, can the following requests be granted, why or why not? Please also run the safety algorithm on each request as necessary. a. P1 requests (2,1,1,0) b. P1 requests (0,2,1,0) 2/20/2014 Computer Engineering Department - MEFGI 39

Solution – Need Matrix 2/20/2014 Computer Engineering Department - MEFGI 40

Use the safety algorithm to test if the system is in a safe state – 2nd Solution Check to see if need-0 (0,1,0,0) is less than or equal to work. It is, so let’s set finish to true for that process and also update work by adding the allocated resources (0,1,1,0) for that process to work 2/20/2014 Computer Engineering Department - MEFGI 41

Continue Now, let’s check to see if need-1 (0,4,2,1) <= work. Remember that we have to check each element of the vector need-1 against the corresponding element in work. Because 1 is not less than 0 (the fourth element), we need to move on to P2. Need2 (1,0,0,1) is not less than work, so must move on to P3. Need3 (0,0,2,0) is less than work, so we can update work and finish. 2/20/2014 Computer Engineering Department - MEFGI 42

Next, let’s look at P4. Need-4 (0,6,4,2) is less than work, so we can update work and finish as follows: Now we can go back up to P1. Need1 (0,4,2,1) is less than work, so let’s update work and finish 2/20/2014 Computer Engineering Department - MEFGI 43

Finally, let’s look at P2. Need2 (1,0,0,1) is less than work, so we can then say that the system is in a safe state and the processes will be executed in the following order: P0,P3,P4,P1,P2 2/20/2014 Computer Engineering Department - MEFGI 44

3rd Solution • If the system is in a safe state, can the following requests be granted, why or why not? Please also run the safety algorithm on each request as necessary. • P1 requests (2,1,1,0) – We cannot grant this request, because we do not have enough available instances of resource A. • P1 requests (0,2,1,0) – There are enough available instances of the requested resources, so first let’s pretend to accommodate the request and see what the system looks like: – system is in a safe state when the processes are run in the following order: P0,P3,P4,P1,P2. We therefore can grant the resource request 2/20/2014 Computer Engineering Department - MEFGI 45

Add a comment

Related presentations

Related pages

Module 7: Deadlocks The Deadlock Problem

Module 7: Deadlocks • System ... Resource Allocation Graph With A Cycle But No Deadlock Operating System Concepts ... go to step 4. Operating System ...
Read more

Module 7: Deadlocks

Operating System Concepts Silberschatz and Galvin 1999 7.1 Module 7: Deadlocks • System Model ... 7.4 System Model
Read more

Operating Systems: Deadlocks - UIC - Computer Science

... "Operating System Concepts, Ninth Edition ", Chapter ... In order to avoid deadlocks, the system must have additional information ... In step 4, the ...
Read more

Deadlock - Wikipedia, the free encyclopedia

When a deadlock occurs, different operating systems respond to them in different non-standard manners. ... ACM Computing Surveys 19 (4): 303–328.
Read more

Module 8: Deadlocks - John Wiley & Sons

Module 8: Deadlocks • System Model ... 8.4 System Model ... deadlock. Applied Operating System Concepts Silberschatz, ...
Read more

Module 7: Deadlocks

Module 7: Deadlocks System Model ... Combined Approach to Deadlock Handling Operating System Concepts 7.1 Silberschatz and ... Resource type with 4 ...
Read more

www.bitmesra.ac.in

TCS1025 ADVANCED OPERATING SYSTEM CONCEPTS. Module 1 [6 periods] ... Distributed Operating Systems, ... Module 4 [4 ...
Read more

OPERATING SYSTEMS DEADLOCKS - Computer Science - WPI

OPERATING SYSTEM Deadlocks. 7: Deadlocks 3 DEADLOCKS EXAMPLES: • "It takes money to make money". ... Deadlocks 4 • Traffic only in one direction.
Read more

Deadlock - Computer Science at RPI

CSCI.4210 Operating Systems Deadlock ... */ #define SIGILL 4 /* illegal instruction (not reset when caught) */ #define SIGTRAP 5 /* trace trap ...
Read more