Deadlocks
This presentation is the property of its rightful owner.
Sponsored Links
1 / 74

Deadlocks PowerPoint PPT Presentation


  • 116 Views
  • Uploaded on
  • Presentation posted in: General

Deadlocks. Sarah Diesburg Operating Systems COP 4610. Deadlocks. Deadlocks : Occur when threads are waiting for resources with circular dependencies

Download Presentation

Deadlocks

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Deadlocks

Deadlocks

Sarah Diesburg

Operating Systems

COP 4610


Deadlocks1

Deadlocks

  • Deadlocks: Occur when threads are waiting for resources with circular dependencies

    • Often involve nonpreemptable resources, which cannot be taken away from its current thread without failing the computation (e.g., storage allocated to a file)


Deadlocks2

Deadlocks

  • Deadlocks that involve preemptable resources (e.g., CPU) can usually be resolved by reallocation of resources

  • Starvation: a thread waits indefinitely

  • A deadlock implies starvation


An example of deadlocks

An Example of Deadlocks

Thread AThread B

P(x);P(y);

P(y);P(x);

  • A deadlock won’t always happen with this code, but it might


Deadlocks deadlocks everywhere

Deadlocks, Deadlocks, Everywhere…

  • Can happen with any kind of resource

    • Among multiple resources

  • Cannot be resolved for each resource independently

    • A thread can grab all the memory

    • The other grabs all the disk space

    • Each thread may need to wait for the other to release


Deadlocks deadlocks everywhere1

Deadlocks, Deadlocks, Everywhere…

  • Round-Robin CPU scheduling cannot prevent deadlocks (or starvation) from happening

  • Can occur whenever there is waiting…


A classic example of deadlocks

A Classic Example of Deadlocks

  • Dinning lawyers (philosophers)

  • Each needs two chopsticks to eat


Dining lawyers

Dining Lawyers

  • If each first grabs the chopstick on their right before the one on their left, and all grab at the same time, we have a deadlock

    (Personally, I prefer to starve than share chopsticks…)


A dining lawyer implementation

A Dining Lawyer Implementation

semaphore chopstick[5] = {1, 1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

}

}


A dining lawyer implementation1

A Dining Lawyer Implementation

// chopstick[5] = {0, 0, 0, 0, 0};

lawyer(int j) {

while (TRUE) {

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

}

}


A dining lawyer implementation2

A Dining Lawyer Implementation

// chopstick[5] = {0, 0, 0, 0, 0};

lawyer(int j) {

while (TRUE) {

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

}

}


Conditions for deadlocks

Conditions for Deadlocks

  • Four necessary (but not sufficient) conditions

    • Limited access (lock-protected resources)

    • No preemption (if someone has the resource, it cannot be taken away)

    • Wait while holding (holding a resource while requesting and waiting for the next resource)

    • Circular chain of requests


Deadlock prevention techniques

Deadlock Prevention Techniques

  • When encountering a deadlock

    • All four conditions must be true

  • To prevent deadlocks

    • Remove one of the four conditions


Deadlock prevention techniques1

Deadlock Prevention Techniques

1. Infinite resources (buy a very large disk)


Deadlock prevention techniques2

Deadlock Prevention Techniques

2.No sharing (independent threads)


Deadlock prevention techniques3

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

easier said than done…


Deadlock prevention techniques4

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}


Deadlock prevention techniques5

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

semaphore chopstick[5] = {1, 1  0, 1  0, 1, 1}, s = 1  0;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

1


Deadlock prevention techniques6

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 0, 0, 1, 1}, s = 0;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

// eat

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

3

1


Deadlock prevention techniques7

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 1, 1, 1, 1}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

V(s);

// eat

P(s);

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}


Deadlock prevention techniques8

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 1  0, 1  0, 1, 1}, s = 1  0  1;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

V(s);

// eat

P(s);

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

1


Deadlock prevention techniques9

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 0, 0, 1  0, 1  0}, s = 1  0  1;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

V(s);

// eat

P(s);

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

1

3


Deadlock prevention techniques10

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 0, 0, 0, 0}, s = 1  0;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]); // deadlock

P(chopstick[(j + 1) % 5];

V(s);

// eat

P(s);

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

2

1

3


Deadlock prevention techniques11

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// chopstick[5] = {1, 0, 0, 0, 0}, s = 0;

lawyer(int j) {

while (TRUE) {

P(s);

P(chopstick[j]);

P(chopstick[(j + 1) % 5];

V(s);

// eat

P(s); // deadlock

V(chopstick[(j + 1) % 5];

V(chopstick[j]);

V(s);

}

}

2

1

3


Deadlock prevention techniques12

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

int counter[5] = {1, 1, 1, 1, 1}; semaphore chopstick[5] = {1, 1, 1, 1, 1}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

// if both counters j and (j + 1) % 5 > 0, decrement counters

// and grab chopstick[j] and chopstick[(j + 1) % 5]

V(s);

// if holding both chopsticks, eat

P(s);

// release chopsticks and increment counters as needed

V(s);

}

}


Deadlock prevention techniques13

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

// if both counters j and (j + 1) % 5 > 0, decrement counters

// and grab chopstick[j] and chopstick[(j + 1) % 5]

V(s);

// if holding both chopsticks, eat

P(s);

// release chopsticks and increment counters as needed

V(s);

}

}

1

(1, 2)


Deadlock prevention techniques14

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// counter[5] = {1, 0, 0, 1, 1}; chopstick[5] = {1, 0, 0, 1, 1}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

