哲學家就餐問題的兩種解法

2022-10-09 04:06:08 字數 2970 閱讀 8048

哲學家就餐問題可以這樣表述,假設有五位哲學家圍坐在一張圓形餐桌旁,做以下兩件事情之一:吃飯,或者思考。吃東西的時候,他們就停止思考,思考的時候也停止吃東西。餐桌中間有一大碗義大利面,每兩個哲學家之間有乙隻餐叉。因為用乙隻餐叉很難吃到義大利面,所以假設哲學家必須用兩隻餐叉吃東西。他們只能使用自己左右手邊的那兩隻餐叉。哲學家就餐問題有時也用公尺飯和筷子而不是義大利面和餐叉來描述,因為很明顯,吃公尺飯必須用兩根筷子。

哲學家從來不交談,這就很危險,可能產生死鎖,每個哲學家都拿著左手的餐叉,永遠都在等右邊的餐叉(或者相反)。即使沒有死鎖,也有可能發生資源耗盡。例如,假設規定當哲學家等待另乙隻餐叉超過五分鐘後就放下自己手裡的那乙隻餐叉,並且再等五分鐘後進行下一次嘗試。這個策略消除了死鎖(系統總會進入到下乙個狀態),但仍然有可能發生「活鎖

」。如果五位哲學家在完全相同的時刻進入餐廳,並同時拿起左邊的餐叉,那麼這些哲學家就會等待五分鐘,同時放下手中的餐叉,再等五分鐘,又同時拿起這些餐叉。

第一種:每次獲取筷子時,先檢查一下是否左右筷子都在,如果筷子齊全就獲取兩雙筷子,如不在則等待。

from threading import thread

from threading import lock

import time

​# 產生五隻筷子

chopsticka =  lock()

chopstickb =  lock()

chopstickc =  lock()

chopstickd =  lock()

chopsticke =  lock()​​

​def philosopher(name,leftchopstick,rightchopstick):

if not (leftchopstick.locked() andrightchopstick.locked()): # 如果沒有兩隻筷子,則等待筷子

print("%s is waiting" % (name))

time.sleep(0.01)

else:

leftchopstick.acquire()

rightchopstick.acquire()

print("%s is eating"%(name))

time.sleep(2)

print("%s is over"%(name))

leftchopstick.release()

rightchopstick.release()​​

if __name__ == '__main__':

thread(target=philosopher,args=('philosophera',chopsticka,chopstickb)).start()

thread(target=philosopher,args=('philosopherb',chopstickb,chopstickc)).start()

thread(target=philosopher,args=('philosopherc',chopstickc,chopstickd)).start()

thread(target=philosopher,args=('philosopherd',chopstickd,chopsticke)).start()

thread(target=philosopher,args=('philosophere',chopsticke,chopsticka)).start()

第二種:設定訊號量為4,每次只能有4個人同時獲取筷子,這樣總能有乙個哲學家獲得兩雙筷子

from threading import thread

from threading import semaphore, lock

import time

​# 建立5個筷子

chopsticka =  lock()

chopstickb =  lock()

chopstickc =  lock()

chopstickd =  lock()

chopsticke =  lock()

​# 至多允許4個人同時搶鎖

count = semaphore(4)​​

def philosopher(name,leftchopstick,rightchopstick):

while true:

# 搶鎖之前先獲取資格。

count.acquire()

leftchopstick.acquire()

rightchopstick.acquire()

print("%s is eating" % (name))

time.sleep(1)

print("%s is over" % (name))

leftchopstick.release()

rightchopstick.release()

count.release()

​if __name__ == '__main__':

thread(target=philosopher,args=('philosophera',chopsticka,chopstickb)).start()

thread(target=philosopher,args=('philosopherb',chopstickb,chopstickc)).start()

thread(target=philosopher,args=('philosopherc',chopstickc,chopstickd)).start()

thread(target=philosopher,args=('philosopherd',chopstickd,chopsticke)).start()

thread(target=philosopher,args=('philosophere',chopsticke,chopsticka)).start()

兩種辦法哲學家e總是搶不到筷子,不知道為什麼

哲學家就餐問題

本文是哲學家就餐問題在 linux 上的程式實現,與windows 平台的實現類似,程式上稍有不同。philosopherdining.cpp include include include include include include rasutil.h using namespace std ...

哲學家就餐問題

pragma once include include include include include include include include include include include include include stdafx.h handle chopstick 5 room l...

哲學家就餐問題

假設有五位哲學家圍坐在一張圓形餐桌旁,做以下兩件事情之一 吃飯,或者思考。吃東西的時候,他們就停止思考,思考的時候也停止吃東西。餐桌中間有一大碗義大利面,每兩個哲學家之間有乙隻餐叉。因為用乙隻餐叉很難吃到義大利面,所以假設哲學家必須用兩隻餐叉吃東西。他們只能使用自己左右手邊的那兩隻餐叉。哲學家就餐問...