哲學家就餐問題

2021-08-01 20:29:36 字數 782 閱讀 2122

假設有五位哲學家圍坐在一張圓形餐桌旁,做以下兩件事情之一:吃飯,或者思考。吃東西的時候,他們就停止思考,思考的時候也停止吃東西。餐桌中間有一大碗義大利面,每兩個哲學家之間有乙隻餐叉。因為用乙隻餐叉很難吃到義大利面,所以假設哲學家必須用兩隻餐叉吃東西。他們只能使用自己左右手邊的那兩隻餐叉。哲學家就餐問題有時也用公尺飯和筷子而不是義大利面和餐叉來描述,因為很明顯,吃公尺飯必須用兩根筷子。哲學家從來不交談,這就很危險,可能產生死鎖,每個哲學家都拿著左手的餐叉,永遠都在等右邊的餐叉(或者相反)。即使沒有死鎖,也有可能發生資源耗盡。

/**

* 筷子

*/public class chopstick

taken=true;

} public synchronized void

drop()

}

/**

* 哲學家

*/public class philosopher implements runnable

public

philosopher(chopstick left,chopstick right,int ident ,int ponder)

@override

public void

run()

} catch (interruptedexception e)

}public string tostring()

}

public class deadlockingdiningphilosophers 

}

哲學家就餐問題

本文是哲學家就餐問題在 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...

哲學家就餐問題

問題描述 哲學家就餐 c c 語言 author erice s binbin erices 163.com date 2017 10 21 include include include include define max 5 五個哲學家 define left i max 1 max 佇列處理...