CS139-lecture-20211104

Deadlocks cont #

Banker’s algorithm cont. #

Recall that the safety subroutine simulates allocating all resources, and if able to if returns true (because it is considered a safe state). If it is unable to simulate allocating all resources, it will return false.

The second subroutine of the Banker’s algorithm is the resource request:

image_2021-11-04-13-47-16

  • simulates allocating resource requests by modifying the state of the process
  • if safe, the resources can be allocated
  • otherwise, it has to wait

Example using Banker’s algorithm #

image_2021-11-04-13-49-51 image_2021-11-04-14-00-07

So first we run the safety algorithm:

image_2021-11-04-14-03-06 image_2021-11-04-14-04-33 image_2021-11-04-14-05-55 image_2021-11-04-14-07-16 image_2021-11-04-14-08-31

After the initial loop thru all processes, we then go back and check the remaining false finished processes.

image_2021-11-04-14-10-25 image_2021-11-04-14-11-45

This returns a safe sequence. A safe sequence may or may not be unique.

image_2021-11-04-14-14-26

When \( P_1 \) requests (1,0,2), the resource request subroutine is called.

image_2021-11-04-14-18-53

Then, the safety algorithm runs on this new simulated state. If it returns true, then we can approve this request. Otherwise, it is denied.

Deadlock detection #

Incase we can’t always avoid deadlock, we need a way to detect whether we are in a deadlock state or not.

image_2021-11-04-14-21-20 image_2021-11-04-14-21-24 image_2021-11-04-14-21-29

Once the wait-for graph is built, we can see that there are cycles (2). However, for multiple instance case, we rely on a variation of the Banker’s algorithm.

image_2021-11-04-14-23-55 image_2021-11-04-14-24-01 image_2021-11-04-14-24-48

  • this is the safety algorithm in the detection version
  • if any process’s finish[i] is false, then a deadlock has occured