哲學家就餐

2022-02-24 19:35:45 字數 723 閱讀 4530

#include

#include

#include

#include

#include

#include

//筷子作為mutex

pthread_mutex_t chopstick[6] ;

void *eat_think(void *arg)

int i;

for(;;)

//    pthread_mutex_lock(&chopstick[right]); //拿起右手的筷子,如果想觀察死鎖,把上一句if注釋掉,再把這一句的注釋去掉

printf("philosopher %c fetches chopstick %d\n", phi, right);

printf("philosopher %c is eating.\n",phi);

usleep(3); //吃飯

pthread_mutex_unlock(&chopstick[left]); //放下左手的筷子

printf("philosopher %c release chopstick %d\n", phi, left);

pthread_mutex_unlock(&chopstick[right]); //放下左手的筷子

printf("philosopher %c release chopstick %d\n", phi, right);}}

int main()

哲學家就餐問題

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

哲學家就餐問題

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