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

2016-01-29 12:39 10 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");: : }

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

延伸阅读
标签: Web开发
var arr=['a','b','c']; 若要删除其中的'b',有两种方法: 1.delete方法:delete arr[1] 这种方式数组长度不变,此时arr[1]变为undefined了,但是也有好处原来数组的索引也保持不变,此时要遍历数组元素可以才用 for(index in arr) document.write('arr['+index+']='...
标签: 心理健康
问:人为什么要结婚? 经济学家答:1+1>2 。一是指1+1之后多出一个小孩,达到了婚姻的生育目的。二是指协同效应,原意是两家公司合并之后,如果重组得当,能够使资源得到更合理的配置,从而产生更大的效益。         问:做二奶好不好? 经济学家答:借壳上市看似是一条快速的途径,但...
标签: Web开发
首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下: u 红桃按照从小到大依次为:1-13 u 方块按照从小到大依次为:14-26 u 黑桃按照从小到大依次为:27-39 u 梅花按照从小到大依次为:40-52 u 小王为53,大王为54 算法实现如下: u 首先按照以上编号规则初始化一个包含108个数字的数组 u 每次随机从该数组中抽取一个数字,...
标签: 瑜伽 养生 健康
发源于古印度的瑜伽术,经历了几千年的延续之后,在今天渐渐成为一种颇受欢迎的健身方式。人们重新发掘瑜伽的神奇魅力,目的不再是追随古印度人那种苦行僧似的修行或修炼,选择瑜伽,在某种意义上,是选择了一种健康的生活方式,更确切的说,是选择了一种健康的生活哲学。  古老瑜伽受现代人青睐 瑜伽在今天拥有越来越多的追随...
处罚孩子的哲学 以前的传统家庭是:“家有一老,如有一宝” 现在的小家庭则是:“家有一小,如有一魔” 孩子一再犯错,家长该怎么办? 3岁的小庭很喜欢巴斯光年(电影《玩具总动员》中的角色),所以妈妈给他买了一个巴斯光年的玩具。有一次妈妈出门时把玩具放在桌子上,小庭想玩,可是桌子太高了,怎么也拿不着,这让她十分懊恼和沮丧。...

经验教程

223

收藏

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