Linux   发布时间:2022-05-08  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Linux环境下实现哲学家就餐问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="cnblogs_code">

#include 

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

<span style="color: #000000;"> }

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

}

}
<span style="color: #0000ff;">int<span style="color: #000000;"> main(){
pthread_t A,B,C,D,E; <span style="color: #008000;">//<span style="color: #008000;">5个哲学家

<span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; i;
</span><span style="color: #0000ff;"&gt;for</span> (i = <span style="color: #800080;"&gt;0</span>; i < <span style="color: #800080;"&gt;5</span>; i++<span style="color: #000000;"&gt;)
pthread_mutex_init(</span>&amp;chopstick[i],null);<span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;初始化<a href="http://code.js-code.com/tag/mo/" target="_blank" class="keywords">默</a>认互斥锁<a href="http://code.js-code.com/tag/shuxing/" target="_blank" class="keywords">属性</a>的互斥锁数组chopstick[i],<a href="http://code.js-code.com/tag/mo/" target="_blank" class="keywords">默</a>认<a href="http://code.js-code.com/tag/shuxing/" target="_blank" class="keywords">属性</a>为<a href="http://code.js-code.com/tag/kuaisu/" target="_blank" class="keywords">快速</a>互斥锁</span>
pthread_create(&amp;A,NULL,eat_think,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;A</span><span style="color: #800000;"&gt;"</span>);<span style="color: #808080;"&gt;///</span><span style="color: #008000;"&gt;/创建并<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>到线程<a href="http://code.js-code.com/tag/hanshu/" target="_blank" class="keywords">函数</a>创建并<a href="http://code.js-code.com/tag/tiaozhuan/" target="_blank" class="keywords">跳转</a>到参数为指向线程标识符的指针为 A 线程<a href="http://code.js-code.com/tag/hanshu/" target="_blank" class="keywords">函数</a>eat_think,A是运行<a href="http://code.js-code.com/tag/hanshu/" target="_blank" class="keywords">函数</a>的参数</span>
pthread_create(&amp;B,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;B</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
pthread_create(</span>&amp;C,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;C</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
pthread_create(</span>&amp;D,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;D</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);
pthread_create(</span>&amp;E,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;E</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;);

pthread_join(A,null);</span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;等待线程标识符为 A 的eat_think线程<a href="http://code.js-code.com/tag/hanshu/" target="_blank" class="keywords">函数</a>结束</span>

<span style="color: #000000;"> pthread_join(B,null);
pthread_join(C,null);
pthread_join(D,null);
pthread_join(E,null);
<span style="color: #0000ff;">return <span style="color: #800080;">0<span style="color: #000000;">;
}

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: