算法讨论:哲学家就餐问题

2016-01-29 12:39 4 1 收藏

算法讨论:哲学家就餐问题,算法讨论:哲学家就餐问题

【 tulaoshi.com - ASP.NET 】

In 1965, Dijkstra posed and solved a synchronization problem he called thedining philosophers problem. ........ The problem can be stated quite simplyas follows. Five philosophers are seated around a circular table. Each philosopher has a plate of spaghetti. The spaghetti is so slippery that a philosopher needs two forks to eat it. Between each pair of plates is one fork. The life of a philosopher consists of alternate periods of eating and thinking. When a philosopher gets hungry, she tries to acquire her left and rightfork, one at a time, in either order. If successful in acquiring two forks,she eats for a while, then puts down the forks and continues to think. Thekey question is: Can you write a program for each philosopher that does what it is supposed to do and never gets stuck? --from written by Andrew S. Tanenbaum typed by foolball :-PProgramme provided by ya: : 法一: 用公共文件,按照严格轮流执行: : #include : : #include : : #include : : #include : : #define N 5: : int i,j,t,status;: : FILE * f;: : char *state[N];: : main(): : {: : f=fopen("/share","w+");: : putc(j,f);: : if (fork()): : { if (fork()): : { if (fork()): : { if (fork()): : { if (fork()): : {waitpid(-1,*status,0);: : fclose(f);}: : else : : philosophy(4);}: : else: : philosophy(3);}: : else: : philosophy(2);}: : else: : philosophy(1);}: : else: : philosophy(0);: : }: : void philosophy(int i): : {: : state[i ="thinking";: : printf("%d%sn",i,"is thinking");: : for(t=0;t<=rand()+10000;t++);: : state[i ="hungry";: : printf("%d%sn",i,"is hungry");: : for(t=0;t<=rand()+10000;t++);: : for(;i!=j;): : {fseek(f,0l,0);: : j=getc(f);} : : state[i ="eating";: : printf("%d%sn",i,"is eating");: : j=(j+1)% N: : fseek(f,0l,0);: : putc(j,f);: : }: : 法二:通过文件加锁实现: : #include : : #include : : #define N 4: : FILE *f;: : int i, status;: : char *state[N];: : void philosofy(int i);: : void main(): : {: : if ((f=fopen("turn", "w+"))==NULL): : {: : printf("Cann't open this file"); : : exit(0);: : }: : if (fork()): : {: : if(fork()): : {: : if(fork()): : {: : if(fork()): : {: : if(fork()): : {: : waitpid(-1, &status, 0);: : fclose(f);: : }: : else: : philosofy(4);: : }: : else: : philosofy(3);: : }: : else : : philosofy(2);: : }: : else: : philosofy(1);: : }: : else: : philosofy(0);: : }//end of main: : void philosophy(int i): : { int t;: : state[i ="thinking";: : printf("%d%sn",i,"is thinking");: : for(t=0;t<=rand()+10000;t++);: : state[i ="hungry";: : printf("%d%sn",i,"is hungry");: : for(t=0;t<=rand()+10000;t++);: : while ((f=fopen("turn.lock","r"))!=NULL);: : link ("turn","turn.lock");: : state[i ="eating"; : : printf("%d%sn",i," is eating");: : for(t=1; t<=10000+rand();t++) ;: : unlink ("turn");: : }

来源:https://www.tulaoshi.com/n/20160129/1487014.html

延伸阅读
标签: 瑜伽 养生 健康
瑜伽的呼吸是瑜伽练习的重要部分,也是瑜伽练习能否收到效益的关键所在。呼吸是联系人和生理与精神的纽带,正确的瑜伽练习必须先从呼吸的练习开始而不应先从体位法开始。但是这个时代的功利思想使得人们太容易忽略内在的训练而易流于表面的展示,所以体位法的练习兴盛而呼吸法反而不被重视,这是很严重的方向上的错误。下面就很多朋友要求了解的...
标签: 心理健康
问:人为什么要结婚? 经济学家答:1+1>2 。一是指1+1之后多出一个小孩,达到了婚姻的生育目的。二是指协同效应,原意是两家公司合并之后,如果重组得当,能够使资源得到更合理的配置,从而产生更大的效益。         问:做二奶好不好? 经济学家答:借壳上市看似是一条快速的途径,但...
标签: 瑜伽 养生 健康
发源于古印度的瑜伽术,经历了几千年的延续之后,在今天渐渐成为一种颇受欢迎的健身方式。人们重新发掘瑜伽的神奇魅力,目的不再是追随古印度人那种苦行僧似的修行或修炼,选择瑜伽,在某种意义上,是选择了一种健康的生活方式,更确切的说,是选择了一种健康的生活哲学。  古老瑜伽受现代人青睐 瑜伽在今天拥有越来越多的追随...
标签: Web开发
首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下: u 红桃按照从小到大依次为:1-13 u 方块按照从小到大依次为:14-26 u 黑桃按照从小到大依次为:27-39 u 梅花按照从小到大依次为:40-52 u 小王为53,大王为54 算法实现如下: u 首先按照以上编号规则初始化一个包含108个数字的数组 u 每次随机从该数组中抽取一个数字,...
已知一棵二叉树按顺序方式存储在数组 A[1..n]中。设计算法求出下标分别为 i 和 j 的两个结点的最近的公共祖先结点的值。 武汉大学2000年第五(1)题(8’) #inlcude stdio.h parent(int A[],int i,int j) {int k,m; m=k=0; if(i==1j==1A[i]==0A[j]==0i==j) // A[i]==0或A[j]==0表示不存在该结点 ...

经验教程

505

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部