One article will give you a thorough understanding of deadlock

One article will give you a thorough understanding of deadlock

During the interview, operating system knowledge is essential, and deadlock is an important part of operating system knowledge.

So to find a good job, it is necessary to understand the concept of deadlock

Four conditions for deadlock (must be met at the same time)

  • Mutually exclusive condition : a resource can only be occupied by one process at a time

  • Hold and wait : When the process is blocked due to requested resources, the acquired resources are not released

  • Non-preemptable : The resources that the process has acquired cannot be preempted by other processes during use, and can only be released by the process after use

  • Loop waiting : Multiple processes form a relationship of waiting for resources in a loop

    p 1 p1 process hold R 2 R2 , request R 1 R1 p 2 p2 process holds R 1 R1 , request R 2 R2

Deadlock solution \rightarrow Prevention of deadlock

Prevention of deadlocks: No deadlocks are allowed. You can start with the four necessary conditions to eliminate deadlocks because if they are not available at the same time

The above four necessary conditions, then deadlock will not occur

  • Mutually exclusive conditions, a mechanism essential for program concurrency, are unavoidable.
  • Hold and wait: Don't hold and wait : If a process cannot obtain all the resources in one request, it does not occupy any resources. Hold does not wait : If the resources are sufficient, as long as the application requests the resources, it will be allocated to its resources, not to wait.
  • Non-preemptable: If the resource requested by one process is occupied by another process, it can preempt the resources occupied by another process
  • Loop wait: Sort resources, that is, the order in which each process accesses resources is fixed.

The solution to the deadlock \rightarrow Avoidance of deadlock

Deadlock prevention methods can prevent deadlocks from occurring, but they will inevitably reduce system concurrency, resulting in inefficient resource utilization.

Deadlock avoidance is to avoid deadlock while the program is running.

Banker algorithm for a single resource

A banker in a small town, he promised a certain amount of loans to a group of customers. The algorithm needs to determine whether the satisfaction of the request will enter an unsafe state. If it is, the request will be rejected; otherwise it will be allocated. ( 10 resources in total )

img

Figure c above is an unsafe state, so the algorithm will reject the previous request, thus avoiding entering the state in Figure c.

Banker algorithm for multiple resources

img

There are five processes and four resources in the picture above. The figure on the left shows the resources that have been allocated, and the figure on the right shows the resources that still need to be allocated. The rightmost E, P, and A respectively represent: total resources, allocated resources, and available resources. Note that these three are vectors, rather than specific values, such as 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 whether the matrix on the right has a row less than or equal to vector A. If there is no such line, then the system will deadlock and the state is unsafe.
  • If such a line is found, mark the process as terminated and add its allocated resources 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.

Deadlock solution \rightarrow Deadlock detection and recovery

Do not try to prevent the deadlock, but take measures to recover when the deadlock is detected

  • If 进程-资源分配图there is no loop, there is no deadlock in the system
  • If 进程-资源分配图there is a loop, there are two cases
    • There is only one resource for each resource class, the system has a deadlock
    • There are multiple resources in each resource class, the system may not be deadlocked

Deadlock detection (for multiple resources in each resource class)

img

Each resource class is represented by a box, and the origin in the box indicates each resource in this resource class;

Each process is represented by a circle, and directed edges indicate the process of resource application and resource allocation.

The agreed box → circle indicates resource allocation, and the circle → box indicates resource application.

In this case, a deadlock occurs in Figure 3-6, but no deadlock occurs in Figure 3-7.

img

Deadlock recovery

  • Resource deprivation method : Deprive the resources occupied by the process stuck in the deadlock, but do not cancel the process until the deadlock is lifted.

  • Process rollback method : All processes are rolled back according to the checkpoints saved by the system until it is sufficient to remove the deadlock. This measure requires the system to establish a mechanism for saving checkpoints, rollbacks, and restarts.

  • Process revocation

    • Undo all the processes that were stuck in the deadlock, lift the deadlock, and continue to run.
    • One by one, the processes that have fallen into a deadlock are revoked, their resources are reclaimed and reallocated until the deadlock is released.

references

The generation, prevention, avoidance, detection and release of deadlock

Operating system deadlock and deadlock handling

Published 192 original articles · Like 383 · Visits 150,000+

Guess you like

Origin blog.csdn.net/zycxnanwang/article/details/105516701