操作系统:The Dining Philosophers Problem
哲学家进餐问题
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>pthread_mutex_t chopstick[5]; //5把锁,也就是5根筷子。void*tfn(void *arg)
{int i = (int)arg;int left,right; //左右筷子的编号。if(i == 4){left = 0;right = i;}else{left = i;right = i+1;}while(1){sleep(1); //思考pthread_mutex_lock(&chopstick[left]); //拿到左手的筷子。printf("philosopher %d fetches chopstick %d\n",i,left);if(pthread_mutex_trylock(&chopstick[right]) != 0 )//拿右手的筷子失败{pthread_mutex_unlock(&chopstick[left]); //右手筷子被拿走,放下左手的筷子。continue;}printf("philosopher %d fetches chopstick %d\n",i,right);printf("philosopher %d is eating.\n",i);sleep(2); //吃饭pthread_mutex_unlock(&chopstick[right]); //放下右手的筷子。printf("philosopher %d release chopstick %d\n",i,right);pthread_mutex_unlock(&chopstick[left]); //放下右手的筷子。printf("philosopher %d release chopstick %d\n",i,left);}
}int main()
{pthread_t tid[5]; //5个哲学家int i ;for(i = 0; i < 5; ++i){pthread_mutex_init(&chopstick[i],NULL);pthread_create(&tid[i],NULL,tfn,(void*)i);}for( i = 0; i < 5; ++i){pthread_join(tid[i],NULL);}return 0;
}
运行结果:
操作系统:The Dining Philosophers Problem相关推荐
- Dining philosophers problem
经常使用的方法: Locking a resource is a common technique to ensure the resource is accessed by only one pro ...
- 1226. The Dining Philosophers (Leetcode 1226)
1226. The Dining Philosophers (Leetcode 1226) 题目描述 原文地址 https://leetcode.com/problems/the-dining-phi ...
- 操作系统computer operate system
凭记忆,部分内容参考西安电子科技大学操作系统书 操作系统,主要功能是两个,向下管理硬件资源,向上提供命令.接口,增大吞吐量throughtput和利用率是操作系统的目标. 人机矛盾,速度不匹配,试试加 ...
- 【北航操作系统】2020春课堂测试及答案
目录 第二次课上测试(上) 第二次课上测试(下) 第三次课上测试 第四次课上测试 第五次课上小测试 第六次课上测试 第七次课上测试 第8次课上测试 第9次课上测试 第十次课上问卷 第 11 次课问卷 ...
- 操作系统笔记(6) 进程同步(Process Synchronization)
文章目录 一.背景 二. 临界区(Critical-Section) 理论上的解决方案要满足(单核状态): Peterson's Solution 进程的情况 简单的形式? 三. 硬件同步机制(Syn ...
- 《深入理解计算机系统》CSAPP第3版术语索引表
<深入理解计算机系统>几乎做到了只讲对程序员"有用的"原理,所以这本书性价比非常高.并没有在体系结构和操作系统的许多实现问题上纠缠. 第三版的英文原版是有index表的 ...
- Synchronization (computer science)
过程同步.数据同步. In computer science, synchronization refers to one of two distinct but related concepts: ...
- 蒙蒂霍尔问题_常见的逻辑难题–骑士和刀,蒙蒂·霍尔和就餐哲学家的问题解释...
蒙蒂霍尔问题 While not strictly related to programming, logic puzzles are a good warm up to your next codi ...
- 2000个软件开发领域的高频特殊词及精选例句
1101.单词 uuids 的匹配文本: For example, when accessing /_uuids, you get a list of UUIDs from the system. G ...
最新文章
- Linux输入输出I/O
- 我的Java后端书架 (2016年暖冬4.0版)
- CNN中的卷积操作的参数数计算
- 单片机实用工具大全,超级赞,工程师必备!
- 对大脑有益的16种食物_对大脑有益的食物有哪些?
- echart仪表盘旋转_使用echart仪表盘
- php model controller,Laravel 中 Controller访问Model函数/常量
- linux 每日学一点《Linux链接文件类型》
- 新手小白python数据清洗代码_5天10分钟,学会数据清洗!(Python)
- 《JAVA程序设计基础(第3版)实验指导》pdf 附下载链接
- 高等代数——大学高等代数课程创新教材(丘维声)——3.8笔记+习题
- What Music简单的全网音乐播放器
- Python学习之波形图
- Codeforces 918D MADMAX 图上dp 组合游戏
- win7虚拟机iOS坑爹法语键盘冲突解决方法
- 第三类边界条件 matlab,matlab怎么解偏微分方程
- 用Cat.1+GPS+蓝牙+RFID实现工卡式室内室外定位器
- 判断图片色彩模式 CYMK || RGB
- 碟评--梁紫丹新碟<<紫色的声音>>
- 《超能陆战队》人工智能离我们还有多远
热门文章
- Lora超全知识归纳,对于lora和lorawan的详细介绍
- 谈分布式事务一致性(2PC、3PC、TCC)、强一致性算法Paxos等关系
- 面试最让你手足无措的一个问题:你的系统如何支撑高并发?
- PFCdocumentation_Coupling PFC and FLAC3D
- FTP 331 Please specify the password 500 OOPS: cannot change directory
- 使用网易等网络相册来保存文件
- web前端入门必读书籍
- mybaties数据源配置类型(POOLED、JNDI、UNPOOLED)
- HarmonyOS鸿蒙学习笔记(11)Flex组件alignItems属性说明
- JAVA中0xFF代表什么_详解 0xff 的作用