Linux环境下实现哲学家就餐问题

Linux环境下实现哲学家就餐问题,第1张

概述#include <stdio.h>#include <stdlib.h>#include <memory.h>#include <pthread.h>#include <errno.h>#include <math.h>//筷子作为mutexpthread_mutex_t chopstick[6] ;//定义以筷子为锁的数组void *eat_think(void *arg){char phi = *(char *)arg; //将任意类型的指针*arg转化为*char类型int left,right; //左右筷子的编号switch (phi){case 'A':left = 5;right = 1;break;case 'B':left = 1;right = 2;break;case 'C':left = 2;right = 3;break;case 'D':left = 3;right = 4;break;case 'E':left = 4;right = 5;break;}int i;for(;;){usleep(3); //思考,将进程挂起一段时间pthread_mutex_lock(&chopstick[left]); //拿起左手的筷子,锁定互斥量,加锁printf("Philosopher %c fetches chopstick %dn", phi, left);输出哲学家拿起了左手边的筷子if (pthread_mutex_trylock(&chopstick[right]) == EBUSY){ //判断右手的筷子是否有人用, 再试一次获得对互斥量的锁定(非阻塞)pthread_mutex_unlock(&chopstick[left]); //如果右边筷子被拿走放下左手的筷子,解锁互斥量,解锁continue;//如果此哲学家没能吃饭,验证下一个哲学家是否能吃饭,即跳出本次循环进行下次循环}// pthread_mutex_lock(&chopstick[right]); //拿起右手的筷子,如果想观察死锁,把上一句if注释掉,再把这一句的注释去掉printf("Philosopher %c fetches chopstick %dn", phi, right); //输出此哲学家又拿起了右手边的跨子printf("Philosopher %c is eating.n",phi);//输出此次的哲学家拿起啦一双筷子在吃饭usleep(3); //吃饭,把进程挂起一段时间pthread_mutex_unlock(&chopstick[left]); //放下左手的筷子printf("Philosopher %c release chopstick %dn", phi, left);pthread_mutex_unlock(&chopstick[right]); //放下右手的筷子printf("Philosopher %c release chopstick %dn", phi, right);}}int main(){pthread_t A,B,C,D,E; //5个哲学家int i;for (i = 0; i < 5; i++)pthread_mutex_init(&chopstick[i],NULL);//初始化默认互斥锁属性的互斥锁数组chopstick[i],默认属性为快速互斥锁pthread_create(&A,NULL, eat_think, "A");////创建并跳转到线程函数创建并跳转到参数为指向线程标识符的指针为 A 线程函数eat_think,A是运行函数的参数pthread_create(&B,NULL, eat_think, "B");pthread_create(&C,NULL, eat_think, "C");pthread_create(&D,NULL, eat_think, "D");pthread_create(&E,NULL, eat_think, "E");pthread_join(A,NULL);//等待线程标识符为 A 的eat_think线程函数结束pthread_join(B,NULL);pthread_join(C,NULL);pthread_join(D,NULL);pthread_join(E,NULL);return 0;} 

<div >

#include pthread_mutex_t chopstick[] ; *eat_think( * phi = *( *)arg;      left,right;      = =  = =  = =  = =  = = </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; i;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;(;;){    usleep(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;3</span>); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;思考,将进程挂起一段<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a></span>    pthread_mutex_lock(&amp;chopstick[<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>]); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;拿起左手的筷子,锁定互斥量,加锁</span>    printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Philosopher %c fetches chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,phi,<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>);<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" >输出</a>哲学家拿起了左手边的筷子    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;if</span> (pthread_mutex_trylock(&amp;chopstick[right]) == EBUSY){ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;判断右手的筷子是否有人用,再试一次获得对互斥量的锁定(非阻塞)    </span>        pthread_mutex_unlock(&amp;chopstick[<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>]); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;如果右边筷子被拿走放下左手的筷子,解锁互斥量,解锁</span>        <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;continue</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;如果此哲学家没能吃饭,验证下<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>哲学家是否能吃饭,即跳出本次循环<a href="https://m.jb51.cc/tag/jinxing/" target="_blank" >进行</a>下次循环</span>

<span > }

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;    pthread_mutex_lock(&amp;chopstick[right]); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;拿起右手的筷子,如果想观察死锁,把上一句if<a href="https://m.jb51.cc/tag/zhushi/" target="_blank" >注释</a>掉,再把这一句的<a href="https://m.jb51.cc/tag/zhushi/" target="_blank" >注释</a>去掉</span>    printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Philosopher %c fetches chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>,right); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" >输出</a>此哲学家又拿起了右手边的跨子</span>    printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Philosopher %c is eating.\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>,phi);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" >输出</a>此次的哲学家拿起啦一双筷子在吃饭</span>    usleep(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;3</span>); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;吃饭,把进程挂起一段<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a> </span>    pthread_mutex_unlock(&amp;chopstick[<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>]); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;放下左手的筷子</span>    printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Philosopher %c release chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>);    pthread_mutex_unlock(</span>&amp;chopstick[right]); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;放下右手的筷子</span>    printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;Philosopher %c release chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;,right);}

}
<span >int<span > main(){
pthread_t A,B,C,D,E; <span >//<span >5个哲学家

<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; i;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;for</span> (i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;0</span>; i < <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;"&gt;5</span>; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)pthread_mutex_init(</span>&amp;chopstick[i],N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;初始化<a href="https://www.jb51.cc/tag/mo/" target="_blank" >默</a>认互斥锁<a href="https://www.jb51.cc/tag/shuxing/" target="_blank" >属性</a>的互斥锁数组chopstick[i],<a href="https://www.jb51.cc/tag/mo/" target="_blank" >默</a>认<a href="https://www.jb51.cc/tag/shuxing/" target="_blank" >属性</a>为<a href="https://www.jb51.cc/tag/kuaisu/" target="_blank" >快速</a>互斥锁</span>pthread_create(&amp;A,N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L,eat_think,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;A</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;"&gt;///</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;/<a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>并<a href="https://www.jb51.cc/tag/tiaozhuan/" target="_blank" >跳转</a>到线程<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" >函数</a><a href="https://m.jb51.cc/tag/chuangjian/" target="_blank" >创建</a>并<a href="https://www.jb51.cc/tag/tiaozhuan/" target="_blank" >跳转</a>到<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>为指向线程标识符的指针为 A 线程<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" >函数</a>eat_think,A是运行<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" >函数</a>的<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a></span>pthread_create(&amp;B,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;B</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);pthread_create(</span>&amp;C,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;C</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);pthread_create(</span>&amp;D,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;D</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);pthread_create(</span>&amp;E,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;E</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;);pthread_join(A,N<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>L);</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;等待线程标识符为 A 的eat_think线程<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" >函数</a>结束</span>

<span > pthread_join(B,NulL);
pthread_join(C,NulL);
pthread_join(D,NulL);
pthread_join(E,NulL);
<span >return <span >0<span >;
}

 

总结

以上是内存溢出为你收集整理的Linux环境下实现哲学家就餐问题全部内容,希望文章能够帮你解决Linux环境下实现哲学家就餐问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1050561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存