How to effectively avoid deadlocks as a necessary condition for deadlock generation

Source: Internet
Author: User

Deadlock in a computer system:

The cause of deadlocks usually stems from the contention of multiple processes for resources, which can cause deadlocks not only when competing for non-preemptive resources, but also when competing for consumable resources.

Can preempt resources:

The resources in the system can be divided into two categories, one is the preemptive resource, and the resource can be preempted by other processes or systems after the process has obtained the resources. For example, high-priority resources can preempt a low-priority process processor. You can also transfer a process from one store to another, and in memory tension, you can recall a process from memory to the external memory, that is, to preempt the process's memory space, that is, the CPU and main storage is a preemptive resource, for this kind of resources will not cause deadlock.

Cannot preempt resources:

Another type of resource is a non-preemptive resource, that is, once the system has allocated a resource to the process, it cannot be forcibly retracted, only after the process has been exhausted, for example, when a process has started to burn a disc, if the burner is suddenly assigned to another process, the result will inevitably damage the disc being burned, Therefore can only wait for the CD-ROM after the process to release the recorder, in addition to tape drives, printers, etc. also belong to the non-preemptive resources.

1. The competition cannot preempt the deadlock caused by the resource:

In general, the number of non-preemptive resources in the system is not sufficient to meet the needs of multiple processes running, the process is running, due to contention for resources deadlock, for example, there are two processes in the system P1,P2, they are ready to write two files f1,f2, both of which belong to the reusable and non-preemptive resources, Process P1 open F1, and then open file F, open F1,.

when two processes are executing concurrently, if P1 first opens F1 and F2, then P2 then turns on F1 (or F2), because F1 (F2) has been F1 , due to the file F2 or P1, Therefore, the P2 is blocked, when P1 write the file F1 (or F2) and close F1 (or F2, P2 will be blocked by the state into a ready state, is scheduled to reopen the file F1 (or F2), in which case, p1 and P2 can continue to run normally.

However, if P1 open the file F1, p2 to open the F2, each process occupies an open file, this time there may be a problem. Because P1 tries to open F2,p2 and tries to open the F1, the two processes will be blocked because the files are opened, they want the other person to close their required files, but no one can run, so two processes will wait indefinitely, and form a deadlock.

2. A deadlock caused by competing consumable resources

The deadlock situation caused by communication with the message communication mechanism, if M1,M2,M3 is a consumable resource,

P1:.. Send (P2,M1); Receive (P3,M3);.

P2:.. Send (P3,M2); Receive (P1,M1); .

P3:.. Send (P1,M3); Receive (p2,m2);.

All three processes can send messages to the next process, and they can also accept messages sent from the previous process, so three processes can run smoothly without deadlocks, but if you perform a receive operation and then perform a send operation , that is, the following sequence of operations

P1:receive (P3,M3); P1:.. Send (P2,M1)

P2:receive (P1,M1); P2:.. Send (P3,M2);

P3:receive (P2,M2); .. Send (P1,M3);

These three processes will permanently block their receive operation, waiting for a message that will never be sent, and a deadlock has occurred.

The necessary conditions for creating a deadlock:

(1) mutually exclusive condition. The process uses exclusive use of the allocated resources, that is, for a period of time, a resource can only be consumed by one process, and if there are other processes requesting the resource, the request process can wait until the process that consumes the resource is freed.

(2) Request and maintain conditions. The process maintains at least one resource, but it raises a new resource request, which is already occupied by another process, where the request process is blocked, but the resources it obtains are persisted.

(3) No preemption conditions. The resources that the process has obtained cannot be preempted until it is exhausted, and can only be released on its own when the process is exhausted.

(4) Cyclic wait condition. In the event of a deadlock, there must be a process-a cycle chain of resources, that is, the p0 in the process collection {P1,P2,P3...PN} is waiting for a P1 to occupy the resources, P1 is waiting for P2 to occupy the resource, ... pn is waiting for the resources that the P0 occupies.

Ways to handle deadlocks

(1) Prevent deadlocks

(2) Avoid deadlocks

(3) Detection of deadlocks

(4) Unlocking the deadlock

Method of avoiding deadlocks (banker algorithm)

The most representative algorithm for avoiding deadlocks is Dijkstra's banker algorithm, which is named because the algorithm was originally designed for the banking system to ensure that when a bank makes a cash loan, it does not meet the needs of all customers.

1. Data structure of bankers ' algorithms

(1) Available resource vector available. Represents the number of available resources, the initial value is the number of all available resources in the system configured for that class, and its value varies dynamically with the allocation and reclamation of that class of resources.

(2) Maximum demand Max, which is a n*m matrix, if max[i,j]=k, represents process I requires the maximum number of RJ class resources K.

(3) Distribution matrix allcoation. This is also a n*m matrix, which defines the number of resources that each class of resources in the system is currently assigned to each process, and if need[i,j]=k, indicates that process I also requires an RJ class resource to complete the task.

NEED[I,J]=MAX[I,J]-ALLCOATION[I,J]

2. Banker algorithm

Set request is the process P1 requests vector, if request[j]=k, indicates that process p requires an RJ type of resource, when P issues a resource request, the system follows the steps below to check

(1) If REQUEST[J]<=NEED[I,J], then turn to step 2, otherwise it is considered an error, because it requires more resources than it has declared maximum value.

(2) If REQUEST[J]<=AVAILABLE[J], turn to step 3, otherwise, indicates that there is not enough resources, P1 need to wait.

(3) The system is tempted to allocate resources to the process P1 and modify the values in the data structure.

Available [j]=Available[j]-Request[j]

ALLOCATION[I,J]=ALLOCATION[I,J]+REQUEST[I,J]

NEED[I,J]=NEED[I,J]-REQUEST[J]

(4) System execution security algorithm, check whether the resource allocation after the security state, if the security, formally assigned to the process, the completion of this allocation, or the allocation failed, the original allocation of resources to restore the state.




How to effectively avoid deadlocks as a necessary condition for deadlock generation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.