操作系统课程设计报告苹果问题代码

上传人:206****923 文档编号:40461477 上传时间:2018-05-26 格式:DOC 页数:8 大小:50.50KB
返回 下载 相关 举报
操作系统课程设计报告苹果问题代码_第1页
第1页 / 共8页
操作系统课程设计报告苹果问题代码_第2页
第2页 / 共8页
操作系统课程设计报告苹果问题代码_第3页
第3页 / 共8页
操作系统课程设计报告苹果问题代码_第4页
第4页 / 共8页
操作系统课程设计报告苹果问题代码_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《操作系统课程设计报告苹果问题代码》由会员分享,可在线阅读,更多相关《操作系统课程设计报告苹果问题代码(8页珍藏版)》请在金锄头文库上搜索。

1、#include /用到了 time 函数,所以要有这个头文件#include /用到了 srand 函数,所以要有这个头文件#include int Plate_Size=0; /表示盘子中当前有几个水果int orange=0,apple=0; /表示盘子中 orange 和 apple 的个数bool Father_lag,Mother_lag,Son1_lag,Son2_lag,Daughter1_lag,Daughter2_lag; /六个进程处于等待时,变量值为 truevoid main() /main()函数,实现其他功能函数的调用 void Print(); /函数声明voi

2、d Father();void Mother();void Son1();void Son2();void Daughter1();void Daughter2();int k;srand(unsigned)time(NULL);/srand()函数产生一个以当前时间开始的随机种子 for(k=0;k10;k+) cout“第“k+1“次操作:“endl; /printf(“第%d 次操作:n“,k+1);int i=rand()%6; /随进生成 6 以内的整数,即 0-5 的 6 个整数Plate_Size=apple+orange;int MonFa_c,Daughter_b,Son_a

3、; /for 语句的局部变量,控制进程等待的优先次序,MonFa_c=2,Mather()执行,MonFa_c=1,Father()执行switch(i) /0 为 Father()调用,1 为 Mother()调用,2、3 为 Son1()、Son2()调,4、5 为 Daughteri()调用 case 0:cout“Father 调用.“endl;if(Plate_Size=2)Father_lag=true; /Father()等待Print();if(Mother_lag=false)MonFa_c=1; elseFather();if(Daughter1_lag=true)/Dau

4、ghter1 等待取消Daughter1(); /处于等待的 Daughter1 自动调用Daughter_b=2; /优先级让给 Daughter2else Daughter2_lag=false;/Daughter2 等待取消Daughter2(); /处于等待的 Daughter2()自动调用Daughter_b=1; /优先级让给 Daughter1elseif(Daughter1_lag=true)Daughter1_lag=false;/Daughter1 等待取消Daughter1(); /处于等待的 Daughter1()自动调用Daughter_b=0; /没有等待的 Dau

5、ther 进程else if(Daughter2_lag=true)Daughter2_lag=false;/Daughter2 等待取消Daughter2(); /处于等待的 Daughter2()自动调用Daughter_b=0;break;case 1:cout“Mother 调用.“endl;if(Plate_Size=2)Mother_lag=true; /Mother()等待Print();if(Father_lag=false)MonFa_c=2; elseMother();if(Son1_lag=true)/Son1 等待取消Son1(); /处于等待的 Son1()自动调用S

6、on_a=2; /优先级让给 Son2else Son2_lag=false;/Son2 等待取消Son2(); /处于等待的 Son2()自动调用Son_a=1; /优先级让给 Son1else if(Son1_lag=true)Son1_lag=false; /Son1 等待取消Son1(); /处于等待的 Son1()自动调用Son_a=0;else if(Son2_lag=true)Son2_lag=false; /Son2 等待取消Son2(); /处于等待的 Son2()自动调用Son_a=0;break;case 2: cout“Son1 调用.“endl;if(orange=0

7、)Son1_lag=true; /Son1 处于等待Print();if(Son2_lag=false)Son_a=1; /用于判断 Son1 和 Son2 等待的先后性elseSon1();if(Father_lag=true)Father();MonFa_c=2;else /Father 和 Mother 同时处于等待,但 Mother 先等待,因此先调用Mother_lag=false;Mother();MonFa_c=1;elseif(Father_lag=true) /只有 Father 处于等待,调用Father_lag=false;Father();MonFa_c=0;else

8、if(Mother_lag=true)/只有 Mother 处于等待,调用Mother_lag=false;Mother();MonFa_c=0;break;case 3:cout“Son2 调用.“endl;if(orange=0)Son2_lag=true; /Son2 处于等待Print();if(Son1_lag=false)Son_a=2;elseSon2();if(Father_lag=true)Father();MonFa_c=2;else /Father 和 Mother 同时处于等待,但 Mother 先等待,因此先调用Mother_lag=false;Mother();Mo

9、nFa_c=1;elseif(Father_lag=true) /只有 Father 处于等待,调用 FatherFather_lag=false;Father();MonFa_c=0;else if(Mother_lag=true) /只有 Mother 处于等待,调用 MotherMother_lag=false;Mother();MonFa_c=0;break;case 4:cout“Daughter1 调用.“endl;if(apple=0)Daughter1_lag=true; /Daughter1 等待Print();if(Daughter2_lag=false)Daughter_

10、b=1;elseDaughter1(); if(Father_lag=true)Father();MonFa_c=2;else /Father 和 Mother 同时处于等待,但 Mother 先等待,因此先调用Mother_lag=false;Mother();MonFa_c=1;elseif(Father_lag=true) /只有 Father 处于等待,调用Father_lag=false;Father();MonFa_c=0;else if(Mother_lag=true) /只有 Mother 处于等待,调用Mother_lag=false;Mother();MonFa_c=0;b

11、reak;case 5:cout“Daughter2 调用.“endl;if(apple=0)Daughter2_lag=true;/Daughter2 等待Print();if(Daughter1_lag=false)Daughter_b=2;elseDaughter2();if(Father_lag=true)Father();MonFa_c=2;else /Father 和 Mother 同时处于等待,但 Mother 先等待,因此先调用 Mother_lag=false;Mother();MonFa_c=1;else if(Father_lag=true) /只有 Father 处于等

12、待,调用 Father_lag=false;Father();MonFa_c=0;else if(Mother_lag=true) /只有 Mother 处于等待,调用 Mother_lag=false;Mother();MonFa_c=0;break; /switch 语句结束 /for 语句结束 /main 函数结束void Print() /Print 函数(打印盘子剩余水果及各进程等待状态)cout“现在盘子里有“apple“个苹果,“orange“个橘子,“共有“apple+orange“个水果.“endl;if(Father_lag=true)cout“Father 进程处于等待状

13、态,“;if(Mother_lag=true)cout“Mother 进程处于等待状态,“;if(Son1_lag=true)cout“Son1 进程处于等待状态,“;if(Son2_lag=true) cout“Son2 进程处于等待状态, “;if(Daughter1_lag=true)cout“Daughter1 进程处于等待状态,“;if(Daughter2_lag=true)cout“Daughter2 进程处于等待状态,“;if(Father_lag=false)void Father() /Father 进程apple+;Print();void Mother() /Mother 进程orange+;Print();void Son1() /Son1 进程orange-;Print();void Son2() /Son2 进程orange-;Print();void Daughter1() /Daughter1 进程apple-;Print();void Daughter2() /Daughter2 进程apple-;Print();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号