module 4 deadlock in operating system

45
Module 4 - Deadlock Hemang Kothari Assistant Professor Computer Engineering Department MEFGI, Rajkot. Email: [email protected] Slides: http://www.slideshare.net/hemangkothari Computer Engineering Department - MEFGI 1 06/09/2022

Upload: hemang-kothari

Post on 06-May-2015

1.098 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 1

Module 4 - Deadlock

Hemang KothariAssistant Professor

Computer Engineering Department MEFGI, Rajkot.

Email: [email protected]: http://www.slideshare.net/hemangkothari

Page 2: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 2

Content

• Deadlock Problem• Deadlock Characterization• Deadlock Detection• Deadlock recovery • Deadlock avoidance• Deadlock Prevention.

Page 3: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 3

Motivation

Page 4: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 4

Technical Issue

Page 5: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 5

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.

Page 6: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 6

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.

Page 7: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 7

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

Page 8: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 8

Deadlock Characterization• Mutual exclusion: only one process at a time can use a resource.• Hold and wait: a process holding at least one resource is waiting

to acquire additional resources held by other processes.• No preemption: a resource can be released only voluntarily 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.

Four conditions hold simultaneously then and then only deadlock arise

Page 9: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 9

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

Page 10: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 10

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

Page 11: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 11

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

Page 12: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 12

How it Occurs

Page 13: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 13

How can we avoid

Page 14: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 14

Example of a Resource Allocation Graph

Page 15: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 15

Resource Allocation Graph With A Deadlock

Page 16: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 16

Resource Allocation Graph With A Cycle But No Deadlock

Page 17: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 17

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.

Page 18: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 18

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.

Page 19: Module 4 Deadlock in Operating System

19

1st 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

Page 20: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 20

2nd Solution - Deadlock Prevention

• 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.

Restrain the ways request can be made

Page 21: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 21

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.

Page 22: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 22

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!

Page 23: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 23

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.

Page 24: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 24

Example of Safe State• Suppose there are 12 tape drives

max need current usage could ask forp0 10 5 5p1 4 2 2p2 9 2 7

3 drives remain

• Current state is safe because a safe sequence exists: <p1,p0,p2>p1 can complete with current resourcesp0 can complete with current+p1p2 can complete with current +p1+p0

• If p2 requests 1 drive, then it must wait to avoid unsafe state.

Page 25: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 25

Page 26: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 26

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

Page 27: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 27

More Technically • Decides whether to grant a resource request. • Data structures:

n: integer # of processesm: integer # of resourcesavailable[1..m] - available[i] is # of avail resources of type imax[1..n,1..m]- max demand of each Pi for each Riallocation[1..n,1..m] - current allocation of resource Rj to Pineed[1..n,1..m] max # resource Rj that Pi may still request

let request[i] be vector of # of resource Rj Process Pi wants

Page 28: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 28

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.

Page 29: Module 4 Deadlock in Operating System

Safety Checkfree[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

Page 30: Module 4 Deadlock in Operating System

Operating System Concepts

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 AvailableA B C A B C A B C

P0 0 1 07 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 24 3 3

Page 31: Module 4 Deadlock in Operating System

Operating System Concepts

Example (Cont.)

• The content of the matrix. Need is defined to be Max – Allocation.NeedA B C

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.

Page 32: Module 4 Deadlock in Operating System

Example P1 Request (1,0,2) (Cont.)• Check that Request Available (that is, (1,0,2) (3,3,2) true.

Allocation Need AvailableA B C A B C A B C

P0 0 1 0 7 4 3 2 3 0

P1 3 0 20 2 0

P2 3 0 1 6 0 0

P3 2 1 1 0 1 1

P4 0 0 2 4 3 1

• 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?

Page 33: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 33

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 1 1 2 1 2 0 0 x 1 1

Process B 2 0 1 1 0 2 2 2 1 0

Process C 1 1 0 1 0 2 1 3 1 0

Process D 1 1 1 1 0 1 1 2 2 1

What is the smallest value of x for which this is a safe state?

Page 34: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 34

Solution • The needs matix is as follows:

0 1 0 0 1 0 2 1 0 0 1 0 3 0 0 0 0 1 1 1

• 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.

Page 35: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 35

Deadlock Detection & Recovery

• Allow system to enter deadlock state

• Detection algorithm

• Recovery scheme

Page 36: Module 4 Deadlock in Operating System

Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Graph Corresponding wait-for graph

Page 37: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 37

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

Page 38: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 38

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

Page 39: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 39

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)

Page 40: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 40

Solution – Need Matrix

Page 41: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 41

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

Page 42: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 42

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.

Page 43: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 43

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

Page 44: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 44

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

Page 45: Module 4 Deadlock in Operating System

04/11/2023 Computer Engineering Department - MEFGI 45

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