Interview Questions of operating system: four necessary conditions for deadlock and solutions

Deadlock concept and principle of generation

  The concept: multiple concurrent processes compete for system resources due to the phenomenon of waiting for each other.

  How it works: When a group of processes in each process are waiting for an event occurs, and only other processes that set the process in order to trigger the event, said this group of processes which occur deadlock.

  The essential reason:

  1) the limited system resources

  2) unreasonable order to promote the process

4 prerequisite Deadlock

  1, mutually exclusive : a resource allows only one process to access, that is, once the resource is assigned to a process, other processes can no longer access until the end of the visit process.

  2, possession and wait : a process itself takes the resource (one or more), as well as resources have not been met, is waiting for other processes to release the resource.

  3, can not be seized : someone else has possession of a resource, you can not because they need the resources to go to other people's resources take it over.

  4, circular wait : there is a process chain, so that each process has played at least one of the resources needed for the next process.

  When the above four conditions are met, will definitely lead to a deadlock, the process deadlock can not go on, they can not release the resources held. This will cause the CPU throughput. So a deadlock situation is a waste of system resources and affect the performance of the computer. So, to solve the deadlock problem is quite necessary up.

Ways to avoid deadlock

1, deadlock prevention ----- ensure that the system will never enter a deadlock state

   Deadlock requires four conditions, then, as long as there is at least one condition of the four conditions are not met, it is impossible deadlock occurred. Since the condition is non-exclusive shared resources necessary, not only can not be changed, it should also be guaranteed, so the other three main conditions for deadlock damage.

  A , destruction of "possession and wait" condition

    Method 1: all processes before running, you must apply once all the resources in its entire run in need.

    Advantages: easy to implement and Safety

    Cons: Because a resource is not satisfied, the process can not be started, while others have been met will not be utilized resources, severely reducing the utilization of resources, a waste of resources. The process hunger frequently.

    Method 2: This method is the first method of improvement, the process is only allowed access to resources needed for the operation early, they start to run, during operation gradually freed resource allocation had been used up to then go to new request resources. In this case, resource utilization will be improved, also the problem of hunger reduction process.

  B , destruction "not seize" conditions

    When a process already holds some resources in the proposed new resource request is not met, it must release all resources have been held, to be used later when needed and then re-apply. This means that the process has possession of resources will be briefly released or be seized.

    This method is more complex to implement, and the cost is relatively large. Release resources have been held before is likely to lead to effective work processes, etc., repeated application and release of resources will lead to the implementation process is hugely delayed, it will not only prolong the process of turnaround, but also affect the system throughput.

  c , destruction "cycle wait" condition

    Can be prevented by the linear sequence defined resource types, each resource may be numbered, when a process occupies a resource number i, then it can only apply for next application for resources is greater than the number of resource i. as the picture shows:

Although this avoids the wait cycle, but this song method is relatively inefficient, slow speed of execution resources back, and may deny access to resources in the absence of necessary, for example, wants to apply for funding process c 1, If the resource 1 is not occupied by another process, then assign it a process c is not a problem, but in order to avoid waiting cycle, the application will be rejected, thus reducing resource utilization.

2, to avoid deadlock ------ judge before use, only allows the process does not produce deadlock application resources

Deadlock avoidance is the use of https://blog.csdn.net/guaiguaihenguai/article/details/80303835

 

Guess you like

Origin www.cnblogs.com/yichengming/p/11450413.html