吃水果问题的模拟程序
2015-10-30 23:56
716 查看
题目:
有一个盘子,可以放5个水果(苹果or桔子)。父亲每次向盘子随机放入一个水果(苹果or桔子),父亲放入水果的次数不少于11次。儿子只吃桔子,女儿只吃苹果。请编程使用信号量机制模拟解决此进程同步问题。打印信息包括盘子的情况、调度的情况以及父亲、儿子或者女儿执行的操作。
思路描述:
构造一个循环链表来模拟缓冲区的队列,两个移动的指针,一个代表儿子和女儿,一个代表父亲。当出现空盘时阻塞儿子和女儿,当盘子里出现了儿子和女儿要吃的的东西的时候唤醒他们,当盘子满了的时候,阻塞父亲,因为量比较小就没有构造阻塞队列了,直接用了个标志数组。
程序某次运行部分结果如下:
有一个盘子,可以放5个水果(苹果or桔子)。父亲每次向盘子随机放入一个水果(苹果or桔子),父亲放入水果的次数不少于11次。儿子只吃桔子,女儿只吃苹果。请编程使用信号量机制模拟解决此进程同步问题。打印信息包括盘子的情况、调度的情况以及父亲、儿子或者女儿执行的操作。
思路描述:
构造一个循环链表来模拟缓冲区的队列,两个移动的指针,一个代表儿子和女儿,一个代表父亲。当出现空盘时阻塞儿子和女儿,当盘子里出现了儿子和女儿要吃的的东西的时候唤醒他们,当盘子满了的时候,阻塞父亲,因为量比较小就没有构造阻塞队列了,直接用了个标志数组。
<span style="font-size:18px;">#include<stdio.h> #include<time.h> //定义表示盘子的结构体 struct Fruit { int kind;//0表示空盘,1表示苹果,2表示橘子 struct Fruit*next; }; //表示三者各自是否处于阻塞状态 //int sonBlockedFlag = 0;//1表示blocked,0表示ready //int daughterBlockedFlag = 0;//1表示blocked,0表示ready //int fatherBlockedFlag = 0; //1表示blocked,0表示ready //block[0],block[1],block[2]分别表示父亲,儿子,女儿 int block[3]; //传入可放水果的个数 //构造一个循环链表来模拟缓冲区的队列 struct Fruit *initFruit(int num) { int i; if(num == 0) { return; } struct Fruit *head = (struct Fruit*)malloc(sizeof(struct Fruit)); struct Fruit *p = head,*q; for(i = 0;i < num;i++) { q = (struct Fruit*)malloc(sizeof(struct Fruit)); q->kind = 0;//开始都是空盘 p->next = q; p = q; } q->next = head; return head; } //用来打印盘子现在的转态 void showPlate(struct Fruit *head) { if(!head) { return; } struct Fruit *p = head->next; printf("plate: |"); while(p != head) { if(p->kind == 0) { printf(" |"); p = p->next; } else { if(p->kind == 1) { printf("A|"); p = p->next; } else { printf("O|"); p = p->next; } } } printf("\n\n"); } int main() { struct Fruit *head = initFruit(5);//题目中说可以放5个水果 int schedulingCount,i; printf("Please input scheduling count:"); scanf("%d",&schedulingCount); //两个移动的指针,一个代表儿子和女儿,一个代表父亲 struct Fruit *sonOrDaughter = head; struct Fruit *father = head; int choice1;//用来存放产生的随机数,0调度父亲,1调度儿子,2调度女儿 int choice2;//用来存放产生的随机数,0放苹果,1放橘子 srand((unsigned)time(NULL)); for(i = 0;i < schedulingCount;i++) { //从未阻塞部分选取 do { choice1 = rand()%3; }while(block[choice1]); if(choice1 == 0) { printf("sched: father\n"); if(father->next == head) { father = father->next; } if(father->next->kind != 0) { printf("father: full, blocked me\n\n"); block[0] = 1; } else { choice2 = rand()%2; if(choice2 == 0) { father->next->kind = 1; printf("father: put an 'A'pple\n"); father = father->next; if(block[2] == 1) { block[2] = 0; printf("daughter:awake me!\n"); } showPlate(head); } else { father->next->kind = 2; printf("father: put an 'O'range\n"); father = father->next; if(block[1] == 1) { block[1] = 0; printf("son:awake me!\n"); } showPlate(head); } } } else { if(choice1 == 1) { printf("sched: son\n"); if(sonOrDaughter->next == head) { sonOrDaughter = sonOrDaughter->next; } if(sonOrDaughter->next->kind == 0) { printf("son: not orange, blocked me\n\n"); block[1] = 1; // if(block[0]) { // printf("father:awake me!\n"); // } } else { if(sonOrDaughter->next->kind == 1) { printf("son: not orange\n\n"); } else { printf("son: eat an orange\n"); sonOrDaughter->next->kind = 0; sonOrDaughter = sonOrDaughter->next; if(block[0]) { block[0] = 0; printf("father:awake me!\n"); } showPlate(head); } } } else { printf("sched: daughter\n"); if(sonOrDaughter->next == head) { sonOrDaughter = sonOrDaughter->next; } if(sonOrDaughter->next->kind == 0) { printf("daughter: not orange, blocked me\n\n"); block[2] = 1; // if(block[0]) { // printf("father:awake me!\n"); // } } else { if(sonOrDaughter->next->kind == 2) { printf("daughter: not Apple\n\n"); } else { printf("daughter: eat an Apple\n"); sonOrDaughter->next->kind = 0; sonOrDaughter = sonOrDaughter->next; if(block[0]) { block[0] = 0; printf("father:awake me!\n"); } showPlate(head); } } } } } }</span>
程序某次运行部分结果如下:
相关文章推荐
- 贪心-- HDU - 4296 Buildings
- Backbone系列:View的学习
- MongoDB文档的主键_id
- hdoj 1015 Safecracker 【DFS】
- POJ3181 Dollar Dayz
- dns记录整理
- nyoj 123 士兵杀敌(四)
- sass
- sublime text 2 调试python时结果空白
- 项目管理日志(一)
- Spring、Spring MVC、MyBatis整合文件配置详解
- 数学专题
- Xcode中使用Git
- 关于Spring与hibernate整合的困惑
- NSURLSession解决下载中的解压缩问题
- 使用$.getJSON实现跨域ajax请求
- java基础阶段关于斐波那契数列的问题
- mysql多实例的安装和管理(一台服务器上运行两个mysql实例)
- 9、Swift语言中switch条件语句的基本用法
- Wex5-util