Deadlock conditions, causes and scene analysis

Deadlock conditions, causes and scene analysis

The process can be called deadlock deadlock. It is likely to occur in the case of multi-process (concurrent) of.

Refers to a deadlock because multiple processes competing for resources caused by (wait for each other), there is no external force, then all the process will not move forward.

So is a problem in operating systems and concurrent programming requires special consideration.

Thus, the following scenarios can be drawn and prerequisites.

Scenes:

  • Competition system resources. Deadlock may appear only when the lack of resources, in addition, be deprived of the resources of the competition will not lead to deadlock;
  • Process to move in the wrong order. Multi-process at run time, request and order the release of resources properly.
  • System resource misallocation.

Four necessary conditions:

  • Exclusive: the process of allocation of resources to the exclusive use. Exclusive resources, is determined by the properties of the resource itself.
  • Request and Hold: keep the existing resources, while new resources requested in the request process and new resources because there is no obstruction, existing resources remain;
  • Inalienable: The process can not be deprived of the resources already used up before, only to release their own;
  • Loop wait: there must be a process resource request chain ring.

Deadlock prevention : to break the four conditions before

  • In breaking the exclusive little practical application;
  • Request to break and hold, it can carry out pre-allocated resource strategy, which is the process to apply all the resources needed before running a one-time, if not met, then temporarily run. Practical application, in the implementation process is dynamic, unpredictable, and resource utilization is low, reducing the process concurrency.
  • Inalienable break, when requesting new resources can not meet the need to release existing resources, system performance is greatly reduced
  • Break the cycle of waiting: the orderly implementation of resource allocation strategy. Resources can be pre-classification number assigned numbers according to the application process, taking up resources will not form a loop. All process requests for resources must strictly follow the order proposed increasing number of resources. But there are problems, difficulties in reasonable numbers, increasing the system overhead, while also increasing the process time possession of resources.

Deadlock Avoidance:

  Without limiting the application process command resources, but the resources to apply them every command issued to the process dynamically check and decide whether to allocate resources according to test results. That is, in the resource allocation process, if the prediction of the possibility of a deadlock, then be avoided. The key to this method is to determine the safety of resource allocation.

  Banker's algorithm (1968): Allows a process to dynamically apply resources, resource allocation system before each implementation, the first computing security resource allocation, the allocation of resources if the security (ie, resource allocation, the system can be in a certain order to allocate resources to each process they need, up to the maximum demand, so that each process can be completed successfully), we put the resources allocated to the process, or do not allocate resources to the process of waiting.

Deadlock detection and repair:

  Means to prevent and avoid the deadlock achieve the purpose of exclusion is very difficult. A simple method is to allocate system resources to process, do not take any restrictive measures, but provides deadlock detection and means of relief: the deadlock can be found and recovered from the deadlocked state. Thus, the actual operating system is often used in deadlock detection and recovery methods to exclude deadlock.

----------------------------------------------------------------------------------------------------

Deadlock common scenario is as follows:

Forget to release the lock:

1

2

3

4

5

6

7

8

9

void data_process()

{

    EnterCriticalSection();

 

    if(/* error happens, forget LeaveCriticalSection */)

        return;

 

    LeaveCriticalSection();

}

  

Repeat single-threaded application lock (single-threaded so when there are likely to enter a deadlock)

1

2

3

4

5

6

7

8

9

10

11

12

13

void sub_func()

{

    EnterCriticalSection();

    do_something();

    LeaveCriticalSection();

}

 

void data_process()

{

    EnterCriticalSection();

    sub_func();

    LeaveCriticalSection();

}

  

Multi-threaded multi-lock application

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

void data_process1()

{

    EnterCriticalSection(&cs1);  // 申请锁的顺序有依赖

    EnterCriticalSection(&cs2);

    do_something1();

    LeaveCriticalSection(&cs2);

    LeaveCriticalSection(&cs1);

}

 

void data_process2()

{

    EnterCriticalSection(&cs2);  // 申请锁的顺序有依赖

    EnterCriticalSection(&cs1);

    do_something2();

    LeaveCriticalSection(&cs1);

    LeaveCriticalSection(&cs2);

}

  

Multi-threaded lock ring

 

Programming How to avoid:

  • Checks have not forgotten to release the lock
  • If you own a module may be reused lock, it is recommended to use nested lock
  • We recommend the use of the library inside the lock
  • If a business needs to acquire multiple locks, lock guarantee a certain order to get
  • Write a simple test to verify whether there is a deadlock
Published an original article · won praise 0 · Views 4180

Guess you like

Origin blog.csdn.net/b_just/article/details/102659098
Recommended