// ■食事をする哲学者問題(dining philosophers problem) // (改良版)食事に失敗した哲学者を優先 //  大きな丸いテーブルを囲んで椅子に座った哲学者5人がいる。 //  中央にご飯が入った器があり, // 5本の箸が哲学者の脇に1本ずつ置いてある。 // 哲学者は両脇の箸を2本取れたときに食事ができる。 // 哲学者は食事をするか考えに耽るかして生きている。 // 哲学者は考えているとき隣と会話しない。 // 時々,空腹になり食事をし, // 食事が終わると箸をテーブルに戻して // 再び考え始める。 // #include "stdafx.h" #include "conio.h" #include "time.h" #include "stdlib.h" #define rand4() (int)(5*((double)rand()/(RAND_MAX+1))) int numP=0; int waitP[5]; int Ph[5]={1,1,1,1,1}; int Md[5]={0,0,0,0,0}; int _tmain(int argc, _TCHAR* argv[]) { srand((unsigned int ) time(NULL)); int i,i1,i2, iflag; while(getch()!=0x1A){ iflag=true; for(i=0;i<5;i++) { if(Md[i]!=0){ Ph[i]--; if(Ph[i]<=0){ printf("\n 哲学者 %d 食事終了 ",i); Md[i]=0;Ph[i]=4;iflag=false; } } } for(int k=0;k