Basics of Computer Operating System (7)---Deadlock of Job Management

introduction

This article is the seventh chapter, the deadlock of job management . Deadlock is a very important concept in computer operating system. This article mainly introduces what is deadlock and how to solve it.

Deadlock

Deadlock refers to a blocking phenomenon caused by competition for resources or communication with each other during the execution of two or more processes. If there is no external force, they will not be able to advance . At this time, the system is said to be in a deadlock state or the system has a deadlock. These processes that are always waiting for each other are called deadlock processes.

for example:

If these four cars are driving in the direction shown in the picture, they are stuck at the intersection, and they do not give way to each other, the four cars will form a deadlock.

The occurrence of deadlock

  • Competing resources
  • Improper process scheduling sequence

Competing resources

Why is there competition for resources?

  • The number of shared resources does not meet the needs of each process
  • Resource competition between various processes leads to deadlock

For example:

Suppose there are two processes: process 1 and process 2. Process 1 needs to use a fax machine and has obtained fax machine resources. Process 2 needs to obtain the printer, and it is also obtained. If process 2 still needs a fax machine, or process 1 still needs a printer, they all need to wait for the requested resource to be released, but the resources they occupy each other will not be released, thus causing The occurrence of deadlock, this is the deadlock caused by competition for resources. If there is one more fax machine or printer resource at this time, there will be no deadlock. The essence is still due to insufficient resources

Improper process scheduling sequence

Taking the above process 1 and process 2 as an example, suppose process 1 applies for fax machine resources as step A, process 2 applies for printer resources as step B, process 2 applies for fax machine resources as step C, and process 1 applies for printer resources as step D . If these two processes apply for resources in the order of A, B, C, and D, a deadlock will occur . If the program can change the scheduling sequence to A, D, B, and C, there will be no deadlock at this time. Because when process 1 obtains the fax machine resource first, and then obtains the printer resource and completes its work, process 1 will release these two resources. At this time, process 2 can obtain printer and fax resources. This is due to the improper process scheduling sequence leading to deadlock

Four necessary conditions for deadlock

  • Mutually exclusive conditions
  • Request hold condition
  • Inalienable condition
  • Loop waiting condition

If only one or two of the above are met, no deadlock will occur

Mutually exclusive conditions

  • The use of resources by the process is exclusive
  • A resource can only be used by one process, and other processes that need to use the resource can only wait for the resource to be released

Request hold condition

  • The process holds at least one resource and makes a new resource request
  • New resources are occupied and requests are blocked
  • The blocked process does not release the resources it holds

Inalienable condition

  • The resources obtained by the process cannot be deprived before being used (neither the program nor the operating system)
  • The acquired resources can only be released by the process itself

Loop waiting condition

When a deadlock occurs, there must be a process-resource circular chain

P is the process, R is the resource

Deadlock handling

Ways to prevent deadlock

The four necessary conditions of deadlock mentioned above, as long as one or more of them is destroyed, the occurrence of deadlock can be avoided

Break request to keep condition

  • Before the system stipulates that the process runs, apply for all required resources at once
  • The process does not make a resource request during its operation, thus abandoning the request to keep the condition. It is impossible to cause a waiting situation because of requesting resources at runtime

Breach of inalienable conditions

  • When a process request for new resources is not satisfied, it must release the occupied resources
  • Process runtime possession of resources can be released , it can be deprived of their means

Break the loop waiting condition

  • Available resources are sorted linearly, applications must be applied in ascending order as needed
  • Linear application no longer forms a loop, thus abandoning the loop waiting condition

Suppose there are five resources A, B, C, D, and E, and arrange them in a linear order. Suppose a process needs A and D resources, it must first apply for A before applying for D, so Linear application

Banker algorithm

The banker algorithm is a well-known and operable algorithm to avoid deadlock, an algorithm based on the allocation strategy of the bank lending system

  • Assuming that the loan applied by the customer is limited, each application needs to declare the maximum amount of funds
  • Bankers should provide loans to customers when they can satisfy the loan
  • After using the loan, the customer can repay the loan in time

The above is a basis for the banker's algorithm strategy, and the following is the specific process:

This algorithm requires three data structures, namely the allocated resource table , the required resource table , and the allocatable resource table

A, B, C, and D are shared resources that can be applied for , and P1, P2, P3, and P4 are the four processes that need to apply for resources

Allocated resource table

The values ​​in the table indicate the resources currently owned by each process (for example, the P1 process has allocated 1 C resource and 4 D resources)

Required resource table

The values ​​in the table indicate the number of resources required by each process (for example, the P1 process requires 6 B resources, 5 C resources and 6 D resources)

Allocated resource table

The values ​​in the table indicate the number of various types of resources left in the system

With the tables of several data structures above, you can perform real drills

(1) The list of required resources minus the list of allocated resources

The two data structures are subtracted to obtain a table of resources to be allocated , and then the table of resources to be allocated is compared with the table of allocatable resources

We will find that the allocatable resource table does not meet the needs of process P1, because the P1 process requires 6 B resources, 4 C resources, and 2 D resources, while the available A, B, C, and D resources are only 1, respectively. 5, 2, 0, so it does not meet the needs of the process PA. P2, P3, and P4 are compared using the same method. It can be found that the resources in the allocable resource table meet the needs of P2, but do not meet the needs of P1, P3, and P4. Therefore, the system will distribute all the allocatable resources to P2, then the P2 process can continue to execute, and after P2 is executed, all the resources will be returned. After the return, the new resources can be allocated to others. process. This is the banker's algorithm. (P1, P2, P3, P4 can be regarded as people who borrow, and the numbers represent the money of the loan)

It is the core competitiveness of a technical person to find the constant in the rapidly changing technology. Unity of knowledge and action, combining theory with practice

Guess you like

Origin blog.csdn.net/self_realian/article/details/107037209