Summary of operating system basics (4) - deadlock

1. Necessary conditions

  • Mutual exclusion : each resource is either already allocated to a process or is available.
  • Possession and waiting : A process that has already obtained a resource can request a new resource.
  • Non-preemptible : A resource that has been allocated to a process cannot be forcibly preempted, it can only be explicitly released by the process that owns it.
  • Loop waiting : There are two or more processes forming a loop, and each process in the loop is waiting for the resource occupied by the next process.

2. Processing method

  • ostrich strategy
  • Deadlock detection and deadlock recovery
  • deadlock prevention
  • deadlock avoidance

2.1 Ostrich strategy

Put your head in the sand and act like nothing is wrong . Because the cost of solving the deadlock problem is very high, the ostrich strategy, which does not take task measures, will achieve higher performance .

When a deadlock does not cause much impact on users, or the probability of a deadlock is very low, the ostrich strategy can be used. Most operating systems, including Unix, Linux, and Windows, deal with the deadlock problem simply by ignoring it .

2.2 Deadlock detection

Does not attempt to prevent deadlocks, but takes steps to recover from deadlocks when they are detected.

1) Deadlock detection for one resource of each type

The above figure is a resource allocation diagram, in which the boxes represent resources, and the circles represent processes. A resource pointing to a process means that the resource has been allocated to the process, and a process pointing to a resource means that the process requests to acquire the resource.

Figure a can extract the ring, as shown in Figure b, it satisfies the cycle waiting condition, so deadlock will occur.

The deadlock detection algorithm for each type of resource is realized by detecting whether there is a cycle in the directed graph . Starting from a node, a depth-first search is performed, and the visited nodes are marked. If the marked node is visited , it means that there is There is a cycle in the directed graph , that is, the occurrence of deadlock is detected.

2) Deadlock detection for multiple resources of each type 

In the figure above, there are three processes and four resources. The meaning of each data is as follows:

  • E vector: Total resources
  • A vector: resource remaining
  • C matrix: the number of resources owned by each process, each row represents the number of resources owned by a process
  • R matrix: number of resources requested by each process

The resources requested by processes P1 and P2 are not satisfied, only process P3 can, let P3 execute, and then release the resources owned by P3, at this time A = (2 2 2 0). P2 can be executed, and the resources owned by P2 will be released after execution, A = (4 2 2 1). P1 can also be executed. All processes can execute smoothly without deadlocks.

The algorithm is summarized as follows: each process is not marked at the beginning, and the execution process may be marked. When the algorithm ends, any process that is not marked is a deadlocked process .

  1. Find an unmarked process Pi whose resource request is less than or equal to A.
  2. If such a process is found, add the ith row vector of matrix C to A, mark the process, and roll back to 1.
  3. If there is no such process, the algorithm terminates.

2.3 Deadlock recovery

  • Recovery with preemption
  • Recovery with Rollback
  • Recover by killing the process

2.4 Deadlock Prevention

Prevent deadlocks before the program runs.

1) Destroy the mutex condition

For example, spooling printer technology allows several processes to output at the same time, and the only process that actually requests a physical printer is the printer daemon.

2) Destruction of possession and wait conditions

One way to do this is to specify that all processes request all the resources they need before starting to execute.

3) Breaking the non-preemptive condition

4) Destruction loop wait

The resources are uniformly numbered, and the process can only request resources in the order of numbering.

2.5 Deadlock Avoidance

Avoid deadlocks while the program is running.

1) Security status

 In Figure a, the second column Has indicates the number of resources already owned, the third column Max indicates the total number of resources needed, and Free indicates the number of available resources. Starting from Figure a, first let B have all the resources it needs (Figure b), release B after the operation is over, and then Free becomes 5 (Figure c); then run C and A in the same way, so that all processes can run successfully, so it can be said that the state shown in Figure a is safe.

Definition: A state is said to be safe if no deadlocks occur and there is still some scheduling order in which each process runs to completion even if all processes suddenly request the maximum demand for resources .

The detection of a safe state is similar to the detection of a deadlock, since a safe state must require that deadlocks cannot occur. The following banker's algorithm is very similar to the deadlock detection algorithm, and can be combined for reference comparison.

2) Banker's algorithm for a single resource

A banker in a small town promises a certain loan amount to a group of customers respectively. What the algorithm needs to do is to judge whether the satisfaction of the request will enter an unsafe state, and if so, reject the request; otherwise, allocate it.

3) Banker's Algorithm for Multiple Resources 

In the figure above, there are five processes and four resources. The graph on the left represents the resources that have been allocated, and the graph on the right represents the resources that still need to be allocated. E, P, and A on the far right represent: total resources, allocated resources, and available resources, respectively. Note that these three are vectors, not specific values. For example, A=(1020), which means that 4 resources are left with 1/ 0/2/0.

The algorithm for checking whether a state is safe is as follows:

  • Find if there is a row in the matrix on the right that is less than or equal to vector A. If no such row exists, the system will deadlock and the state is unsafe.
  • If such a row is found, the process is marked as terminated and its allocated resources are added to A.
  • Repeat the above two steps until all processes are marked as terminated, then the state is safe.

If a state is not safe, you need to refuse to enter this state.

References: 

  • Tanenbaum A S, Bos H. Modern operating systems[M]. Prentice Hall Press, 2014.
  • Tang Ziying, Zhe Fengping, Tang Xiaodan. Computer Operating System[M]. Xidian University Press, 2001.
  • Bryant, RE, & O'Hallaron, DR (2004). Deep understanding of computer systems.
  • Stevens. Advanced Programming in UNIX Environment [M]. People's Posts and Telecommunications Press, 2014.

Guess you like

Origin blog.csdn.net/daydayup858/article/details/129333875