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

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. 三石推荐!把 Bootstrap 小清新带回家!

    无敌传送门:http://fineui.com/demo_pro/default.aspx?theme=bootstrap1&menu=accordion   喜欢就来赞一个! 把麻烦留给三石 ...

  2. csv表格处理(上)-- JS 与 PHP 协作导入导出

    CSV简介 在开发后台管理系统的时候,几乎无可避免的会遇到需要导入导出Excel表格的需求.csv也是表格的一种,其中文名为“逗号分隔符文件”.在Excel中打开如下图左边所示,在记事本打开如下图右边 ...

  3. asp.net获取服务器绝对路径和相对路径

    绝对路径 AppDomain.CurrentDomain.SetupInformation.ApplicationBase 相对路径 Server.MapPath("~/")表示当 ...

  4. json的场景应用与实战

    首先 要感谢慕课网的老师 地址:http://www.imooc.com/learn/68 下面我来开始总结: 什么是json的这些我就不多说了  不懂百度 <?php function cre ...

  5. 冰冻三尺非一日之寒--web框架Django(三)

      第二十章: django(三,多对多)   1.Django请求的生命周期         路由系统 -> 视图函数(获取模板+数据-->渲染) -> 字符串返回给用户   2. ...

  6. jQuery进阶

    复习: jq无论如何都是一个集合 jq是一个包装集 var arr=$("div").get( )会将所有的DOM对象转换成真正的数组, get( )里边没传参数 兄弟元素: 只要 ...

  7. java面向对象---对象容器

    泛型类--ArrayList<>; 2.对象数组中的每个元素都是对象的管理者而并非对象本身!!!!! 3.java类的基本数据类型 基本数据类型 包装类 byte Byte short S ...

  8. js修改伪类的值

    css文件 p.change:after { content: attr(data-content); } js文件 $(this).addClass('change').attr('data-con ...

  9. Android ooVoo Apk附件关联分析

    为什么要分析附件关联 发送和接收的图片以及头像等从网上下载的存储在手机的sdcard上面以转换后的名字命名,需要分析数据库中的记录所对应的sdcard的文件才能关联.比如数据库存储是http://oo ...

  10. foreach

    一 foreach的语法介绍    PHP 4以上的版本包括了 foreach 结构,这只是一种遍历数组简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生 ...