Chiefly task thread deadlock senior day09] + + bankers avoid deadlock algorithm

Deadlock

Reality, both men and women are waiting for the other to apologize

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-jr3gIvDK-1579699677994) (../ Images / 07day / Snip20160828_4.png)]

If both sides are so stubborn waiting for the other to speak first, calling it a sub-search

1. Deadlock

When multiple resources shared among threads, if two threads each occupy a portion of the resources and at the same time waiting for each other's resources, it will result in a deadlock.

Despite the deadlock rarely happens, but the event will cause the application to stop responding. See the following example of a deadlock

#coding=utf-8
import threading
import time

class MyThread1(threading.Thread):
    def run(self):
        # 对mutexA上锁
        mutexA.acquire()

        # mutexA上锁后,延时1秒,等待另外那个线程 把mutexB上锁
        print(self.name+'----do1---up----')
        time.sleep(1)

        # 此时会堵塞,因为这个mutexB已经被另外的线程抢先上锁了
        mutexB.acquire()
        print(self.name+'----do1---down----')
        mutexB.release()

        # 对mutexA解锁
        mutexA.release()

class MyThread2(threading.Thread):
    def run(self):
        # 对mutexB上锁
        mutexB.acquire()

        # mutexB上锁后,延时1秒,等待另外那个线程 把mutexA上锁
        print(self.name+'----do2---up----')
        time.sleep(1)

        # 此时会堵塞,因为这个mutexA已经被另外的线程抢先上锁了
        mutexA.acquire()
        print(self.name+'----do2---down----')
        mutexA.release()

        # 对mutexB解锁
        mutexB.release()

mutexA = threading.Lock()
mutexB = threading.Lock()

if __name__ == '__main__':
    t1 = MyThread1()
    t2 = MyThread2()
    t1.start()
    t2.start()

The result:
Run Results: [image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-oURieZDT-1579699677996) (../ Images / 07day / 2.gif)]
Here Insert Picture Description
At this point the state has entered into a deadlock, you can use ctrl-c to exit

2. Avoid Deadlock

  • Program design should try to avoid (the banker's algorithm)
  • Add timeout, etc.

Appendix - bankers algorithm

[background knowledge]

A banker how to secure a certain number of funds lent a number of customers so that those customers to borrow money to complete both wanted to do, but at the same time the bankers will not recover all funds go bankrupt, this is the problem banker. The problem with the operating system resource allocation problem is very similar: the bankers is like an operating system, customers like to run the process, is the system of funding bankers resources.

[Description of the problem]

Bankers have 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 example: There are three clients C1, C2, C3, borrowing from bankers, the bankers of the total funds of funds is 10 units, of which C1 clients to borrow funds 9 each unit, C2 clients to borrow funds unit 3, C3 to customers by eight units of funds, a total of 20 fund units. State at a certain time as shown in FIG.

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-jcBD807z-1579699677997) (../ Images / 07day / Snip20170319_9.png)]

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 lend a financial unit C2 customers, 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 status of each client individually check sequence by security 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.

Published 120 original articles · won praise 19 · views 3628

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104072769