哲学家用餐模型

分析:

为了避免死锁,做了如下规定:每个哲学家先拿自己左手边的筷子,然后再去拿右手边的筷子,如果不能同时得到两支筷子,则该哲学家放下手中已有的筷子。这种规定依然会因为振荡而产生死锁,例如:每个哲学家都同时拿上了左手边的筷子,然后都阻塞,都释放左手筷子;然后又同时去拿左手边的筷子,这样周而复始,一直进行下去,就是因为振荡而产生的死锁。解决方法:规定其中一位哲学家先拿右手边的筷子,再拿左手边的筷子即可。这样即使最坏的情况下,也至少有一位哲学家可以拿到两支筷子,而只要有一位哲学家拿到了两支筷子,就肯定不会出现死锁。

在这里筷子是临界资源,即只能被哲学家互斥访问,因此可以将筷子作为互斥量或者信号量。五支筷子应该引入五个互斥量,或者5个信号量(初始值为1)。将五个哲学家当成五个线程,其编号为i(0、1、2、3、4),互斥量或信号量编号也为0、1、2、3、4。则按照规定:i=0~3时,先拿i,再拿i+1i=4时,先拿0,再拿i。在此基础上,对于每一个线程,如果不能同时拿上两支筷子,则放弃已获得的筷子,则:第一次用lock函数,采用阻塞加锁;第二次拿筷子用trylock函数,采用非阻塞加锁,并且释放掉已经拿掉的筷子。

注意:互斥量、信号量既可以用于进程间同步,也可以用于线程间同步(一般来说,进程间同步信号量用的多一点);条件变量需要结合互斥量使用;读写锁用于线程间同步,而文件锁用于进程间同步。

下面线程版采用互斥量,进程版采用信号量

//线程版哲学家用餐模型

