81
07/03/22 CST 352 - Operating Systems 1 Operating Systems CST 352 Deadlock

Operating Systems

Embed Size (px)

DESCRIPTION

Operating Systems. CST 352 Deadlock. Topics. Definitions Deadlock Conditions Ostrich Algorithm Deadlock Detection Deadlock Recovery Deadlock Avoidance Deadlock Prevention. Definitions. Resources – Parts of a system that are required for a process to run (e.g. memory, disk, CPU, etc.) - PowerPoint PPT Presentation

Citation preview

Page 1: Operating Systems

04/20/23 CST 352 - Operating Systems 1

Operating Systems

CST 352

Deadlock

Page 2: Operating Systems

04/20/23 CST 352 - Operating Systems 2

Topics

Definitions

Deadlock Conditions

Ostrich Algorithm

Deadlock Detection

Deadlock Recovery

Deadlock Avoidance

Deadlock Prevention

Page 3: Operating Systems

04/20/23 CST 352 - Operating Systems 3

Definitions

Resources – Parts of a system that are required for a process to run (e.g. memory, disk, CPU, etc.)

Preemptable – A resource that can be taken from a process without adverse affects (e.g. seized memory)Non Preemtable– A resource that cannot be taken away (e.g. a device driver connected to printer)

Page 4: Operating Systems

04/20/23 CST 352 - Operating Systems 4

Definitions

Deadlock – In a set of processes, each process is waiting for a resource that one of the other process has seized.

Remember the dining philosophers.

Page 5: Operating Systems

04/20/23 CST 352 - Operating Systems 5

Definitions

A Traffic Deadlock

All Stop…..

Ready…..

Go!

Page 6: Operating Systems

04/20/23 CST 352 - Operating Systems 6

Definitions

A Traffic DeadlockOops….

Deadlock

How is this deadlock avoided under normal circumstances?

Page 7: Operating Systems

04/20/23 CST 352 - Operating Systems 7

Definitions

Question:

1. Which of the two types of resource can lead to a deadlock condition?

2. Why?

Page 8: Operating Systems

04/20/23 CST 352 - Operating Systems 8

Definition

Question:

Can there be deadlock in a “first-come, first-serve” scheduling scheme?

Page 9: Operating Systems

04/20/23 CST 352 - Operating Systems 9

Required Conditions

Four conditions necessary for Deadlock:1. Mutual Exclusion – Only one process can

access a resource at any given time.

2. Hold and Wait – A process may hold a resource while waiting for others to become available.

3. No preemption – No resource may be forcibly taken from a holding process.

4. Circular wait – There must be at least two processes, each waiting for a resource held by the other.

Page 10: Operating Systems

04/20/23 CST 352 - Operating Systems 10

Deadlock Concepts

Modeling Deadlock (grid method)

X – axis: progress of process A.

Y – axis: progress of process B.

This gives us a good feel for deadlock overlap, however, it becomes unmanageable for more than two processes.

Page 11: Operating Systems

04/20/23 CST 352 - Operating Systems 11Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Rel 1Get 1 Get 2 Rel 2

Proc A andProc B wantResource 2

Proc A andProc B wantResource 1

DeadlockPending

Deadlock ConceptsModeling

Deadlock (Resource Trajectories)

Example: Proc A and Proc B competing for Resource 1 and Resource 2.

Page 12: Operating Systems

04/20/23 CST 352 - Operating Systems 12

Deadlock Concepts

Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Rel 1Get 1 Get 2 Rel 2

Proc A andProc B wantResource 2

Proc A andProc B wantResource 1

DeadlockPending

5. B acquires 1

6. B releases 2

7. B releases 1

8. A can acquire both 1 and 2

4. B acquires 2

1. A Executes

2. B Executes

3. A Executes

Page 13: Operating Systems

04/20/23 CST 352 - Operating Systems 13

Deadlock Concepts

Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Rel 1Get 1 Get 2 Rel 2

Proc A andProc B wantResource 2

Proc A andProc B wantResource 1

DeadlockPending

5. B acquires 1

6. A blocks on 1

7. B releases 2

9. A can acquire both 1 and 2

4. B acquires 2

1. A Executes

2. B Executes

3. A Executes

8. B releases 1

Page 14: Operating Systems

04/20/23 CST 352 - Operating Systems 14

Deadlock Concepts

Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Rel 1Get 1 Get 2 Rel 2

Proc A andProc B wantResource 2

Proc A andProc B wantResource 1

DeadlockPending

5. A acquires 1

6. B blocks on 1

7. A blocks on 2

4. B acquires 2

1. A Executes

2. B Executes

3. A Executes

8. Deadlock

X

Page 15: Operating Systems

04/20/23 CST 352 - Operating Systems 15

Deadlock Concepts

Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Get 2Get 1 Rel 1 Rel 2

Proc A andProc Bwant

Resource 2

Proc A andProc Bwant

Resource 1

To reduce resource contention, construct Process A such that it does not need both resource 1 and resource 2 at the same time.

This increases the number of valid paths through the “gray” areas.

Page 16: Operating Systems

04/20/23 CST 352 - Operating Systems 16

Deadlock Concepts

Modeling Deadlock (grid method)

The resource trajectory model has disadvantages

Unmanageable for complex systems

Dependent on resource acquisition sequencing

This model does suggest solutions to deadlock (e.g. avoid getting into the “Deadlock Pending” region.

Page 17: Operating Systems

04/20/23 CST 352 - Operating Systems 17

Deadlock ConceptsModeling Deadlock (graph method)

A circle represents a processA square represents a resourceA directed arch from a circle to a square represents a process requesting a resource.

P1 requesting R1

A directed arch from square to circle represents a process holding a resource

P1 holding R1

A Deadlock is condition is represented by a “cycle” in the graph.

P1 R1

P1 R1

Page 18: Operating Systems

04/20/23 CST 352 - Operating Systems 18

Deadlock Concepts

Modeling Deadlock (graph method)

Example:Processes P1, P2, and P3

Resources R1, R2, and R3

P1

R1

P3P2

R2 R3

Page 19: Operating Systems

04/20/23 CST 352 - Operating Systems 19

Deadlock Concepts

Modeling Deadlock (graph method)

P1

R1

P3P2

R2 R3

1. P1 requests and holds R1

2. P2 requests and holds R2

3. P3 requests and holds R3

4. P1 requests R2

5. P2 requests R3

6. P3 requests R1

The cyclic condition of the graph indicates this sequence of event will cause deadlock.

Page 20: Operating Systems

04/20/23 CST 352 - Operating Systems 20

Ostrich Algorithm

Stick your head in the sand and pretend there is no problem at all.Mathematically speaking – this is unacceptable.From an engineering point of view, this may be ok.

Page 21: Operating Systems

04/20/23 CST 352 - Operating Systems 21

Ostrich Algorithm

Considerations:

What is the half-life of your OS.

What resource contentions could cause a deadlock.

How often statistically does your OS experience a visible deadlock.

Page 22: Operating Systems

04/20/23 CST 352 - Operating Systems 22

Ostrich Algorithm

The Ostrich Algorithm is the easiest solution to the deadlock problem.In standard “user” based systems (e.g. Windows, Unix Workstations, etc.), this is perfectly acceptable.Would you consider the “Ostrich Algorithm” an acceptable solution for an enterprise management system?

Page 23: Operating Systems

04/20/23 CST 352 - Operating Systems 23

Deadlock Detection

Graph based approachFor each resource access

Create a node representing the process accessing the resource.Create a node representing the resource.Make the appropriate directed association.

Page 24: Operating Systems

04/20/23 CST 352 - Operating Systems 24

Deadlock Detection

Graph based approach (example)1. PA requests R1 and gets it.2. PB requests R2 and gets it.3. PC requests R2.

4. PD requests R3 and gets it.5. PD requests R1.

PA R1

PB R2

PC R3 PD

6. PA requests R3.

Every time a resource request is made:

1. Add the appropriate node to the graph

2. Check the graph for cycles.

Page 25: Operating Systems

04/20/23 CST 352 - Operating Systems 25

Deadlock Detection

Graph based approachTo implement the graph based approach, the OS needs to:

Build a graph on the fly based on resource requests.Provide some form of cycle detection to detect deadlock conditions in the constructed graph.

Page 26: Operating Systems

04/20/23 CST 352 - Operating Systems 26

Deadlock Detection

Q:

1. List the four conditions for deadlock.

Page 27: Operating Systems

04/20/23 CST 352 - Operating Systems 27

Deadlock Detection

Matrix ApproachLet “n” = number of processes == (P1, …, Pn).Let “m” = number of resource classes.

E1 – resource of type 1

E2 – resource of type 2…Em – resource of type m

Page 28: Operating Systems

04/20/23 CST 352 - Operating Systems 28

Deadlock Detection

Matrix ApproachAt any point in time, the allocation of resources can be represented by a two vectors.

(E1, E2, …, Em) – Existing resource vector.

(A1, A2, …, Am) – Available resource vector.

Page 29: Operating Systems

04/20/23 CST 352 - Operating Systems 29

Deadlock Detection

Matrix ApproachRepresent allocation with a matrix

Columns represent resourcesRows represent processes

Represent requests with a matrixColumns represent resourcesRows represent processes

Page 30: Operating Systems

04/20/23 CST 352 - Operating Systems 30

Deadlock Detection

Matrix Approach

Current Allocation and Requestc11 c12 … c1m

c22 c22 … c2m

… … …

cn1 cn2 … cnm

pro

cess

Current Allocation

r11 r12 … r1m

r22 r22 … r2m

… … …

rn1 rn2 … rnm

pro

cess

Current Request

Page 31: Operating Systems

04/20/23 CST 352 - Operating Systems 31

Deadlock Detection

Matrix Approach

At any point in time, the resource state must conform to the equation:

For any j (j -> resource “column”):

i = 1

n

cij+Aj = Ej

e.g. allocated instances of resource j + available instances of resource j = number of existing instances of resource j.

Page 32: Operating Systems

04/20/23 CST 352 - Operating Systems 32

Deadlock Detection

Matrix ApproachProcess allocation is now done by comparing vectors.

Vector A <= B iffFor all i < m, Ai <= Bi

Initialize the processes to “r” for “run”.As the algorithm proceeds, set the process flags to either “r” for processes capable of running, or “b” for processes blocked and waiting for a resource.

Page 33: Operating Systems

04/20/23 CST 352 - Operating Systems 33

Deadlock Detection

Matrix ApproachEvery time there is a resource allocation:

Look for a process (Pi) for which the request row (Ri) <= available resources (Ai).

Looking for resource demands that can be met.

If such a process is found, add the ith row of the c matrix to the A vector.

Loop over next request.

ElseTerminate the detection algorithm and set Pi to “b”

All “b” processes are blocked.

Page 34: Operating Systems

04/20/23 CST 352 - Operating Systems 34

Deadlock Detection

Matrix ApproachIn practical application, keep the matrices as a running total.

Each time there is a resource allocation, update the matrices.

Each time there is a resource de-allocation, update the matrices.

Deadlock occurs when two (or more) processes have cross referenced resources.

Page 35: Operating Systems

04/20/23 CST 352 - Operating Systems 35

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [3 2 2]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

0 0 00 0 00 0 0

2 0 00 0 00 0 0

C = R =P1P2P3

1) P1 requests 2 Printers

