国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中死鎖的形成示例及死鎖情況的防止

2019-11-25 16:43:49
字體:
來源:轉載
供稿:網友

死鎖示例
搞多線程的經常會遇到死鎖的問題,學習操作系統的時候會講到死鎖相關的東西,我們用Python直觀的演示一下。
死鎖的一個原因是互斥鎖。假設銀行系統中,用戶a試圖轉賬100塊給用戶b,與此同時用戶b試圖轉賬200塊給用戶a,則可能產生死鎖。
2個線程互相等待對方的鎖,互相占用著資源不釋放。

#coding=utf-8 import time import threading class Account:   def __init__(self, _id, balance, lock):     self.id = _id     self.balance = balance     self.lock = lock    def withdraw(self, amount):     self.balance -= amount    def deposit(self, amount):     self.balance += amount   def transfer(_from, to, amount):   if _from.lock.acquire():#鎖住自己的賬戶     _from.withdraw(amount)     time.sleep(1)#讓交易時間變長,2個交易線程時間上重疊,有足夠時間來產生死鎖     print 'wait for lock...'     if to.lock.acquire():#鎖住對方的賬戶       to.deposit(amount)       to.lock.release()     _from.lock.release()   print 'finish...'  a = Account('a',1000, threading.Lock()) b = Account('b',1000, threading.Lock()) threading.Thread(target = transfer, args = (a, b, 100)).start() threading.Thread(target = transfer, args = (b, a, 200)).start() 

防止死鎖的加鎖機制
問題:
你正在寫一個多線程程序,其中線程需要一次獲取多個鎖,此時如何避免死鎖問題。
解決方案:
在多線程程序中,死鎖問題很大一部分是由于線程同時獲取多個鎖造成的。舉個例子:一個線程獲取了第一個鎖,然后在獲取第二個鎖的 時候發生阻塞,那么這個線程就可能阻塞其他線程的執行,從而導致整個程序假死。 解決死鎖問題的一種方案是為程序中的每一個鎖分配一個唯一的id,然后只允許按照升序規則來使用多個鎖,這個規則使用上下文管理器 是非常容易實現的,示例如下:

import threadingfrom contextlib import contextmanager# Thread-local state to stored information on locks already acquired_local = threading.local()@contextmanagerdef acquire(*locks):  # Sort locks by object identifier  locks = sorted(locks, key=lambda x: id(x))  # Make sure lock order of previously acquired locks is not violated  acquired = getattr(_local,'acquired',[])  if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):    raise RuntimeError('Lock Order Violation')  # Acquire all of the locks  acquired.extend(locks)  _local.acquired = acquired  try:    for lock in locks:      lock.acquire()    yield  finally:    # Release locks in reverse order of acquisition    for lock in reversed(locks):      lock.release()    del acquired[-len(locks):]

如何使用這個上下文管理器呢?你可以按照正常途徑創建一個鎖對象,但不論是單個鎖還是多個鎖中都使用 acquire() 函數來申請鎖, 示例如下:

import threadingx_lock = threading.Lock()y_lock = threading.Lock()def thread_1():  while True:    with acquire(x_lock, y_lock):      print('Thread-1')def thread_2():  while True:    with acquire(y_lock, x_lock):      print('Thread-2')t1 = threading.Thread(target=thread_1)t1.daemon = Truet1.start()t2 = threading.Thread(target=thread_2)t2.daemon = Truet2.start()

如果你執行這段代碼,你會發現它即使在不同的函數中以不同的順序獲取鎖也沒有發生死鎖。 其關鍵在于,在第一段代碼中,我們對這些鎖進行了排序。通過排序,使得不管用戶以什么樣的順序來請求鎖,這些鎖都會按照固定的順序被獲取。 如果有多個 acquire() 操作被嵌套調用,可以通過線程本地存儲(TLS)來檢測潛在的死鎖問題。 假設你的代碼是這樣寫的:

import threadingx_lock = threading.Lock()y_lock = threading.Lock()def thread_1():  while True:    with acquire(x_lock):      with acquire(y_lock):        print('Thread-1')def thread_2():  while True:    with acquire(y_lock):      with acquire(x_lock):        print('Thread-2')t1 = threading.Thread(target=thread_1)t1.daemon = Truet1.start()t2 = threading.Thread(target=thread_2)t2.daemon = Truet2.start()

