CS139-lecture-20211102

Deadlocks cont. #

Resource allocation graph cont. #

image_2021-11-02-13-29-13 image_2021-11-02-13-29-19 image_2021-11-02-13-29-27

\( P_2 \) and \( P_4 \) have the ability to exit, so the resources they hold will be allocated elsewhere. No deadlock.

image_2021-11-02-13-29-41

We can use a depth first search to look for cycles, to detect the possibility of deadlock.

Methods for handling deadlocks #

image_2021-11-02-14-01-58 image_2021-11-02-14-03-02 image_2021-11-02-14-04-19

  • to impose total order: if we have multiple resources, force process requests for resources in an increasing order of enumeration

So, from the example before, if we swap the order in which each thread obtains lock (so they request the locks in the same order), we eliminate the deadlock:

image_2021-11-02-14-14-03 image_2021-11-02-14-15-40

Deadlock avoidance #

image_2021-11-02-14-15-49 image_2021-11-02-14-15-56 image_2021-11-02-14-23-00 image_2021-11-02-14-23-05 image_2021-11-02-14-23-20 image_2021-11-02-14-25-58 image_2021-11-02-14-26-05

  • dotted lines indicate future requests, so we can detect cycles

image_2021-11-02-14-26-10 image_2021-11-02-14-26-16

Banker’s algorithm #

Another algorithm from Dijkstra. The banker’s algorithm can be used where there are multiple instances of a resource type.

image_2021-11-02-14-36-23 image_2021-11-02-14-37-42

  • available is the resource pool, the unallocated resources
  • max is a matrix that stores the maximum use of each process, a quota for each process (the most resources a process may request)
  • allocated is a matrix that stores the currently allocated resources to their respective process
  • need is a matrix that stores how many more resources a process may request in the future

Total resources can be obtained by adding allocated + available.

The first subroutine in the banker’s algorithm is the safety algorithm:

image_2021-11-02-14-41-07

  • returns whether the current state is safe or not
  • step 3 basically simulates that the process completes