About blocking / non-blocking, synchronous / asynchronous, deadlock

1, blocking / non-blocking, synchronous / asynchronous

Synchronize with asynchronous (message notification mechanism): concern is to inform the way waiting process needs its own initiative to ask or passive notification.

Blocking and non-blocking (state of waiting for message notification): concern is whether to wait for the process can be converted to other non-wait state.

For example: Let's say I want to download a video

1, waiting for the results of the download is complete by looking at the download progress bar (synchronous), do not do other things during the (blocking)

2, wait for the results of the download is complete by looking at the download progress bar (synchronous), go during the QQ chat, chat QQ in time to see non-stop download is complete (non-blocking)

3, the download is complete tone is notified of the result of the download is complete (asynchronous), do not do other things during the (blocking)

4, obtained by the tone download is complete notification of the result of the download is complete (asynchronous), go during the QQ chat, QQ chat at the time do not need to see whether the download is complete, because the download finished beep will inform my (non-blocking)

On these four concepts of time must be on the same level, such as the operating system level, frame level, business code level, etc., because of the nature of an event is not necessarily the same in different levels belong only in the same level, in order to discuss it is the nature of synchronous / asynchronous or blocking / non-blocking.

 

2, deadlock

Four necessary conditions for deadlock 2.1

Deadlock refers to two or more processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, the absence of external force, they will be unable to continue advancing . At this time, say the system is in deadlock state or system to produce a deadlock, which is always in the process of waiting for another process called the deadlock.

The following four conditions are necessary conditions of deadlock, indispensable , if a deadlock occurs all the following conditions must be met: the four states meet the state is unsafe, unsecure but not necessarily a deadlock occurs ;

1) exclusive conditions: refers to the process of the allocated resources to be exclusive use of a resource that is occupied by only one process at a time. If at this time there are other resources to process the request, the requestor can only wait until the share of resources used up in the process of release.
2) request and keeping conditions: refers to the process has to keep at least one resource, but proposed a new resource request, the resource has been occupied by other processes, this time requesting process blocked, but they have other resources available remain put.
3) does not deprive condition: Resource refers to the process have been obtained before is not used, can not be denied, can only be released by themselves when you are finished.
4) Loop wait condition: refers to a deadlock occurs, there is a necessary process - endless chain of resources, i.e., the process set {P0, P1, P2, ··· , Pn} of waiting for an P0 P1 occupies resources; P1 P2 is waiting for resources occupied, ......, Pn is waiting for a resource that has been occupied by P0.

2.2 avoid deadlock

Avoid deadlock is to break at least one of the four necessary conditions:

(1) break mutually exclusive conditions: American Express multiple processes simultaneously access (some resources are exclusive resources, such as printers, can not be shared)

(2) break request to maintain conditions: a one-time pre-allocate all the resources, if not all of the missing assignment.

(3) does not break deprivation: Allows a process to capture resources from occupants where, in fact, have the resources they have A, to apply for resource B, but without success, this time A release resources allocated to other processes, it is equivalent to other recessive process to seize the resources A.

(4) wait for conditions to break the loop: the orderly implementation of resource allocation. All resources to achieve numbers, from small to large numbers assigned in accordance with all procedures are to apply large resource plays trumpet in the premise of resources, so as not to form a loop.

 

2.3 deadlock avoidance algorithm

Security Sequence : Security sequence {P1, P2, ..., Pn } is composed of: if for each process Pi, it needs additional resources may be all processes Pj plus the current share of resources currently available resources of the system and satisfied, then {P1, P2, ..., Pn } of a security sequence, then the system in a safe state, would not enter into a deadlock state. ? ? ? ? Do not understand
Banker's algorithm : a banker has a certain amount of money, there are a number of loans to customers. Each customer must declare at the outset that he needed total loans. If the customer loans not to exceed the total number of funds bankers, bankers can receive customer requirements. Loans to customers is carried out every time a fund unit (such as 10,000 RMB, etc.) the way, all the units required by the customer in full amount before may wait, but it ensures that the bankers wait is limited, to be completed of.

For a state diagram, in accordance with the safety requirements of the sequence, we first selected a client should be required to meet the customers credit bankers less current remaining money, it can be seen that only the client C2 be satisfied: C2 Customer need a capital units, small bankers in the hands of two units of funds, so the bankers to a fund unit C2 by the customer, making the job done and return the money borrowed three units of funds into the b chart. Similarly, the four bankers to lend money units C3 clients, so the job done, c in the figure, only a client C1, it requires seven units of funds, the bankers then have eight units of funds, so C1 can successfully borrow money and complete the work. Finally (see Figure d) bankers to recover the full 10 money units, to ensure not to lose money. Then the customer sequence {C1, C2, C3} is a sequence of security, in accordance with the sequence of the loan, the banker is safe. Otherwise, if the state in Figure b, bankers put in the hands of four units of funds lent C1, an unsafe condition occurs: At this time C1, C3 can not complete the work, but no money in the hands of bankers, the system standoff, bankers can not recover the investment.

In summary, the banker's algorithm is starting from the current state, one at a security check sequence all customers who can complete its work, then assumed its completion and return of all loans, and then re-examine the next customer to complete the work , .. ..... If customers can complete all the work, then find a safe sequence, the banker is safe.

  From the above analysis shows, the banker's algorithm allowing exclusive conditions, occupancy and eligibility criteria, can not be seized conditions exist , so that, compared with the several methods that deadlock prevention, restriction less, degree of resource utilization improved .

This is the advantage of this algorithm. The disadvantages are:

   <1> This algorithm requires that the number of customers remains stationary , in a multiprogramming system, which is difficult to do.   

   <2> This algorithm ensures that all customers are met within a limited period of time , but real-time response to customer requirements quickly, so take this into account.  

    <3> Because looking for a safe sequence, actually increase the cost of the system .

2.4 Deadlock detection and recovery

2.4.1 Deadlock Detection

  A simple but no efficient way: There are two containers, one for the lock to save the thread is requesting a lock for saving thread has been held. Before each lock will do the following tests:

  1) detects whether the lock is currently being requested has been held by another thread, if any, put those threads to find out;

  2) traversing thread first step in returning to check whether they hold the lock being any one thread request. The second step is returned if true, it indicates that an deadlock.

  java class Thread in holdsLock () method.

2.4.2 Deadlock recovery

   Once a deadlock when the deadlock detection, it is necessary to eliminate the deadlock, the system to recover from a deadlock state.  

    (1) the simplest and most common method is to perform system restart , but this method costs a lot, it means that all the processes have been completed before the calculations are wasted, including participating in the deadlock of those processes, and the process is not participating in the deadlock.

    (2) revocation process, deprived of resources. Terminate the process involved in deadlock, to recover possession of their resources, to release the deadlock . Then they divided into two cases: one-time withdrawal of all processes involved in the deadlock, deprived of all resources; or phased out the process of participating in the deadlock, deadlock the process of gradual recovery resources possession. In general, the selection process should be phased out according to certain principles, aims to undo those minimum cost process, such as the cost of the process according to the priority determination process; consider the cost of running the process associated with this process and external the cost of operations and other factors. 

    In addition, the process fallback strategy , namely to make the process of participating in the deadlock fall back to a point before the deadlock does not occur, and thus continues at the point, in order to execute when the deadlock will not occur again. While this is an ideal solution, but extremely expensive system to operate, there must be change in the recording process every step of the stack of such institutions, so that future rollback, sometimes it can not be done.

Guess you like

Origin www.cnblogs.com/simpleDi/p/11518692.html