Concurrent implementation mechanism -3- deadlock and hunger

Deadlock and starvation are two basic questions concurrent processing

Deadlock three common methods: prevention, detection and avoidance

Deadlock principle

  1. Deadlock is permanent.
  2. Each process in a group of processes are waiting for an event (typically wait for the release of the requested resource), while other processes only this set of processes that are blocked before they can trigger the event, so this group of processes on a deadlock occurred.

todo: joint process deadlock can not be avoided map area

Different types of resource deadlock

Resources are divided into two categories: reusable resources, you can consume resources.

Reusable resource is a process only once and will not use the safe use of depleted resources, the process will use and release resources, such as reusable resources example: processors, I / O channels, memory, and outside the village, equipment, and such as files, databases, data structures, and semaphore or the like. Examples of resources can be reused deadlock:

  • The two processes compete exclusive access to the file D and T, each process occupies a file and request another file so that a deadlock occurs. (Design Resources request to solve the constraint order)
  • Memory requests can be allocated space is 200KB, if the request order is: P1 process requests 80kb, P2 request 70KB, (before the request p1, p2 completed before this point of time and there is no free space) p1 request 60kb, p2 request 80KB, then deadlock occurs because not all the required space. (Request to know in advance the total amount of space, virtual memory using two methods can solve the deadlock)

Resources can consume resources can be created (production) and destruction (consumption) of. Resource resource consumption obtained after the process will cease to exist, examples are: the terminal, signals, messages, and I / O buffers. Examples are as follows:, consumable resource is usually hard to find design errors, rare events may lead to a deadlock, it may take a long time to come across.

  • Each process attempts to receive a message from another process, p1: receive (p2) ... send (p2, m1) p2: receive (p1) ... send (p1, m1)

Deadlock condition

Three necessary conditions:

  1. Mutually exclusive. Only one process may use a resource.
  2. Possession and wait. When the process waits for other processes, continue to occupy resources have been allocated.
  3. Not be preempted. It can not be forced to seize the resources have been occupied.

A sufficient condition:

  1. Circular wait. There is a closed process chain, from each child at least one resource needed for the chain to the next process

Constitute the above-described four conditions are necessary and sufficient conditions for deadlock. The fourth condition is that the first three conditions of the potential outcome of the first three conditions just shows that there may be a deadlock.

The method of treatment according to deadlock these four conditions,

** four conditions appear to eliminate certain conditions: ** Prevention

** Avoid: ** do dynamic selection based on the current state of the resource allocation

Detection **: ** error detection and recovery from deadlock exists deadlock

todo: Table 6.1 operating system, deadlock detection, prevention and avoidance method

Deadlock Prevention

Deadlock prevention divided into two categories, one is indirectly deadlock prevention, i.e. preventing the three requirements listed above; one is directly deadlock prevention, i.e. preventing the fourth condition wait cycles.

Deadlock prevention and preemption exists rollback phenomenon.

Mutually exclusive: inevitable

Possession and wait: a one-time process requests all the resources needed. (Question: process may have to be blocked for a long time, although there are some resources as long as you can continue to run; processes do not know all of the resources required)

Can not be preempted: 1. possession of certain resources to further the process of application resources if they are rejected, it must first release the resources occupied. 2. When a process requests a resource currently occupied by another process, the operating system can preempt another process, demanding the release of resources (priority if the same is not solvable). Only the case of state resources can easily save and restore (processor resources), this approach is practical.

Circular wait: Defines the resource type first order to prevent, (make the process slower execution), the mechanism that is designed to prevent the occurrence of conditions

Deadlock avoidance (can also be seen as an example of deadlock prevention)

Allows the three necessary conditions, through a wise choice, to ensure that never reaches the point of deadlock. It can allow more concurrency. Whether to allow the resource allocation request is made by determining whether the request may cause a deadlock determined. (Determination request, the possibility of killing)

Deadlock deadlock avoidance strategy does not predict the exact, step into the possibility of deadlock and ensure that never predicted such a possibility does not occur (it can be seen from the banker algorithm)

Both methods are rejected :( what is going to happen)

  • If the request process will lead to a deadlock, the process is not started
  • If the process of requesting additional resources will lead to a deadlock, you can not assign

Four kinds of vector or proof:

  • The total amount of each resource resource system
  • The total amount of available unallocated a process of each resource
  • i demand matrix of the claim process of resource j
  • The current allocation of resources allocated to the process i j

The classic problem of resource allocation algorithm bankers refused algorithm (banker algorithm), see below

Deadlock avoidance is limited:

  • Prior notice of maximum resource request
  • No execution order process synchronization requirements, the process is irrelevant
  • A fixed amount of resources allocated
  • When the share of resources can not quit the program

Deadlock Detection

Deadlock prevention strategies are very conservative, to solve the deadlock problem by limiting access to resources and impose constraints on the process. Deadlock detection does not limit resource access or restrict process behavior. For deadlock detection, whenever possible allocation of resources to process their requests.

Deadlock detection can be carried out frequently when each resource request occurs, it can be a little less, check in each request resource deadlock: a deadlock situation is detected early, simple algorithm, but can take a lot of processor time.

Deadlock detection can be divided into: Deadlock detection and recovery

Deadlock detection algorithms:

  1. Mark allocation matrix in a row all zeros process
  2. A temporary initialization vector w, so that w is equal to available
  3. I is a lookup table, such that i q flight ah who labeled the i-th row and less w, i.e. for all 1 <= k <= m, Qik <= Wk, if no such row, the algorithm terminates ( to meet the needs of a process)
  4. If such a row, labeled processes i, and the corresponding row of the matrix added allocation w, i.e. for all 1 <= k <= m, so that Wk = Wk + Aik, the step returns to the step 33 (the recovery the process resource, repeat 3)

Policy deadlock detection algorithm is to find a process so that the available resources to meet the resource request process, then agreed to assume these resources to run until the end of the process, the release of all its resources, then, the algorithm can look for another to meet the resource request process, the algorithm can not guarantee to prevent deadlock, depending on whether the consent order thinking request in the future. All the things he has done to determine whether there is currently deadlock

Only when the final result of this algorithm are unlabeled process before there is a deadlock, each unmarked process is deadlocked.

Recovery strategy:

  1. Cancel all the process deadlock
  2. Each deadlock the process to roll back to some previously defined checkpoint, and restart all processes need to build a rollback mechanism and restart the system
  3. Continuous cancel the process deadlock until there are no deadlocks, cancel the order based on a least-cost principle. Order the cancellation process should be based on a least-cost principle.
  4. Continuous seize the resource until the deadlock is not a need for a cost-based selection algorithm, a resource preempted process must be rolled back to a previous state to obtain this resource.

Select ideological principle :()

  • Processor time consumed at least so far
  • Currently the minimum output produced so far
  • Expected the rest of the longest
  • Currently the minimum amount of resources allocated so far
  • Lowest priority

Three ways to avoid deadlock :( deadlock before the article was added to go inside)

  • Fixed locking order (order lock for deadlock)

  • Open call (deadlock for collaboration between objects caused)

  • Using the timing lock -> tryLock ()

    • If you wait to acquire the lock timeout, an exception is thrown and not have to wait!

Guess you like

Origin www.cnblogs.com/cheaptalk/p/12549663.html