哲学家就餐问题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验一一、实验名称:哲学家就餐问题的实现二、实验学时:2三、实验内容和目的:实验目的:实现哲学家就餐问题,要求不能出现死锁。通过本实验熟悉Linux系统的基本环境,了解Linux下进程和线程的实现。实验内容:在Unix系统下实现教材2.4.2节中所描述的哲学家就餐问题。要求显示出每个哲学家的工作状态,如吃饭,思考。连续运行30次以上都未出现死锁现象。四、实验原理:由Dijkstra提出并解决的哲学家进餐问题(TheDinningPhilosophersProblem)是典型的同步问题。该问题是描述有五个哲学家共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是交替地进行思考和进餐。平时,一个哲学家进行思考,饥饿时便试图取用其左右最靠近他的筷子,只有在他拿到两只筷子时才能进餐。进餐完毕,放下筷子继续思考。五、实验器材(设备、元器件)(1)学生每人一台PC,安装WindowsXP/2000操作系统。(2)局域网络环境。(3)个人PC安装VMware虚拟机和Ubuntu系统。六、实验内容:(一)熟悉Ubuntu系统下的多线程编程。(二)实现哲学家就餐问题1.算法思想规定奇数号哲学家先拿他左边的筷子,然后再去拿右边的筷子,而偶数号哲学家则相反。按此规定,将是1、2号哲学家竞争1号筷子;3、4号哲学家竞争3号筷子。即五位哲学家都生竞争奇数号筷子,获得后,再去竞争偶数号筷子,最后总会有一位哲学家能获得两只筷子而进餐。2.流程图否否是是否是开始正在等待拿起左右手筷子放下左右手筷子状态为进餐状态为思考正在思考正在进餐结束状态为等待3.程序代码(重要代码请注释)#includestdio.h#includepthread.h#includesemaphore.h#includestdlib.h#includestring.h#defineNOC5//numberofchopstic#defineNOP5//numberofphilosophersem_tchopstic[NOC];//semaphoreintflag[5];//philosopher'sstatusvoid*eat(inti){intposition;inttemp=0;intj=(i+1)%NOC;position=i%2;while(1){if(position==0){//oddtakeleftfirstsem_wait(&chopstic[i]);printf(philosopher%dget%d\n,i,i);sem_wait(&chopstic[j]);printf(philosopher%dget%d\n,i,j);flag[i]=1;//philosopheriseatingprintf(waitting:);//printothers'statuswhile(temp5){if(!flag[temp])printf(philosopher%d\t,temp);temp++;}temp=0;printf(\n);printf(eating:);//printothers'statuswhile(temp5){if(flag[temp])printf(philosopher%d\t,temp);temp++;}printf(\n\n);temp=0;//printf(\nphilosopher%diseating\n\n,i);sleep(2);flag[i]=0;printf(philosopher%dput%d\n,i,i);sem_post(&chopstic[i]);printf(philosopher%dput%d\n,i,j);sem_post(&chopstic[j]);}else{//eventakerightfirstsem_wait(&chopstic[j]);printf(philosopher%dget%d\n,i,j);sem_wait(&chopstic[i]);printf(philosopher%dget%d\n,i,i);flag[i]=1;printf(waitting:);while(temp5){if(!flag[temp])printf(philosopher%d\t,temp);temp++;}temp=0;printf(\n);printf(eating:);while(temp5){if(flag[temp])printf(philosopher%d\t,temp);temp++;}printf(\n\n);temp=0;//printf(\nphilosopher%diseating\n\n,i);sleep(2);flag[i]=0;printf(philosopher%dput%d\n,i,j);sem_post(&chopstic[j]);printf(philosopher%dput%d\n,i,i);sem_post(&chopstic[i]);}}}intmain(void){inti=0;interror;pthread_tphilosopher[NOP];//initsemwhile(i5){flag[i]=0;sem_init(&chopstic[i],0,1);i++;}i=0;//createthreadwhile(i5){error=pthread_create(&philosopher[i],NULL,(void*)eat,(void*)i);if(error){printf(error:createthreadfailed!!\n);exit(0);}i++;}i=0;//destroythreadwhile(i5){pthread_join(philosopher[i],NULL);i++;}i=0;//destroysemwhile(i5){sem_destroy(&chopstic[i]);i++;}return0;}七、实验及结果分析:运行结果:udbwxfso@ubuntu:~/Desktop/sy2$gccgphilosopher.c-pthreadudbwxfso@ubuntu:~/Desktop/sy2$./a.outphilosopher4get4philosopher4get0waitting:philosopher0philosopher1philosopher2philosopher3eating:philosopher4philosopher2get2philosopher2get3waitting:philosopher0philosopher1philosopher3eating:philosopher2philosopher4philosopher4put4philosopher4put0philosopher4get4philosopher4get0waitting:philosopher0philosopher1philosopher3eating:philosopher2philosopher4philosopher2put2philosopher2put3philosopher2get2philosopher2get3waitting:philosopher0philosopher1philosopher3eating:philosopher2philosopher4philosopher4put4philosopher3get4philosopher4put0philosopher0get0philosopher0get1waitting:philosopher1philosopher3philosopher4eating:philosopher0philosopher2philosopher2put2philosopher2put3philosopher2get2philosopher2get3waitting:philosopher1philosopher3philosopher4eating:philosopher0philosopher2philosopher2put2philosopher2put3philosopher2get2philosopher2get3waitting:philosopher1philosopher3philosopher4eating:philosopher0philosopher2经分析可知,放在桌子上的筷子是临界资源,在一段时间内只允许一位哲学家使用,为了实现对筷子的互斥使用,用一个信号量表示一只筷子,由这五个信号量组成信号量数组。若哲学家饥饿时总是先拿其左边的筷子,成功后,再去拿右边的筷子,容易造成死锁。当规定奇数号哲学家先拿起右边筷子,然后再去拿左边筷子,而偶数号哲学家则相反后,死锁现象得到了解决。八、实验结论、心得体会和改进建议:通过这次操作系统的实验,我了解到了进程的创建及进行,明白了死锁产生的原因和一些解决的办法,这让我深刻的体会到操作系统的知识作用。而且此次实验还培养了我综合运用知识的能力,结合了C语言的知识来解决这个问题。巩固了以前的知识。在设计这个程序的时候我遇到了很多困难,如进程函数的运用,哲学家之间的协调,还有死锁问题的解决。在老师及同学的帮助下,我解决掉这些问题,并完成了这个实验。在实验的进行中,我发现了我很多学习中的不足之处,对以前学习的知识认识不够深刻,掌握的不够,这个要好好复习一下。总的来说,这次的实验带给我很多收获,让我受益匪浅。

1 / 8
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功