操作系统课程设计--Linux平台哲学家问题
哲学家问题是操作系统中资源分配的经典问题
linux平台下的系统api不同于Windows下的实现
要求:一个正确的哲学家程序(不会发生死锁)
一个错误的哲学家程序(会发生死锁)
系统环境:ElementaryOS
wrong.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/wait.h>
#define DELAY (rand() % 5 + 1)
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while() union semun
{
int val; // Value for SETVAL
struct semid_ds *buf; //Buffer for IPC_STAT, IPC_SET
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};
//semid ! this is the number of share memory
int semid;
//waiting for fork here
//left and right = the number of sourses
void wait_for_1fork(int no)
{
int left = no;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{left, -, },
};
//semop do wait or signal (p/v)
//"first": the number of share memory pro
//"second":the first point of struct
//"third":the number of signal to complete
semop(semid, buf, );
}
void wait_for_2fork(int no)
{
int right = (no + ) % ;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{right, -, }
};
//semop do wait or signal (p/v)
//"first": the number of share memory pro
//"second":the first point of struct
//"third":the number of signal to complete
semop(semid, buf, );
}
void free_1fork(int no)
{
int left = no;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{left, , },
};
semop(semid, buf, );
}
void free_2fork(int no)
{
int right = (no + ) % ;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{right, , }
};
semop(semid, buf, );
}
void philosopere(int num)
{
srand(getpid());
for (; ;)
{
//printf("%d is thinking\n", num);
printf("\033[36m%d is thinking\n\033[0m", num);
if(num!=)
sleep(DELAY);
//printf("%d is hungry\n", num);
wait_for_1fork(num);
//printf("%d pick up left chopsticks\n", num);
printf("\033[31m%d pick up left chopsticks\n\033[0m", num);
sleep(DELAY);
sleep(DELAY);
wait_for_2fork(num);
//printf("%d pick up right chopsticks\n", num);
printf("\033[34m%d pick up right chopsticks\n\033[0m", num);
//printf("%d is eating\n", num);
sleep(DELAY);
free_1fork(num);
//printf("%d return left chopsticks\n", num);
printf("\033[33m%d return left chopsticks\n\033[0m", num);
sleep(DELAY);
free_2fork(num);
//printf("%d return right chopsticks\n", num);
printf("\033[37m%d return right chopsticks\n\033[0m", num);
}
}
int main()
{
//use IPC to connect between processes . A new share memory
//semget() return the number of share memory
//new signal key=0 and never change .So not use key
//"first":use IPC_PRIVATE to share memory between relation processes
//"second":(size_t)naxSize
//"third":(int flag)private:666--read and write ; IPC_CREAT creat new memory
semid = semget(IPC_PRIVATE, , IPC_CREAT | );
if (semid == -)
ERR_EXIT("semget");
union semun su;
su.val = ;
for (int i = ; i < ; i++)
{
//creat a new object on "semid"
//use i to depart 5 processes
semctl(semid, i, SETVAL, su);
}
int no = ;
//pid_t (Process ID _ Type) Just like int
pid_t pid;
for (int i = ; i < ; i++)
{
//use fork() to make a copy of father process named with child pro
//father.pid>0 and child.pid=0
//after for loop will exist a father and five childs(0-4)
pid = fork();
if (pid == -)
ERR_EXIT("fork");
if (pid == )
{
no = i;
//break the child process loop to run the philosopere
break;
}
}
philosopere(no);
return ;
}
right.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/wait.h>
#define DELAY (rand() % 5 + 1)
#define ERR_EXIT(m) \
do { \
perror(m); \
exit(EXIT_FAILURE); \
} while() union semun
{
int val; // Value for SETVAL
struct semid_ds *buf; //Buffer for IPC_STAT, IPC_SET
unsigned short *array; /* Array for GETALL, SETALL */
struct seminfo *__buf; /* Buffer for IPC_INFO (Linux-specific) */
};
//semid ! this is the number of share memory
int semid;
//waiting for fork here
//left and right = the number of sourses
void wait_for_2fork(int no)
{
int left = no;
int right = (no + ) % ;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{left, -, },
{right, -, }
};
//semop do wait or signal (p/v)
//"first": the number of share memory pro
//"second":the first point of struct
//"third":the number of signal to complete
semop(semid, buf, );
}
void free_2fork(int no)
{
int left = no;
int right = (no + ) % ;
//system defined struct
//"first":the current number of pro
//"second":resourses -1:wait cannot use;+1:can use
//"thired":?
struct sembuf buf[] =
{
{left, , },
{right, , }
};
semop(semid, buf, );
}
void philosopere(int num)
{
srand(getpid());
for (; ;)
{
//printf("\033[31m Hello\n\033[0m");
//printf("\033[36m Hello\n\033[0m");
//printf("\%d is thinking\n", num);
printf("\033[31m%d is thinking\n\033[0m", num);
sleep(DELAY);
printf("%d pick up two chopsticks\n", num);
printf("\033[36m%d pick up two chopsticks\n\033[0m", num);
wait_for_2fork(num);
//printf("%d is eating\n", num);
printf("\033[34m%d is eating\n\033[0m", num);
sleep(DELAY);
free_2fork(num);
}
}
int main()
{
//use IPC to connect between processes . A new share memory
//semget() return the number of share memory
//new signal key=0 and never change .So not use key
//"first":use IPC_PRIVATE to share memory between relation processes
//"second":(size_t)naxSize
//"third":(int flag)private:666--read and write ; IPC_CREAT creat new memory
semid = semget(IPC_PRIVATE, , IPC_CREAT | );
if (semid == -)
ERR_EXIT("semget");
union semun su;
su.val = ;
for (int i = ; i < ; i++)
{
//creat a new object on "semid"
//use i to depart 5 processes
semctl(semid, i, SETVAL, su);
}
int no = ;
//pid_t (Process ID _ Type) Just like int
pid_t pid;
for (int i = ; i < ; i++)
{
//use fork() to make a copy of father process named with child pro
//father.pid>0 and child.pid=0
//after for loop will exist a father and five childs(0-4)
pid = fork();
if (pid == -)
ERR_EXIT("fork");
if (pid == )
{
no = i;
//break the child process loop to run the philosopere
break;
}
}
philosopere(no);
return ;
}
操作系统课程设计--Linux平台哲学家问题的更多相关文章
- Bryce1010的操作系统课程设计
https://download.csdn.net/download/fire_to_cheat_/10221003 上面是课程设计的代码,下载需要一些积分. 1.作业调度 2.磁盘调度 常见的磁盘调 ...
- Perl Tk在IC设计中的应用、Windows、Linux平台下的安装-各种错误的摸索解决
本文转自:自己的微信公众号<集成电路设计及EDA教程> <Perl Tk在IC设计中的应用.Windows.Linux平台下的安装-各种错误的摸索解决> Perl在IC设计中有 ...
- 计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场
计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场 注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SSM(S ...
- C 语言学习的第 05 课:了解课程配套的平台
在此之前,已经同授课老师沟通,确认课程的配套平台是Coding.net.对于大多数(甚至是全部)同学来说,这个平台应该是极其陌生的.不过不用担心,且还是娓娓道来. 定义:Coding.net是一个集代 ...
- 嵌入式系统及应用课程设计——基于STM32的温湿度监测系统
大三上学期期末总结,嗯,没错上学期,写在新学期开始,hhh. 上学期学了一门嵌入式系统及应用的课程,期末的课程设计题目是基于STM32的温湿度监测系统. 记得刚开始做课程设计的时候,听说先设计画出原理 ...
- 《基于Arm实验箱的国密算法应用》课程设计 结题报告
<基于Arm实验箱的国密算法应用>课程设计 结题报告 小组成员姓名:20155206赵飞 20155220吴思其 20155234昝昕明 指导教师:娄嘉鹏 设计方案 题目要求:基于Arm实 ...
- 《基于Cortex-M4的ucOS-III的应用》课程设计 结题报告
<基于Cortex-M4的ucOS-III的应用>课程设计 结题报告 小组成员姓名:20155211 解雪莹 20155217 杨笛 20155227 辜彦霖 指导教师:娄嘉鹏 一.设计方 ...
- 20155338课程设计个人报告——基于ARM实验箱的Android交友软件的设计与实现
课程设计个人报告--基于ARM实验箱的Android交友软件的设计与实现 个人贡献 实验环境的搭建 代码调试 在电脑上成功运行 研究程序代码撰写小组报告 一.实验环境 1.Eclipse软件开发环境: ...
- 转:基于 linux 平台的 libpcap 源代码分析
libpcap 是 unix/linux 平台下的网络数据包捕获函数包,大多数网络监控软件都以它为基础.Libpcap 可以在绝大多数类 unix 平台下工作,本文分析了 libpcap 在 linu ...
随机推荐
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Android实现类似换QQ头像功能(图片裁剪)
现在几乎所有的App都有用户登录模块,需要设置用户头像,而关于用户头像部分无疑也是比较头疼的,目前大部分应用的头像部分会有两种方式:一种是利用系统的裁剪功能去获取用户头像,一种就是获取到图片或者照片的 ...
- Web javascript 中常用API合集
来源于:https://www.kancloud.cn/dennis/tgjavascript/241852 一.节点 1.1 节点属性 Node.nodeName //返回节点名称,只读 Node. ...
- IO流-----写到输出流
输出流:---链接:http://blog.csdn.net/du_minchao/article/details/49045421 /** * 方法名:writeStream * 方法描述:写到输出 ...
- ubuntu中pycharm安装激活第二种方法的密钥
43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNza WduZWVOYW1lIjoi ...
- [fiddler] 使用fiddler script自定义代理规则
场景 fiddler作为调试代理工具,可以捕获电脑与互联网之间所有http通讯. 通过可视化操作或命令行可以按某些规则截获特定请求并修改,但当我们需要批量对请求进行更复杂的逻辑操作时,则不是很方便. ...
- bzoj2702[SDOI2012]走迷宫
题意:给你一个有向图,点数10000,边数1000000,SCC大小不超过100(按数据范围的写法只有第三部分数据满足这个条件,不过第二部分数据并没有出现大小大于100个点的SCC,我是用数组大小为1 ...
- mybatis-generator 1.3.5支持流式 fluent 方法
在以往的无数此写model的过程中,大家都会烦恼model的set方法写一堆.比如 Person p = new Person(); p.setName("name"); p.se ...
- JUC学习笔记--Thread多线程基础
实现多线程的两种方法 java 实现多线程通过两种方式1.继承Thread类 ,2.实现Runnable接口 class Newthead extends Thread{ public void ru ...
- HTML5学习总结-番外05 移动终端适配
一 viewport 在使用移动端设备浏览网页时,移动端浏览器是直接把整个页面放到一个虚拟的视图里来显示的,通常来说这个虚拟的视图大小会比手机屏幕大,用户可以通过手势操作来平移.缩放这个视图. 如果不 ...