Current Allocation Requestrrr

Page 36: Operating Systems

04/20/23 CST 352 - Operating Systems 36

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [1 2 2]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 00 0 0

0 0 00 0 00 0 0

C = R =P1P2P3

Current Allocation Requestrrr

Page 37: Operating Systems

04/20/23 CST 352 - Operating Systems 37

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [1 2 2]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 00 0 0

0 0 01 0 20 0 0

C = R =P1P2P3

2) P2 requests 1 Printer, 2 DVDs

Current Allocation Requestrrr

Page 38: Operating Systems

04/20/23 CST 352 - Operating Systems 38

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 2 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 01 0 20 0 0

0 0 00 0 00 0 0

C = R =P1P2P3

Current Allocation Requestrrr

Page 39: Operating Systems

04/20/23 CST 352 - Operating Systems 39

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 2 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 01 0 20 0 0

0 0 00 0 01 1 0

C = R =P1P2P3

3) P3 requests 1 Printer, 1 CD Rom

Current Allocation Requestrrr

Page 40: Operating Systems

04/20/23 CST 352 - Operating Systems 40

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 2 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 01 0 20 0 0

0 0 00 0 01 1 0

C = R =P1P2P3

4) P3 blocked

Current Allocation Requestrrb

Page 41: Operating Systems