// if both counters j and (j + 1) % 5 > 0, decrement counters

// and grab chopstick[j] and chopstick[(j + 1) % 5]

V(s);

// if holding both chopsticks, eat

P(s);

// release chopsticks and increment counters as needed

V(s);

}

}

1

(1, 2)

2

()


Deadlock prevention techniques15

Deadlock Prevention Techniques

3.Allocate all resources at the beginning (if you need 2 chopsticks, grab both at the same time)

// counter[5] = {1, 0, 0, 0, 0}; chopstick[5] = {1, 0, 0, 0, 0}, s = 1;

lawyer(int j) {

while (TRUE) {

P(s);

// if both counters j and (j + 1) % 5 > 0, decrement counters

// and grab chopstick[j] and chopstick[(j + 1) % 5]

V(s);

// if holding both chopsticks, eat

P(s);

// release chopsticks and increment counters as needed

V(s);

}

}

1

3

(1, 2)

(3, 4)


Deadlock prevention techniques16

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {1, 1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}


Deadlock prevention techniques17

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {1 0, 1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

0


Deadlock prevention techniques18

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 1 0, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

0

1


Deadlock prevention techniques19

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 1 0, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

0

1

2


Deadlock prevention techniques20

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 1 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

0

1

2

3


Deadlock prevention techniques21

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

0

1

2

3

4


Deadlock prevention techniques22

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

1

2

3

4

0


Deadlock prevention techniques23

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

2

3

4

0

1


Deadlock prevention techniques24

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

3

4

0

1

2


Deadlock prevention techniques25

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1 0};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques26

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 0};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques27

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 0 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques28

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques29

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 1  0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques30

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques31

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 0  1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques32

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0  1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques33

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 1  0, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques34

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques35

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 0  1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques36

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0  1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques37

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 1  0, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques38

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques39

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 0  1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques40

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0  1, 1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques41

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {1  0, 1, 1, 1, 1};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


Deadlock prevention techniques42

Deadlock Prevention Techniques

4.Make everyone use the same ordering in accessing resource (All threads must call P(x) before P(y)

// chopstick[5] = {0, 1, 1, 1, 1  0};

lawyer(int j) {

while (TRUE) {

P(chopstick[min(j, (j + 1) % 5)]);

P(chopstick[max(j, (j + 1) % 5)]);

// eat

V(chopstick[max(j, (j + 1) % 5)]);

V(chopstick[min(j, (j + 1) % 5)]);

}

}

4

0

1

2

3


More deadlock prevention methods

More Deadlock Prevention Methods

5.No waiting (phone company)

6.Preempt resources (copying memory content to disk)

7.Banker’s algorithm

8.A combination of techniques


Banker s algorithm

Banker’s Algorithm

  • The idea of Banker’s algorithm:

    • Allows the sum of requested resources > total resources

    • As long as, there is some way for all threads to finish without getting into any deadlocks


Banker s algorithm1

Banker’s Algorithm

  • Banker’s algorithm:

    • A thread states its maximum resource needs in advance

    • The OS allocates resource dynamically as needed. A thread waits if granting its request would lead to deadlocks

    • A request can be granted if some sequential ordering of threads is deadlock free


Example 1

Example 1

  • Total RAM 256 MB

free RAM

(MB)

46

time


Example 11

Example 1

  • Total RAM 256 MB

free RAM

(MB)

46

36

P2

time


Example 12

Example 1

  • Total RAM 256 MB

free RAM

(MB)

56

46

36

P2

time


Example 13

Example 1

  • Total RAM 256 MB

free RAM

(MB)

56

46

36

26

P1

P2

time


Example 14

Example 1

  • Total RAM 256 MB

free RAM

(MB)

136

56

46

36

26

P1

P2

time


Example 15

Example 1

  • Total RAM 256 MB

free RAM

(MB)

136

56

56

46

36

26

P3

P1

P2

time


Example 16

Example 1

  • Total RAM 256 MB

256

free RAM

(MB)

136

56

56

46

36

26

P3

P1

P2

time


Example 17

Example 1

  • Total RAM 256 MB

256

free RAM

(MB)

The system is initially in

a safe state, since we can

run P2, P1, and then P3

136

56

56

46

36

26

P3

P1

P2

time


Example 2

Example 2

  • Total RAM 256 MB

free RAM

(MB)

46

time


Example 21

Example 2

  • Total RAM 256 MB

free RAM

(MB)

46

36

P2

time


Example 22

Example 2

  • Total RAM 256 MB

free RAM

(MB)

56

46

36

P2

time


Example 23

Example 2

  • Total RAM 256 MB

free RAM

(MB)

56

46

36

P2

time


Example 24

Example 2

  • Total RAM 256 MB

free RAM

(MB)

The system is initially in an unsafe state, since we cannot find an execution sequence for P1, P2, and P3

56

46

36

P2

time


Deadlock detection and recovery

Deadlock Detection and Recovery

  • Scan the resource allocation graph

  • Detect circular chains of requests

  • Recover from the deadlock


Resource allocation graph

Thread A

waiting for

owned by

Thread B

owned by

waiting for

Resource Allocation Graph

Resource Y

Resource X


Once a cycle is detected

Once A Cycle is Detected…

  • Some possible actions

    • Kill a thread and force it to give up resources

      • Remaining system may be in an inconsistent state

    • Rollback actions of a deadlocked thread

      • Not always possible (a file maybe half-way through modifications)

      • Need checkpointing, or taking snapshots of system states from time to time


  • Login