Interview Questions ~ Deadlock

One: what is a deadlock? 

  •  When two or more processes seize resources, they all occupy a certain amount of resources with each other. However, they still want to obtain other resources, but the resources are likely to be occupied by other processes, which leads to blockages. Will no longer be able to move forward. Thereby deadlocking.

            For example: there are two processes A and B. A holds resource a and waits for b resource, and B holds resource b and waits for a resource. Both processes are waiting for another resource and do not release the resource while forming a deadlock.

 

      1.  Then want to avoid deadlock, understand the conditions of deadlock

     ①  Conditions

  •  Mutually exclusive condition: The process requires exclusive control of the allocated resources, that is, a certain resource is only occupied by a process for a period of time.
  • Request and hold conditions: When a process is blocked due to a request for resources, hold on to the acquired resources.

  • No deprivation condition: The resources that the process has obtained cannot be deprived before it is used up, and can only be released by itself when it is used up.
  • Loop waiting condition: When a deadlock occurs, there must be a process-a circular chain of resources. 

    

    2. Understand how to generate a deadlock. How to avoid or solve it? 

  • One-time resource allocation: All resources are allocated at once, so that there will be no more requests: (Destroy the request condition)
  • As long as one resource cannot be allocated, no other resources will be allocated to this process: (Destroy please keep the condition)
  • Deprivable resources: that is, when a process obtains some resources but cannot obtain other resources, it releases the occupied resources (destruction of inalienable conditions)
  • Ordered resource allocation method: the system assigns a number to each type of resource, each process requests resources in the order of increasing number, and the release is the opposite (breaking the loop waiting condition) 

     ① Detect deadlock

  • First specify a unique identifier for each process and each resource
  • Then create a resource allocation table and process waiting table

   Utility tool to detect deadlock: 

  • Jstack command jstack is a stack trace tool that comes with the java virtual machine. jstack is used to print out the Java stack information of a given java process ID or core file or remote debugging service. The Jstack tool can be used to generate a thread snapshot of the java virtual machine at the current moment. A thread snapshot is a collection of method stacks being executed by each thread in the current Java virtual machine. The main purpose of generating a thread snapshot is to locate the cause of a long pause in the thread, such as deadlocks between threads, dead loops, and long-term requests caused by external resources Time to wait. When the thread is paused, check the call stack of each thread through jstack, you can know what the unresponsive thread is doing in the background, or what resources it is waiting for
  • JConsole Tool Jconsole is a monitoring tool that comes with JDK and can be found in the JDK / bin directory. It is used to connect a running local or remote JVM, monitor the resource consumption and performance of Java applications, and draw a large number of charts to provide a powerful visual interface. And the server memory occupied by itself is very small, it can even be said that it is almost not consumed.
     

    ② How to solve the deadlock

  • Deprive resources: Deprive sufficient amount of resources from other processes to the deadlock process to release the deadlock state
  • Undo process: You can directly undo the deadlock process or the process with the least cost until there are enough resources available and the deadlock state is eliminated. The so-called cost refers to priority, running cost, process importance and value, etc.

 


 

 





Guess you like

Origin www.cnblogs.com/luy520/p/12721745.html