04/20/23 CST 352 - Operating Systems 41

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 2 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 01 0 20 0 0

0 0 00 0 01 1 0

C = R =P1P2P3

5) P2 frees up printer

Current Allocation Requestrrb

Page 42: Operating Systems

04/20/23 CST 352 - Operating Systems 42

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [1 2 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 20 0 0

0 0 00 0 01 1 0

C = R =P1P2P3

Current Allocation Requestrrb

Page 43: Operating Systems

04/20/23 CST 352 - Operating Systems 43

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 00 0 00 0 0

C = R =P1P2P3

Current Allocation Requestrrr

6) P3 can now run

Page 44: Operating Systems

04/20/23 CST 352 - Operating Systems 44

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 10 0 00 0 0

C = R =P1P2P3

Current Allocation Requestrrr

7) P1 request DVD

Page 45: Operating Systems

04/20/23 CST 352 - Operating Systems 45

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 10 0 00 0 0

C = R =P1P2P3

Current Allocation Requestbrr

8) P1 blocked

Page 46: Operating Systems

04/20/23 CST 352 - Operating Systems 46

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 11 0 00 0 0

C = R =P1P2P3

Current Allocation Requestbrr

9) P2 request printer

Page 47: Operating Systems

04/20/23 CST 352 - Operating Systems 47

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 11 0 00 0 0

C = R =P1P2P3

Current Allocation Requestbbr

10) P2 blocked

Page 48: Operating Systems

04/20/23 CST 352 - Operating Systems 48

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 11 0 00 0 0

C = R =P1P2P3

Current Allocation Requestbbr

• This is still not deadlock since P3 holds a resource that could allow P2 to commence execution.

• If P1 held all 3 printers, we would have true deadlock.

Page 49: Operating Systems

04/20/23 CST 352 - Operating Systems 49

Deadlock Detection

Matrix Approach (Example)

Resource Vector Available Vector

E = [3 2 2] A = [0 1 0]

Pri

nte

r

CD

Rom

DV

D

Pri

nte

r

CD

Rom

DV

D

2 0 00 0 21 1 0

0 0 11 0 00 0 0

C = R =P1P2P3

Current Allocation Requestbbr

• What are the general conditions for true deadlock?

Page 50: Operating Systems

04/20/23 CST 352 - Operating Systems 50

Deadlock Detection

Hypothesis (extra credit – 50 pts)Prove or disproveDeadlock can be detected by rotating the

rows and columns of the C and R matrix (in conjunction) so that if C and R have any inverse sub-matrices, the involved processes are deadlocked.

E and A must be involved.

Page 51: Operating Systems

04/20/23 CST 352 - Operating Systems 51

Deadlock Recovery

Given the above algorithm, the OS can detect which process is waiting for what resource.

Now the OS needs to decide what to do when a deadlocked process is detected.

Page 52: Operating Systems

04/20/23 CST 352 - Operating Systems 52

Deadlock Recovery

Recovery through preemptionTake a resource away from a process and give it to another.

Only works for preemptable processes.

Page 53: Operating Systems

04/20/23 CST 352 - Operating Systems 53

Deadlock Recovery

Recovery through rollbackPCB (TCB) and resource state are periodically saved at a “checkpoint”

When a deadlock is detected, back the deadlocked process up to the previous stable state.

Discard the resource manipulation that occurred after the checkpoint.