如果你運行這個版本的代碼,必定會有一個線程發生崩潰,異常信息可能像這樣:

Exception in thread Thread-1:Traceback (most recent call last): File "/usr/local/lib/python3.3/threading.py", line 639, in _bootstrap_inner  self.run() File "/usr/local/lib/python3.3/threading.py", line 596, in run  self._target(*self._args, **self._kwargs) File "deadlock.py", line 49, in thread_1  with acquire(y_lock): File "/usr/local/lib/python3.3/contextlib.py", line 48, in __enter__  return next(self.gen) File "deadlock.py", line 15, in acquire  raise RuntimeError("Lock Order Violation")RuntimeError: Lock Order Violation>>>

發生崩潰的原因在于,每個線程都記錄著自己已經獲取到的鎖。 acquire() 函數會檢查之前已經獲取的鎖列表, 由于鎖是按照升序排列獲取的,所以函數會認為之前已獲取的鎖的id必定小于新申請到的鎖,這時就會觸發異常。

討論
死鎖是每一個多線程程序都會面臨的一個問題(就像它是每一本操作系統課本的共同話題一樣)。根據經驗來講,盡可能保證每一個 線程只能同時保持一個鎖,這樣程序就不會被死鎖問題所困擾。一旦有線程同時申請多個鎖,一切就不可預料了。

死鎖的檢測與恢復是一個幾乎沒有優雅的解決方案的擴展話題。一個比較常用的死鎖檢測與恢復的方案是引入看門狗計數器。當線程正常 運行的時候會每隔一段時間重置計數器,在沒有發生死鎖的情況下,一切都正常進行。一旦發生死鎖,由于無法重置計數器導致定時器 超時,這時程序會通過重啟自身恢復到正常狀態。

避免死鎖是另外一種解決死鎖問題的方式,在進程獲取鎖的時候會嚴格按照對象id升序排列獲取,經過數學證明,這樣保證程序不會進入 死鎖狀態。證明就留給讀者作為練習了。避免死鎖的主要思想是,單純地按照對象id遞增的順序加鎖不會產生循環依賴,而循環依賴是 死鎖的一個必要條件,從而避免程序進入死鎖狀態。

下面以一個關于線程死鎖的經典問題:“哲學家就餐問題”,作為本節最后一個例子。題目是這樣的:五位哲學家圍坐在一張桌子前,每個人 面前有一碗飯和一只筷子。在這里每個哲學家可以看做是一個獨立的線程,而每只筷子可以看做是一個鎖。每個哲學家可以處在靜坐、 思考、吃飯三種狀態中的一個。需要注意的是,每個哲學家吃飯是需要兩只筷子的,這樣問題就來了:如果每個哲學家都拿起自己左邊的筷子, 那么他們五個都只能拿著一只筷子坐在那兒,直到餓死。此時他們就進入了死鎖狀態。 下面是一個簡單的使用死鎖避免機制解決“哲學家就餐問題”的實現:

import threading# The philosopher threaddef philosopher(left, right):  while True:    with acquire(left,right):       print(threading.currentThread(), 'eating')# The chopsticks (represented by locks)NSTICKS = 5chopsticks = [threading.Lock() for n in range(NSTICKS)]# Create all of the philosophersfor n in range(NSTICKS):  t = threading.Thread(target=philosopher,             args=(chopsticks[n],chopsticks[(n+1) % NSTICKS]))  t.start()

最后,要特別注意到,為了避免死鎖,所有的加鎖操作必須使用 acquire() 函數。如果代碼中的某部分繞過acquire 函數直接申請鎖,那么整個死鎖避免機制就不起作用了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台江县| 志丹县| 华容县| 融水| 大冶市| 西乌| 遂昌县| 雷山县| 齐河县| 资阳市| 文水县| 上高县| 黄平县| 南部县| 嘉义县| 故城县| 广饶县| 鹤峰县| 金乡县| 临武县| 蒙城县| 邵阳市| 商洛市| 德阳市| 塔河县| 景泰县| 苍南县| 奎屯市| 玉山县| 都兰县| 奈曼旗| 万荣县| 当阳市| 兴仁县| 郓城县| 鄂托克前旗| 建始县| 凤翔县| 闵行区| 安庆市| 蒙阴县|