《哲学家进餐问题》word版.doc_第1页
《哲学家进餐问题》word版.doc_第2页
《哲学家进餐问题》word版.doc_第3页
《哲学家进餐问题》word版.doc_第4页
《哲学家进餐问题》word版.doc_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论