哲学家问题是操作系统中资源分配的经典问题

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平台哲学家问题的更多相关文章

  1. Bryce1010的操作系统课程设计

    https://download.csdn.net/download/fire_to_cheat_/10221003 上面是课程设计的代码,下载需要一些积分. 1.作业调度 2.磁盘调度 常见的磁盘调 ...

  2. Perl Tk在IC设计中的应用、Windows、Linux平台下的安装-各种错误的摸索解决

    本文转自:自己的微信公众号<集成电路设计及EDA教程> <Perl Tk在IC设计中的应用.Windows.Linux平台下的安装-各种错误的摸索解决> Perl在IC设计中有 ...

  3. 计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场

    计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场 注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SSM(S ...

  4. C 语言学习的第 05 课:了解课程配套的平台

    在此之前,已经同授课老师沟通,确认课程的配套平台是Coding.net.对于大多数(甚至是全部)同学来说,这个平台应该是极其陌生的.不过不用担心,且还是娓娓道来. 定义:Coding.net是一个集代 ...

  5. 嵌入式系统及应用课程设计——基于STM32的温湿度监测系统

    大三上学期期末总结,嗯,没错上学期,写在新学期开始,hhh. 上学期学了一门嵌入式系统及应用的课程,期末的课程设计题目是基于STM32的温湿度监测系统. 记得刚开始做课程设计的时候,听说先设计画出原理 ...

  6. 《基于Arm实验箱的国密算法应用》课程设计 结题报告

    <基于Arm实验箱的国密算法应用>课程设计 结题报告 小组成员姓名:20155206赵飞 20155220吴思其 20155234昝昕明 指导教师:娄嘉鹏 设计方案 题目要求:基于Arm实 ...

  7. 《基于Cortex-M4的ucOS-III的应用》课程设计 结题报告

    <基于Cortex-M4的ucOS-III的应用>课程设计 结题报告 小组成员姓名:20155211 解雪莹 20155217 杨笛 20155227 辜彦霖 指导教师:娄嘉鹏 一.设计方 ...

  8. 20155338课程设计个人报告——基于ARM实验箱的Android交友软件的设计与实现

    课程设计个人报告--基于ARM实验箱的Android交友软件的设计与实现 个人贡献 实验环境的搭建 代码调试 在电脑上成功运行 研究程序代码撰写小组报告 一.实验环境 1.Eclipse软件开发环境: ...

  9. 转:基于 linux 平台的 libpcap 源代码分析

    libpcap 是 unix/linux 平台下的网络数据包捕获函数包,大多数网络监控软件都以它为基础.Libpcap 可以在绝大多数类 unix 平台下工作,本文分析了 libpcap 在 linu ...

随机推荐

  1. docker 学习过程

    参考: http://git.oschina.net/search?search=csphere&type=project&language=&page=2&condi ...

  2. vue.js 第二课

    实现响应的数据绑定和组合的视图组件. 让数据与Dom保持同步 . 在使用jq手工操作DOM时,我们的代码常常是命令式的.重复的.易错的.Vue.js拥抱数据驱动的视图概念.简单的讲,它意味着我们在普通 ...

  3. C#中out和ref之间的区别【转】

    首先:两者都是按地址传递的,使用后都将改变原来参数的数值. 其次:ref可以把参数的数值传递进函数,但是out是要把参数清空,就是说你无法把一个数值从out传递进去的,out进去后,参数的数值为空,所 ...

  4. 日历插件FullCalendar应用:(二)数据增删改

    接上一篇 日历插件FullCalendar应用:(一)数据展现. 这一篇主要讲使用fullcalendar插件如何做数据的增删改,用到了art.dialog web对话框组件,上一篇用到的webFor ...

  5. 【codeforces 749E】 Inversions After Shuffle

    http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...

  6. POJ2096 Collecting Bugs

    Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 5090   Accepted: 2529 Case Time Limit: ...

  7. PHP判断SESSION过期的方法

    在公共函数function.php中添加如下方法,即可控制用户登录的SESSION有效期 //封装SESSION过期判断方法function start_session($expire = 0){ $ ...

  8. 正则匹配闭合HTML标签(支持嵌套)

    任何复杂的正则表达式都是由简单的子表达式组成的,要想写出复杂的正则来,一方面需要有化繁为简的功底,另外一方面,我们需要从正则引擎的角度去思考问题.关于正则引擎的原理,推荐<Mastering R ...

  9. Theano Inplace

    Theano Inplace inplace Computation computation that destroy their inputs as a side-effect. Example i ...

  10. csv 中 数值被自动转换成科学计数法 的问题 excel打开后数字用科学计数法显示且低位变0的解决方法

    保存在csv中的 013812345678,前面的0会被去掉,后面是科学计数法显示.保存成 col1,="013812345678" 即可. 注意,分隔符逗号后面直接接“=”等号. ...