模拟吃水果的同步模型:桌子上有一只盘子,最多可容纳两个水果,每次只能放入或者取出一个水果。爸爸专门向盘子中放苹果,妈妈专门向盘子中放橘子,两个儿子专门等待吃盘子中的橘子,两个女儿专门等吃盘子中的苹果。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-01-18 15:32 fruits
文件 16627 2011-01-18 10:35 fruitsfruit
文件 8382 2011-01-18 10:34 fruitsfruit.c
文件 8362 2011-01-18 15:32 fruitsfruit.txt
文件 12350 2011-01-18 10:35 fruitsfruits
文件 5892 2011-01-17 21:41 fruitsfruits.c
文件 218 2011-01-18 09:32 fruitsMakefile
文件 6153 2011-01-18 10:39 fruits
esult_info
文件 1271211 2011-01-18 21:17 操作系统课设报告.doc
/*
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 !
“name[i]);
}else{
perror (“thread create failed!
“);
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
“name[i]i);
}else{
perror (“Thread join failed!“);
}
}
fprintf(fp“Thread joinall threads finished!
“);
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“
*********************************
“);
fprintf(fp“There are %d apple(s) %d orange(s)
“appleNumorangeNum);
fprintf(fp“*********************************
“);
}
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“
----------------------
“);
fprintf(fp“Father put an apple
“);
fprintf(fp“Father have put %d apple(s)
“i++);
fprintf(fp“-------
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-01-18 15:32 fruits
文件 16627 2011-01-18 10:35 fruitsfruit
文件 8382 2011-01-18 10:34 fruitsfruit.c
文件 8362 2011-01-18 15:32 fruitsfruit.txt
文件 12350 2011-01-18 10:35 fruitsfruits
文件 5892 2011-01-17 21:41 fruitsfruits.c
文件 218 2011-01-18 09:32 fruitsMakefile
文件 6153 2011-01-18 10:39 fruits
esult_info
文件 1271211 2011-01-18 21:17 操作系统课设报告.doc
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)