资源简介
模拟吃水果的同步模型:桌子上有一只盘子,最多可容纳两个水果,每次只能放入或者取出一个水果。爸爸专门向盘子中放苹果,妈妈专门向盘子中放橘子,两个儿子专门等待吃盘子中的橘子,两个女儿专门等吃盘子中的苹果。

代码片段和文件信息
/*
This program default set:
the time of father put the apple : 3s
the time of mother put the orange : 3s
the count of BigSon eat oranges : 5 3s/one
the count of LittleSon eat oranges : 4 3s/one
the count of BigDaughter eat apples : 3 4s/one
the count of BigDaughter eat apples : 2 4s/one
*/
#include
#include
#include //define EXIT_*
#include
#include
#include
#define threadNum 6
void *threadFunct(void *arg);
void showInfo();
sem_t availPut; //还可以往盘子里放几个水果
sem_t apple; //同步信号量
sem_t orange;
pthread_mutex_t appleLock; //互斥琐
pthread_mutex_t orangeLock;
int appleNum=1;
int orangeNum=1;
short appleNeed=1;
short orangeNeed=1;
short appleNeed_LD=1;
short appleNeed_BD=1;
short orangeNeed_BS=1;
short orangeNeed_LS=1;
FILE *fp;
char* name[threadNum]={“Father““Mother““BigSon““LittleSon““BigDaughter““LittleDaughter“};
int main (){
int res;
int i;
pthread_t threadArray[threadNum];
void *thread_res;
fp=fopen(“result_info““w“);
res=sem_init(&availPut00);
if (res!=0){
perror (“availPut Semaphone init failed!“);
exit(EXIT_FAILURE);
}
res=sem_init(&apple01);
if (res!=0){
perror (“apple Semaphone init failed!“);
exit(EXIT_FAILURE);
}
res=sem_init(&orange01);
if (res!=0){
perror (“orange Semaphone init failed!“);
exit(EXIT_FAILURE);
}
res=pthread_mutex_init(&appleLockNULL);
if (res!=0){
perror (“apple huchi Semaphone init failed!“);
exit(EXIT_FAILURE);
}
res=pthread_mutex_init(&orangeLockNULL);
if (res!=0){
perror (“orange huchi Semaphone init failed!“);
exit(EXIT_FAILURE);
}
showInfo();
for (i=0;i res=pthread_create(&threadArray[i]NULLthreadFunct(void *)i);
if (res==0){
fprintf(fp“*** %s *** thread create succeed !\n“name[i]);
}else{
perror (“thread create failed!\n“);
exit(EXIT_FAILURE);
}
}
//sleep(2);
for (i=threadNum-1;i>=0;i--){
res=pthread_join(threadArray[i]&thread_res);
if (res==0){
fprintf(fp“pick up ### %s ### thread %d\n“name[i]i);
}else{
perror (“Thread join failed!“);
}
}
fprintf(fp“Thread joinall threads finished!\n“);
showInfo();
sem_destroy(&availPut);
sem_destroy(&apple);
sem_destroy(&orange);
pthread_mutex_destroy(&appleLock);
pthread_mutex_destroy(&orangeLock);
exit(EXIT_SUCCESS);
}
void showInfo(){ //sem_t is defined in /usr/include/bits/semaphore.h
// (long int)orange.__align
fprintf(fp“\n*********************************\n“);
fprintf(fp“There are %d apple(s) %d orange(s)\n“appleNumorangeNum);
fprintf(fp“*********************************\n“);
}
void *threadFunct(void *arg){
int n=(int)arg;
int i;
switch(n)
{
case 0: //father
{
sleep(1);
i=1;
while(appleNeed){
sem_wait(&availPut); //P -1
fprintf(fp“\n----------------------\n“);
fprintf(fp“Father put an apple\n“);
fprintf(fp“Father have put %d apple(s)\n“i++);
fprintf(fp“-------
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-01-18 15:32 fruits\
文件 16627 2011-01-18 10:35 fruits\fruit
文件 8382 2011-01-18 10:34 fruits\fruit.c
文件 8362 2011-01-18 15:32 fruits\fruit.txt
文件 12350 2011-01-18 10:35 fruits\fruits
文件 5892 2011-01-17 21:41 fruits\fruits.c
文件 218 2011-01-18 09:32 fruits\Makefile
文件 6153 2011-01-18 10:39 fruits\result_info
文件 1271211 2011-01-18 21:17 操作系统课设报告.doc
- 上一篇:GPS数据转谷歌地图轨迹的工具NMEA2KMZ301
- 下一篇:教师教学辅助系统开发
相关资源
- FTP课程设计(服务端+客户端)
- 升腾Win终端系统升级方法新版.doc
- Uninstall_Cortana_WINCLIENT.CN.rar
- 高频电子线路课程设计报告收音机
- 直流稳压电源的课程设计、安装及调
- EDA课程设计_密码锁
- STM32基于rt_thread操作系统的SDHC卡文件
- 单片机课程设计 篮球计分器
- 数据结构课程设计 6 1 彩票系统
- 端口扫描课程设计详细的报告
- 步进电机课程设计(个人设计)
- 校园网络规划与设计课程设计
- 编译原理课程设计:词法语法编译器
- 操作系统 LRU算法 实验报告 及 程序代
-
simuli
nk 课程设计 qpsk - 武汉理工大学 单片机课程设计 16*16点
- [免费]车载CE6.0操作系统
- 数据库VFP课程设计
- 分页系统模拟实验 操作系统 课程设
- 模拟段页式虚拟存储管理中地址转换
- 硬件课程设计—流水灯(quartus软件
- 超市收银系统eclipse access大学课程设计
- 航空订票系统_数据结构课程设计
- 操作系统实验——虚存管理实验
- c 课程设计 职工信息管理系统
- 广工操作系统实验
- 汇编语言,课程设计,红绿灯
- 机床液压系统课程设计卧式钻床动力
- 广东工业大学操作系统实验四文件系
- 课程设计蔬菜大棚自动控制系统,包
评论
共有 条评论