Python實現哲學家就餐問題

2021-10-10 08:42:33 字數 3762 閱讀 4613

哲學家就餐問題是典型的同步問題,該問題描述的是五個哲學家共用一張圓桌,分別坐在五張椅子上,在圓桌上有五個盤子和五個叉子(如下圖),他們的生活方式是交替的進行思考和進餐,思考時不能用餐,用餐時不能思考。平時,乙個哲學家進行思考,飢餓時便試圖用餐,只有在他同時拿到他的盤子左右兩邊的兩個叉子時才能進餐。進餐完畢後,他會放下叉子繼續思考。請寫出**來解決如上的哲學家就餐問題,要求**返回「當每個哲學家分別需要進食 n 次」時這五位哲學家具體的行為記錄。

輸入:n = 1 (1<=n<=60,n 表示每個哲學家需要進餐的次數。)

[[4,2,1],[4,1,1],[0,1,1],[2,2,1],[2,1,1],[2,0,3],[2,1,2],[2,2,2],[4,0,3],[4,1,2],[0,2,1],[4,2,2],[3,2,1],[3,1,1],[0,0,3],[0,1,2],[0,2,2],[1,2,1],[1,1,1],[3,0,3],[3,1,2],[3,2,2],[1,0,3],[1,1,2],[1,2,2]]

輸出列表中的每乙個子列表描述了某個哲學家的具體行為,它的格式如下:

output[i] = [a, b, c] (3 個整數)

import queue

import threading

import time

import random

class countdownlatch:

def __init__(self, count):

self.count = count

self.condition = threading.condition()

def wait(self):

try:

self.condition.acquire()

while self.count > 0:

self.condition.wait()

finally:

self.condition.release()

def count_down(self):

try:

self.condition.acquire()

self.count -= 1

self.condition.notifyall()

finally:

self.condition.release()

class diningphilosophers(threading.thread):

def __init__(self, philosopher_number, left_fork, right_fork, operate_queue, count_latch):

super().__init__()

self.philosopher_number = philosopher_number

self.left_fork = left_fork

self.right_fork = right_fork

self.operate_queue = operate_queue

self.count_latch = count_latch

def eat(self):

time.sleep(0.01)

self.operate_queue.put([self.philosopher_number, 0, 3])

def think(self):

time.sleep(random.random())

def pick_left_fork(self):

self.operate_queue.put([self.philosopher_number, 1, 1])

def pick_right_fork(self):

self.operate_queue.put([self.philosopher_number, 2, 1])

def put_left_fork(self):

self.left_fork.release()

self.operate_queue.put([self.philosopher_number, 1, 2])

def put_right_fork(self):

self.right_fork.release()

self.operate_queue.put([self.philosopher_number, 2, 2])

def run(self):

while true:

left = self.left_fork.acquire(blocking=false)

right = self.right_fork.acquire(blocking=false)

if left and right:

self.pick_left_fork()

self.pick_right_fork()

self.eat()

self.put_left_fork()

self.put_right_fork()

break

elif left and not right:

self.left_fork.release()

elif right and not left:

self.right_fork.release()

else:

time.sleep(0.01)

print(str(self.philosopher_number) + ' count_down')

self.count_latch.count_down()

if __name__ == '__main__':

operate_queue = queue.queue()

fork1 = threading.lock()

fork2 = threading.lock()

fork3 = threading.lock()

fork4 = threading.lock()

fork5 = threading.lock()

n = 1

latch = countdownlatch(5 * n)

for _ in range(n):

philosopher0 = diningphilosophers(0, fork5, fork1, operate_queue, latch)

philosopher0.start()

philosopher1 = diningphilosophers(1, fork1, fork2, operate_queue, latch)

philosopher1.start()

philosopher2 = diningphilosophers(2, fork2, fork3, operate_queue, latch)

philosopher2.start()

philosopher3 = diningphilosophers(3, fork3, fork4, operate_queue, latch)

philosopher3.start()

philosopher4 = diningphilosophers(4, fork4, fork5, operate_queue, latch)

philosopher4.start()

latch.wait()

queue_list =

for i in range(5 * 5 * n):

print(queue_list)

哲學家就餐問題

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

哲學家就餐問題

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