#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>pthread_mutex_t mutex[5];void *tfn( void *arg )
{int i = (int)arg;int ret;while(1) {if( i<4 ){pthread_mutex_lock( &mutex[i] );ret = pthread_mutex_trylock( &mutex[i+1] );if( ret != 0){//printf("The %dth philosopher missed the second chopstick!\n", i+1);pthread_mutex_unlock( &mutex[i] );}else{printf("Very good, the %dth philosopher has a pair of chopsticks to eat food!\n", i+1);sleep(5);pthread_mutex_unlock( &mutex[i] );pthread_mutex_unlock( &mutex[i+1] );//break;}}else{pthread_mutex_lock( &mutex[0] );ret = pthread_mutex_trylock( &mutex[i] );if( ret != 0){//printf("The %dth philosopher missed the second chopstick!\n", i+1);pthread_mutex_unlock( &mutex[0] );}else{printf("Very good, the %dth philosopher has a pair of chopsticks to eat food!\n", i+1);sleep(5);pthread_mutex_unlock( &mutex[i] );pthread_mutex_unlock( &mutex[0] );//break;}}}return NULL;
}int main( void )
{int i, ret;pthread_t phi[5];pthread_attr_t attr;ret = pthread_attr_init( &attr );if( ret != 0 ){fprintf(stderr,"pthread_attr_init error: %s.\n", strerror(ret));exit(1);}ret = pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );if( ret != 0 ){fprintf(stderr,"pthread_attr_detach error: %s.\n", strerror(ret));exit(1);}for( i=0; i<5; i++){ret =  pthread_mutex_init( &mutex[i], NULL );if( ret != 0 ){fprintf(stderr,"pthread_mutex_init error: %s.\n", strerror(ret));exit(1);}}for( i=0; i<5; i++){ret = pthread_create( &phi[i], &attr, tfn, (void *)i );if( ret != 0 ){fprintf(stderr,"pthread_create error: %s.\n", strerror(ret));exit(1);}}pthread_attr_destroy( &attr );pthread_exit( NULL );
}

[root@localhost 02_pthread_sync_test]# ./philosophy_pthrd

Very good, the 2th philosopher has a pair of chopsticks to eat food!

Very good, the 4th philosopher has a pair of chopsticks to eat food!

Very good, the 1th philosopher has a pair of chopsticks to eat food!

Very good, the 3th philosopher has a pair of chopsticks to eat food!

Very good, the 3th philosopher has a pair of chopsticks to eat food!

Very good, the 1th philosopher has a pair of chopsticks to eat food!

Very good, the 1th philosopher has a pair of chopsticks to eat food!

Very good, the 3th philosopher has a pair of chopsticks to eat food!

Very good, the 3th philosopher has a pair of chopsticks to eat food!

Very good, the 1th philosopher has a pair of chopsticks to eat food!

//进程版哲学家用餐模型

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/wait.h>int main(void)
{int i;pid_t pid;sem_t *s;s = mmap(NULL, sizeof(sem_t)*5, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);if (s == MAP_FAILED) {perror("fail to mmap");exit(1);}for (i = 0; i < 5; i++)sem_init(&s[i], 0, 1);  //信号量初值制定为1,信号量,变成了互斥锁for (i = 0; i < 5; i++)if ((pid = fork()) == 0)break;if (i < 5) {                            //子进程int l, r;srand(time(NULL));if (i == 4)l = 0, r = 4;elsel = i, r = i+1;while (1) {sem_wait(&s[l]);if (sem_trywait(&s[r]) == 0) {printf(" %c is eating\n", 'A'+i);sem_post(&s[r]);}sem_post(&s[l]);sleep(rand() % 5);}exit(0);}for (i = 0; i < 5; i++)wait(NULL);for (i = 0; i < 5; i++)sem_destroy(&s[i]);munmap(s, sizeof(sem_t)*5);return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385272.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++ Primer | 16】std::move和std::forward、完美转发

右值引用应该是C11引入的一个非常重要的技术&#xff0c;因为它是移动语义&#xff08;Move semantics&#xff09;与完美转发&#xff08;Perfect forwarding&#xff09;的基石&#xff1a; 移动语义&#xff1a;将内存的所有权从一个对象转移到另外一个对象&#xff0c;高效…

循环引用

1. 测试代码 #include <iostream> #include <memory> using namespace std;class B; class A { public:shared_ptr<B> pb;~A() { cout << "kill A\n";} };class B { public:shared_ptr<A> pa;~B() { cout << "kill B\n&q…

8. 字符串转换整数 (atoi)

请你来实现一个 atoi 函数&#xff0c;使其能将字符串转换成整数。 首先&#xff0c;该函数会根据需要丢弃无用的开头空格字符&#xff0c;直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时&#xff0c;则将该符号与之后面尽可能多的连续数字组…

【C++ Primer | 16】容器适配器全特化、偏特化

上面对模板的特化进行了总结。那模板的偏特化呢&#xff1f;所谓的偏特化是指提供另一份模板定义式&#xff0c;而其本身仍为模板&#xff1b;也就是说&#xff0c;针对模板参数更进一步的条件限制所设计出来的一个特化版本。这种偏特化的应用在STL中是随处可见的。比如 1.测试…

select、poll、epoll优缺点

select的缺点&#xff1a; 单个进程能够监视的文件描述符的数量存在最大限制&#xff0c;通常是1024&#xff0c;当然可以更改数量&#xff0c;但由于select采用轮询的方式扫描文件描述符&#xff0c;文件描述符数量越多&#xff0c;性能越差&#xff1b;内核/用户空间内存拷贝…

vector源码剖析

一、vector定义摘要&#xff1a; template <class T, class Alloc alloc> class vector { public:typedef T value_type;typedef value_type* pointer;typedef const value_type* const_pointer;typedef value_type* iterator;typ…

vs2013编译win-32位下的libevent-2.0.21-stable,debug版本

环境&#xff1a;win10&#xff08;64位&#xff09;vs2013 首先需要修改Makefile.nmake中的CFLAGS$(CFLAGS) /Ox /W3 /wd4996 /nologo注释掉&#xff0c;这一行是不带调试信息的。CFLAGS$(CFLAGS) /Od /W3 /wd4996 logo /Zi 替换这一行之后就可以自带调试信息。 打开vs2013的…

Leetcode 219. 存在重复元素 II

解题思路&#xff1a; class Solution { public:bool containsNearbyDuplicate(vector<int>& nums, int k) {unordered_map<int, int> cnt;for(int i0; i<nums.size(); i){if(cnt.find(nums[i]) ! cnt.end()){if(i - cnt[nums[i]] < k) return true;}cn…

Linux程序设计01:开发工具和开发平台

1.SecureCRT 1.1SecureCRT支持SSH*&#xff08;SSH1和SSH2&#xff09;&#xff0c;安装的过程不在赘述 1.2与SecureCRT相关的Linux命令 rz和sz是Linux同windows进行ZModem文件传输的命令行工具。 sz命令利用ZModem协议来从Linux服务器传送文件到本地&#xff0c;一次可以传送一…

fork、vfork、clone

1. 概念 写时复制技术最初产生于Unix系统&#xff0c;用于实现一种傻瓜式的进程创建&#xff1a;当发出fork( )系统调用时&#xff0c;内核原样复制父进程的整个地址空间并把复制的那一份分配给子进程。这种行为是非常耗时的&#xff0c;因为它需要&#xff1a; 为子进程的页…

Linux02进程内存管理

1.进程地址空间 1.1程序的结构与进程的结构 [rootlocalhost demo]# size testtext data bss dec hex filename 1193 492 16 1701 6a5 test 一个可执行程序包含三个部分&#xff1a; 代码段&#xff1a;主要存放指令&#xff0c;操作以及只读的常量数据例…

epoll

开发高性能网络程序时&#xff0c;windows开发者们言必称iocp&#xff0c;linux开发者们则言必称epoll。大家都明白epoll是一种IO多路复用技术&#xff0c;可以非常高效的处理数以百万计的socket句柄&#xff0c;比起以前的select和poll效率高大发了。我们用起epoll来都感觉挺爽…

剑指offer目录

序号题目1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

基于升序链表的定时器

#ifndef LST_TIMER#define LST_TIMER#include <time.h>#define BUFFER_SIZE 64class util_timer;//用户数据结构&#xff1a;客户端地址、客户端的socket、socket文件描述符、读缓存和定时器struct client_data{sockaddr_in address;int sockfd;char buf[ BUFFER_SIZE ];…

SIGCHLD信号使用和注意事项

1.SIGCHLD简介 SIGCHILD是指在一个进程终止或者停止时&#xff0c;将SIGCHILD信号发送给其父进程&#xff0c;按照系统默认将忽略此信号&#xff0c;如果父进程希望被告知其子系统的这种状态&#xff0c;则应捕捉此信号。注意&#xff1a;SIGCLD信号与其长得非常相似。SIGCLD是…

08-图7 公路村村通 (30 分

现有村落间道路的统计数据表中&#xff0c;列出了有可能建设成标准公路的若干条道路的成本&#xff0c;求使每个村落都有公路连通所需要的最低成本。 输入格式: 输入数据包括城镇数目正整数N&#xff08;≤&#xff09;和候选道路数目M&#xff08;≤&#xff09;&#xff1b;随…

【Leetcode】33. 搜索旋转排序数组

假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如&#xff0c;数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 搜索一个给定的目标值&#xff0c;如果数组中存在这个目标值&#xff0c;则返回它的索引&#xff0c;否则返回 -1 。 你可以假设数组中不存在重…

08-图9 关键活动 (30 分

假定一个工程项目由一组子任务构成&#xff0c;子任务之间有的可以并行执行&#xff0c;有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。 比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成…

【Leetocde | 10 】54. 螺旋矩阵

解题代码&#xff1a; class Solution { public:vector<int> spiralOrder(vector<vector<int>>& matrix) {if (matrix.empty() || matrix[0].empty()) return {};int m matrix.size(), n matrix[0].size();vector<int> res;int up 0, down m …

09-排序1 排序 (25 分)

给定N个&#xff08;长整型范围内的&#xff09;整数&#xff0c;要求输出从小到大排序后的结果。 本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下&#xff1a; 数据1&#xff1a;只有1个元素&#xff1b; 数据2&#xff1a;11个不相同的整数…