Start the process after it is determined it can run again.

Page 54: Operating Systems

04/20/23 CST 352 - Operating Systems 54

Deadlock Recovery

Recovery through process killing

Kill off the offending processes.The processes that have allocated contending resources are removed from the system.

Kill off the lower priority process.

Kill off non-OS (user) processes.

Return their resources to the system.

Page 55: Operating Systems

04/20/23 CST 352 - Operating Systems 55

Deadlock Recovery

Q:1. How can a system be constructed

so it will never have deadlock?2. Give an example.3. T or F – The graph model for

deadlock only works well for 2 processes.

Page 56: Operating Systems

04/20/23 CST 352 - Operating Systems 56

Deadlock Avoidance

Process Trajectories – revisited

Process A

Pro

cess

B

Get 2

Rel 1

Rel 2

Get 1

Rel 1Get 1 Get 2 Rel 2

Proc A andProc B wantResource 2

Proc A andProc B wantResource 1

DeadlockPending

Shaded regions with slanted lines represent instances where both processes gain access to a resource at the same time.

If the resources are mutually exclusive, it is impossible for these areas to be entered by a “run time trajectory”.

Page 57: Operating Systems

04/20/23 CST 352 - Operating Systems 57

Deadlock Avoidance

Bankers AlgorithmThe banker at Klamath First Federal is granting a group of customers credit.The banker has limited cash to loan.Each customer has a credit limit.The banker assumes no customer will request their entire credit limit at a single time.

Page 58: Operating Systems

04/20/23 CST 352 - Operating Systems 58

Deadlock Avoidance

Bankers AlgorithmThe banker grants loans to the customers based customer credit limit and available cash reserve.The bankers job is to avoid the situation where cash reserves drop so low that the FDIC shuts the bank down.Loans that may cause inspection by the FDIC are called “unsafe” loans.

Page 59: Operating Systems

04/20/23 CST 352 - Operating Systems 59

Deadlock Avoidance

Bankers Algorithm (Example)4 customers – Joe, Jay, Jim, and JillInitial reserve of 10 dollars (it is a very small bank).Each customer has a credit limit.

The goal here is to ensure that two customers cannot be simultaneously blocked waiting on a resource.

Page 60: Operating Systems

04/20/23 CST 352 - Operating Systems 60

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

0

0

0

0

6

7

4

5

Reserve: 10

1. Joe requests 1

Page 61: Operating Systems

04/20/23 CST 352 - Operating Systems 61

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

0

6

7

4

5

Reserve: 9

1. Joe requests 1 – granted

Page 62: Operating Systems

04/20/23 CST 352 - Operating Systems 62

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

0

6

7

4

5

Reserve: 9

1. Joe requests 1 – granted2. Jay requests 1

Page 63: Operating Systems

04/20/23 CST 352 - Operating Systems 63

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

1

6

7

4

5

Reserve: 8

1. Joe requests 1 – granted2. Jay requests 1 – granted

Page 64: Operating Systems

04/20/23 CST 352 - Operating Systems 64

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

1

6

7

4

5

Reserve: 8

1. Joe requests 1 – granted2. Jay requests 1 – granted3. Jim requests 2

Page 65: Operating Systems

04/20/23 CST 352 - Operating Systems 65

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

1

6

7

4

5

Reserve: 6

1. Joe requests 1 – granted2. Jay requests 1 – granted3. Jim requests 2 – granted

Page 66: Operating Systems

04/20/23 CST 352 - Operating Systems 66

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

1

6

7

4

5

Reserve: 6

1. Joe requests 1 – granted2. Jay requests 1 – granted3. Jim requests 2 – granted4. Jill requests 4

Page 67: Operating Systems

04/20/23 CST 352 - Operating Systems 67

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

4

2

1

6

7

4

5

Reserve: 2

1. Joe requests 1 – granted2. Jay requests 1 – granted3. Jim requests 2 – granted4. Jill requests 4 – granted

Page 68: Operating Systems

04/20/23 CST 352 - Operating Systems 68

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

4

2

1

6

7

4

5

Reserve: 2

At any point in time, in this scenario, the states are safe because, with at least two units left, the banker can delay granting any requests until Jim pays back his loan. Jim can ask for the additional 2 dollars.

Page 69: Operating Systems

04/20/23 CST 352 - Operating Systems 69

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

4

0

1

6

7

4

5

Reserve: 4

When Jim pays back his loan, the banker can lend to Jay or Jill.

Page 70: Operating Systems

04/20/23 CST 352 - Operating Systems 70

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

2

6

7

4

5

Reserve: 7

1. Joe requests 1 – granted2. Jay requests 2 – granted

Unsafe State Example

Page 71: Operating Systems

04/20/23 CST 352 - Operating Systems 71

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

0

2

6

7

4

5

Reserve: 7

1. Joe requests 1 – granted2. Jay requests 2 – granted3. Jim requests 2

Unsafe State Example

Page 72: Operating Systems

04/20/23 CST 352 - Operating Systems 72

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

2

6

7

4

5

Reserve: 5

1. Joe requests 1 – granted2. Jay requests 2 – granted3. Jim requests 2 – granted

Unsafe State Example

Page 73: Operating Systems

04/20/23 CST 352 - Operating Systems 73

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

2

6

7

4

5

Reserve: 5

1. Joe requests 1 – granted2. Jay requests 2 – granted3. Jim requests 2 – granted4. Jill requests 4

Unsafe State Example

Page 74: Operating Systems

04/20/23 CST 352 - Operating Systems 74

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

2

6

7

4

5

Reserve: 5

1. Joe requests 1 – granted2. Jay requests 2 – granted3. Jim requests 2 – granted4. Jill requests 4

With a reserve of 1, potentially, any other request could be denied. If the banker gives the

loan to Jill, the reserve will be down to 1.

Unsafe State Example

Page 75: Operating Systems

04/20/23 CST 352 - Operating Systems 75

Deadlock Avoidance

Bankers Algorithm (Example)

Name LimitHas

Joe

Jay

Jim

Jill

1

0

2

2

6

7

4

5

Reserve: 5

1. Joe requests 1 – granted2. Jay requests 2 – granted3. Jim requests 2 – granted4. Jill requests 4

Granting Jill the loan would force the system into an unsafe state.

If all customers demanded their maximum loan, none of them could be granted, forcing the FDIC to come down on the bank (deadlock).

Page 76: Operating Systems

04/20/23 CST 352 - Operating Systems 76

Deadlock Avoidance

Bankers Algorithm (multiple resource)The bankers algorithm for multiple resource is just a modification of the matrix approach for deadlock detection.The modification made relates to what the scheduler does during the application of the algorithm.Rather than waiting for deadlock to occur, the scheduler takes proactive action during resource allocation and de-allocation.

Page 77: Operating Systems

04/20/23 CST 352 - Operating Systems 77

Deadlock Avoidance

Bankers Algorithm (multiple resource)The problem with the bankers algorithm for deadlock avoidance is to operate correctly, the resources must be pre-determined and stable.

There are no systems where resources are all pre-determined and infinitely stable.

Page 78: Operating Systems

04/20/23 CST 352 - Operating Systems 78

Deadlock Prevention

Attack mutual exclusion condition

Do not allow any resources to be mutually exclusive.

Printer spooling was introduced to do this.

Page 79: Operating Systems

04/20/23 CST 352 - Operating Systems 79

Deadlock Prevention

Attack the hold and wait conditionDo not allow any process to seize a resource and hold it.

Require all processes to request their resources up-front.

This solution introduces inefficiencies.

Page 80: Operating Systems

04/20/23 CST 352 - Operating Systems 80

Deadlock Prevention

Attack the No-Preempt condition

It is close to impossible to get rid of all non-preemptable resources in a system.

Page 81: Operating Systems

04/20/23 CST 352 - Operating Systems 81

Deadlock Prevention

Attack the Circular wait condition

Only allow a process one resource at a time.Require process to adhere to a resource numbering scheme sequential allocation