![《哲学家进餐问题》word版.doc_第1页](http://file.renrendoc.com/FileRoot1/2019-1/12/9242131e-f675-4906-bf52-fc1a521f4b36/9242131e-f675-4906-bf52-fc1a521f4b361.gif)
![《哲学家进餐问题》word版.doc_第2页](http://file.renrendoc.com/FileRoot1/2019-1/12/9242131e-f675-4906-bf52-fc1a521f4b36/9242131e-f675-4906-bf52-fc1a521f4b362.gif)
![《哲学家进餐问题》word版.doc_第3页](http://file.renrendoc.com/FileRoot1/2019-1/12/9242131e-f675-4906-bf52-fc1a521f4b36/9242131e-f675-4906-bf52-fc1a521f4b363.gif)
![《哲学家进餐问题》word版.doc_第4页](http://file.renrendoc.com/FileRoot1/2019-1/12/9242131e-f675-4906-bf52-fc1a521f4b36/9242131e-f675-4906-bf52-fc1a521f4b364.gif)
![《哲学家进餐问题》word版.doc_第5页](http://file.renrendoc.com/FileRoot1/2019-1/12/9242131e-f675-4906-bf52-fc1a521f4b36/9242131e-f675-4906-bf52-fc1a521f4b365.gif)
已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
哲学家进餐问题2007/05/16 12:36 P.M./*philosophers.cpp哲学家进餐问题在多线程中如何避免死锁。问题描述:有五位哲学家围绕着餐桌坐,每一位哲学家要么思考要么等待,要么吃饭。为了吃饭,哲学家必须拿起两双筷子(分别放于左右两端)不幸的是,筷子的数量和哲学家相等,所以每只筷子必须由两位哲学家共享下面是一种有问题的解法,因为在某个时刻,五个哲学家同时拿起五根左手边的筷子,则它们会在同一时候对待右手边的筷子,这样会陷入死锁,但是我测试了,这样的几率并不高经过几个小时,还没有出现。但是我们可以肯定,理论上是肯定会出现死锁的,我们不能老是靠运气办事,怎么解决这个问题呢留给下一步的学习吧要编译此文件请用多线程版的c+库*/#include #include #include #include #include using namespace std;unsigned int _stdcall philosopher(void *);void thinking(int);void eating(int);void wait_to_eat(int);void outline(int ,const char *);/全局变量CRITICAL_SECTION crout;/这个变量用来保证输出时不会竞争CRITICAL_SECTION fork5;/定义五个临界变量,代表五更筷子int main(int argc,char *argv)void * hthread5;int i;unsigned int threadid5;int arg5;int count = 5;unsigned long retval;InitializeCriticalSection(&crout);/初始化临界变量for(i=0;i5;i+) InitializeCriticalSection(fork + i);/创建五个哲学家for(i = 0; i5;i+) argi = i; hthreadi = (void *)_beginthreadex(NULL,0,philosopher,(void *)(arg + i),0,threadid+i); if(int)hthreadi = -1)/如果线程创建失败返回-1 cerr error while create thread i endl; cerr error code : GetLastError() endl; /等待所有线程结束retval = WaitForMultipleObjects(5,hthread,true,INFINITE);/等待多个线程if(retval = WAIT_FAILED) cerr wait error,error code: GetLastError()endl;for(i = 0; i5;i+) if(CloseHandle(hthreadi) = false)/关闭句柄 cerr error while close thread iendl; cerr error code: GetLastError()endl; return 0;/*哲学家的行为吃饭,等待,思考*/unsigned int _stdcall philosopher(void *k)int n = (int *)k)0;outline(n, is in!);srand(time(NULL);while(true) thinking(n); wait_to_eat(n); eating(n);outline(n, is out!);return n;/*思考随机一段时间*/void thinking(int k)outline(k, is thinking.);Sleep(rand()%1000) *5);/*吃饭随机一段时间*/void eating(int k)outline(k, is eating.);Sleep(rand()%1000) *5);LeaveCriticalSection(fork + (k+1)%5);/放下右边的筷子/outline(k, give left);LeaveCriticalSection(fork + k);/放下左边的筷子/outline(k, give right);/*等待吃饭需要同时获得他两边的筷子*/void wait_to_eat(int k)outline(k, is waiting.);EnterCriticalSection(fork + k);/获得左边的筷子/outline(k, take left);EnterCriticalSection(fork + (k + 1)%5);/获得右边的筷子/outline(k, take right);/*/没有竞争条件的输出函数*/void outline(int who,const char *str)EnterCriticalSection(&crout);coutprocess whostrendl;LeaveCriticalSection(&crout);/*philosophers.cpp哲学家进餐问题在多线程中如何避免死锁。问题描述:有五位哲学家围绕着餐桌坐,每一位哲学家要么思考要么等待,要么吃饭。为了吃饭,哲学家必须拿起两双筷子(分别放于左右两端)不幸的是,筷子的数量和哲学家相等,所以每只筷子必须由两位哲学家共享下面是一种有问题的解法,因为在某个时刻,五个哲学家同时拿起五根左手边的筷子,则它们会在同一时候对待右手边的筷子,这样会陷入死锁,但是我测试了,这样的几率并不高经过几个小时,还没有出现。但是我们可以肯定,理论上是肯定会出现死锁的,我们不能老是靠运气办事,怎么解决这个问题呢留给下一步的学习吧要编译此文件请用多线程版的c+库WWW.54SH.COMMADE BY EFISH下面经过了一些修改,把哲学家的个数作为一个宏定义,并减少哲学家的个数为2, 死锁现象马上出现了,真不走运!*/#include #include #include #include #include #define NUM_OF_PH 2 /哲学家的个数using namespace std;unsigned int _stdcall philosopher(void *);void thinking(int);void eating(int);void wait_to_eat(int);void outline(int ,const char *);/全局变量CRITICAL_SECTION crout;/这个变量用来保证输出时不会竞争CRITICAL_SECTION forkNUM_OF_PH;/定义五个临界变量,代表五更筷子int main(int argc,char *argv)void * hthreadNUM_OF_PH;int i;unsigned int threadidNUM_OF_PH;int argNUM_OF_PH;int count = NUM_OF_PH;unsigned long retval;InitializeCriticalSection(&crout);/初始化临界变量for(i=0;iNUM_OF_PH;i+) InitializeCriticalSection(fork + i);/创建五个哲学家for(i = 0; iNUM_OF_PH;i+) argi = i; hthreadi = (void *)_beginthreadex(NULL,0,philosopher,(void *)(arg + i),0,threadid+i); if(int)hthreadi = -1)/如果线程创建失败返回-1 cerr error while create thread i endl; cerr error code : GetLastError() endl; /等待所有线程结束retval = WaitForMultipleObjects(NUM_OF_PH,hthread,true,INFINITE);/等待多个线程if(retval = WAIT_FAILED) cerr wait error,error code: GetLastError()endl;for(i = 0; iNUM_OF_PH;i+) if(CloseHandle(hthreadi) = false)/关闭句柄 cerr error while close thread iendl; cerr error code: GetLastError()endl; return 0;/*哲学家的行为吃饭,等待,思考*/unsigned int _stdcall philosopher(void *k)int n = (int *)k)0;outline(n, is in!);srand(time(NULL);while(true) thinking(n); wait_to_eat(n); eating(n);outline(n, is out!);return n;/*思考随机一段时间*/void thinking(int k)outline(k, is thinking.);Sleep(rand()%1000) *NUM_OF_PH);/*吃饭随机一段时间*/void eating(int k)outline(k, is eating.);Sleep(rand()%1000) *NUM_OF_PH);LeaveCriticalSection(fork + (k+1)%NUM_OF_PH);/放下右边的筷子outline(k, give left);LeaveCriticalSection(fork + k);/放下左边的筷子outline(k, give right);/*等待吃饭需要同时获得他两边的筷子*/void wait_to_eat(int k)outline(k, is waiting.);EnterCriticalSection(fork + k);/获得左边的筷子outline(k, take left);EnterCriticalSection(fork + (k + 1)%NUM_OF_PH);/获得右边的筷子outline(k, take right);/*/没有竞争条件的输出函数*/void outline(int who,const char *str)EnterCriticalSection(&crout);coutprocess whostrendl;LeaveCriticalSection(&crout);/*philosophers.cpp哲学家进餐问题在多线程中如何避免死锁。问题描述:有五位哲学家围绕着餐桌坐,每一位哲学家要么思考要么等待,要么吃饭。为了吃饭,哲学家必须拿起两双筷子(分别放于左右两端)不幸的是,筷子的数量和哲学家相等,所以每只筷子必须由两位哲学家共享下面是一种有问题的解法,因为在某个时刻,五个哲学家同时拿起五根左手边的筷子,则它们会在同一时候对待右手边的筷子,这样会陷入死锁,但是我测试了,这样的几率并不高经过几个小时,还没有出现。但是我们可以肯定,理论上是肯定会出现死锁的,我们不能老是靠运气办事,怎么解决这个问题呢留给下一步的学习吧要编译此文件请用多线程版的c+库WWW.54SH.COMMADE BY EFISH下面经过了一些修改,把哲学家的个数作为一个宏定义,并减少哲学家的个数为2死锁现象马上出现了,真不走运!进行了一些改变,把临界变量变成了互斥变量,让每一个哲学家同时等待每一个两更筷子*/#include #include #include #include #include #define NUM_OF_PH 3 /哲学家的个数using namespace std;unsigned int _stdcall philosopher(LPVOID);void thinking(int);void eating(int);void wait_to_eat(int);inline void outline(int ,const char *);/全局变量CRITICAL_SECTION crout;/这个变量用来保证输出时不会竞争/CRITICAL_SECTION forkNUM_OF_PH;/定义五个临界变量,代表五更筷子/修改之后,用了互斥变量两void *mutexNUM_OF_PH + 1;int main(int argc,char *argv)void * hthreadNUM_OF_PH;int i;unsigned int threadidNUM_OF_PH;int argNUM_OF_PH;int count = NUM_OF_PH;unsigned long retval;/ InitializeCriticalSection(&crout);/初始化临界变量for(i=0;iNUM_OF_PH;i+)/ InitializeCriticalSection(fork + i); mutexi = CreateMutex(NULL,false,NULL);/ mutexNUM_OF_PH = mutexNUM_OF_PH -1;/创建五个哲学家for(i = 0; iNUM_OF_PH;i+) argi = i; hthreadi = (void *)_beginthreadex(NULL,0,philosopher,(LPVOID)(arg + i),0,threadid+i); if(int)hthreadi = -1)/如果线程创建失败返回-1 cerr error while create thread i endl; cerr error code : GetLastError() endl; /等待所有线程结束retval = WaitForMultipleObjects(NUM_OF_PH,hthread,true,INFINITE);/等待多个线程if(retval = WAIT_FAILED) cerr wait error,error code: GetLastError()endl;for(i = 0; iNUM_OF_PH;i+) if(CloseHandle(hthreadi) = false)/关闭句柄 cerr error while close thread iendl; cerr error code: GetLastError()endl; return 0;/*哲学家的行为吃饭,等待,思考*/unsigned int _stdcall philosopher(LPVOID k)int n = *(int *) k;outline(n, is in!);srand(time(NULL);while(true) thinking(n); wait_to_eat(n); eating(n);outline(n, is out!);return n;/*思考随机一段时间*/void thinking(int k)outline(k, is thinking.);Sleep(rand()%1000) *NUM_OF_PH);/*吃饭随机一段时间*/void eatin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【正版授权】 IEC 60404-16:2018 EN-FR Magnetic materials - Part 16: Methods of measurement of the magnetic properties of Fe-based amorphous strip by means of a single sheet tester
- 【正版授权】 IEC 60384-20:2023 RLV EN Fixed capacitors for use in electronic equipment - Part 20: Sectional specification - Fixed metallized polyphenylene sulfide film dielectric surface mount DC
- 【正版授权】 IEC 60376:2018 EN Specification of technical grade sulphur hexafluoride (SF<sub>6</sub>) and complementary gases to be used in its mixtures for use in electrical equipment
- 【正版授权】 IEC 60335-2-97:2023 EN-FR Household and similar electrical appliances - Safety - Part 2-97: Particular requirements for drives for shutters,awnings,blinds and similar equipment
- 废钢买卖合同协议书
- 企业logo设计合同范本
- 罐头厂设备租赁合同范本
- 工厂质检合同范本
- 厨柜制作合同范本
- 卖房打包家具合同范本
- GB/T 6829-2024剩余电流动作保护电器的一般安全要求
- 2024年两家公司合作的协议书
- 2024年05月江苏省教育考试院公开招聘2人笔试历年高频考点(难、易错点)附带答案详解
- 2024年保密知识考试教育培训考试含完整答案【夺冠】
- 在线网课知道知慧《战舰与海战》单元测试答案
- 2024-2029年中国分红保险行业市场深度调研及竞争格局与投资前景研究报告
- 幼儿园学生学情分析报告总结
- 幼儿园中班学情分析报告
- 2024时事政治必考试题库及答案(必刷)
- 【单元检测】第三单元(B卷·能力篇)二年级语文下册单元分层训练AB卷(部编版)
- 2024年国家电投山西公司招聘公开引进高层次人才和急需紧缺人才笔试参考题库(共500题)答案详解版
评论
0/150
提交评论