<div >
#includepthread_mutex_t chopstick[] ; *eat_think( * phi = *( *)arg; left,right; = = = = = = = = = = </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> i;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(;;){ usleep(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">3</span>); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">思考,将进程挂起一段<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a></span> pthread_mutex_lock(&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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">拿起左手的筷子,锁定互斥量,加锁</span> printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Philosopher %c fetches chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,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;">if</span> (pthread_mutex_trylock(&chopstick[right]) == EBUSY){ <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">判断右手的筷子是否有人用,再试一次获得对互斥量的锁定(非阻塞) </span> pthread_mutex_unlock(&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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果右边筷子被拿走放下左手的筷子,解锁互斥量,解锁</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">continue</span>;<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">如果此哲学家没能吃饭,验证下<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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"> pthread_mutex_lock(&chopstick[right]); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">拿起右手的筷子,如果想观察死锁,把上一句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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Philosopher %c fetches chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>,right); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Philosopher %c is eating.\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>,phi);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><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;">3</span>); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">吃饭,把进程挂起一段<a href="https://m.jb51.cc/tag/shijian/" target="_blank" >时间</a> </span> pthread_mutex_unlock(&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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">放下左手的筷子</span> printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Philosopher %c release chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,<a href="https://m.jb51.cc/tag/left/" target="_blank" >left</a>); pthread_mutex_unlock(</span>&chopstick[right]); <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">放下右手的筷子</span> printf(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Philosopher %c release chopstick %d\n</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,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;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> i;</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">5</span>; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)pthread_mutex_init(</span>&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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">初始化<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(&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;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">A</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>);<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #808080;">///</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">/<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(&B,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">B</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">);pthread_create(</span>&C,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">C</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">);pthread_create(</span>&D,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">D</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">);pthread_create(</span>&E,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">E</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">);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;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">等待线程标识符为 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环境下实现哲学家就餐问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)