#include<iostream>
#include<time.h>
using namespace std;
int apple=0;
int orange=0;
int Plate_Size=0;
int Son_a,Daughter_b,MonFa_c;
bool Father_lag;
bool Mother_lag;
bool Son1_lag;
bool Son2_lag;
bool Daughter1_lag;
bool Daughter2_lag;
void Print()
{
cout<<"现在盘子里有"<<apple<<"个苹果,"<<orange<<"个橘子,"<<"共有"<<apple+orange<<"个水果."<<endl;
if(Father_lag==true)
cout<<"Father进程处于等待状态,";
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)&&(Mother_lag==false)&&(Son1_lag==false)&&(Son2_lag==false)&&(Daughter1_lag==false)&&(Daughter2_lag==false))!=true)
cout<<endl;
}
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();
}
void main()
{
int k;
srand((unsigned)time(NULL));//srand()函数产生一个以当前时间开始的随机种子
for(k=0;k<10;k++)
{
cout<<"第"<<k+1<<"次操作:"<<endl;
int i=rand()%6; //随进生成1—5.
Plate_Size=apple+orange;
switch(i)
{
case 0:
cout<<"Father调用."<<endl;
if(Plate_Size==2)
{
Father_lag=true;//Father()等待
Print();
if(Mother_lag==false)
MonFa_c=1;
}
else
{
Father();
if((Daughter1_lag==true)&&(Daughter2_lag==true))
{
if(Daughter_b==1)
{
Daughter1_lag=false;//Daughter1等待取消
cout<<"处于等待的Daughter1自动被调用"<<endl;
Daughter1(); //处于等待的Daughter1自动调用
Daughter_b=2;
}
else
{
Daughter2_lag=false;//Daughter2等待取消
cout<<"处于等待的Daughter2自动被调用"<<endl;
Daughter2(); //处于等待的Daughter2()自动调用
Daughter_b=1;
}
}
else
{
if(Daughter1_lag==true)
{
Daughter1_lag=false;//Daughter1等待取消
cout<<"处于等待的Daughter1自动被调用"<<endl;
Daughter1(); //处于等待的Daughter1()自动调用
Daughter_b=0;
}
else if(Daughter2_lag==true)
{
Daughter2_lag=false;//Daughter2等待取消
cout<<"处于等待的Daughter1自动被调用"<<endl;
Daughter2(); //处于等待的Daughter2()自动调用
Daughter_b=0;
}
}
}
break;
case 1:
cout<<"Mother调用."<<endl;
if(Plate_Size==2)
{
Mother_lag=true; //等待
Print();
if(Father_lag==false)
MonFa_c=2;
}
else
{
Mother();
if((Son1_lag==true)&&(Son2_lag==true))
{
if(Son_a==1)
{
Son1_lag=false;//Son1等待取消
cout<<"处于等待的Son1自动被调用"<<endl;
Son1(); //处于等待的Son1()自动调用
Son_a=2;
}
else
{
Son2_lag=false;//Son2等待取消
cout<<"处于等待的Son2自动被调用"<<endl;
Son2(); //处于等待的Son2()自动调用
Son_a=1;
}
}
else
{
if(Son1_lag==true)
{
Son1_lag=false; //Son1等待取消
cout<<"处于等待的Son1自动被调用"<<endl;
Son1(); //处于等待的Son1()自动调用
Son_a=0;
}
else if(Son2_lag==true)
{
Son2_lag=false; //Son2等待取消
cout<<"处于等待的Son2自动被调用"<<endl;
Son2(); //处于等待的Son2()自动调用
Son_a=0;
}
}
}
break;
case 2:
cout<<"Son1调用."<<endl;
if(orange==0)
{
Son1_lag=true; //Son1处于等待
Print();
if(Son2_lag==false)
Son_a=1; //用于判断Son1和Son2等待的先后性
}
else
{
Son1();
if((Father_lag==true)&&(Mother_lag==true))
{
if(MonFa_c==1) //Father和Mother同时处于等待,但Father先等待,因此先调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=2;
}
else //Father和Mother同时处于等待,但Mother先等待,因此先调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=1;
}
}
else
{
if(Father_lag==true) //只有Father处于等待,调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=0;
}
else if(Mother_lag==true)//只有Mother处于等待,调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
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;
}
else
{
Son2();
if((Father_lag==true)&&(Mother_lag==true))
{
if(MonFa_c==1)//Father和Mother同时处于等待,但Father先等待,因此先调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=2;
}
else //Father和Mother同时处于等待,但Mother先等待,因此先调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=1;
}
}
else
{
if(Father_lag==true) //只有Father处于等待,调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=0;
}
else if(Mother_lag==true) //只有Mother处于等待,调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=0;
}
}
}
break;
case 4:
cout<<"Daughter1调用."<<endl;
if(apple==0)
{
Daughter1_lag=true; //Daughter1等待
Print();
if(Daughter2_lag==false)
Daughter_b=1;
}
else
{
Daughter1();
if((Father_lag==true)&&(Mother_lag==true))
{
if(MonFa_c==1) //Father和Mother同时处于等待,但Father先
{ //等待,因此先调用
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=2;
}
else //Father和Mother同时处于等待,但Mother先等待,因此先调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=1;
}
}
else
{
if(Father_lag==true) //只有Father处于等待,调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=0;
}
else if(Mother_lag==true) //只有Mother处于等待,调用
{
Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=0;
}
}
}
break;
case 5:
cout<<"Daughter2调用."<<endl;
if(apple==0)
{
Daughter2_lag=true; //Daughter2等待
Print();
if(Daughter1_lag==false)
Daughter_b=2;
}
else
{
Daughter2();
if((Father_lag==true)&&(Mother_lag==true))
{
if(MonFa_c==1) //Father和Mother同时处于等待,但Father先等待,因此先调用
{
Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=2;
}
else //Father和Mother同时处于等待,但Mother先等待,因此先调用
{ Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=1;
}
}
else
{
if(Father_lag==true) //只有Father处于等待,调用
{ Father_lag=false;
cout<<"处于等待的Father自动被调用"<<endl;
Father();
MonFa_c=0;
}
else if(Mother_lag==true) //只有Mother处于等待,调用
{ Mother_lag=false;
cout<<"处于等待的Mother自动被调用"<<endl;
Mother();
MonFa_c=0;
}
}
}
break;
}
}
}
jincheng.rar_进程同步
版权申诉
49 浏览量
2022-09-24
01:39:52
上传
评论
收藏 2KB RAR 举报
四散
- 粉丝: 49
- 资源: 1万+
最新资源
- STM8L101F3P6单片机+CC1100模块433M遥控器设计硬件(原理图+PCB)工程文件.zip
- 上传下载铁人下载系统 Liuxing 1.0-liuxing1.0.rar
- 南京邮电大学数学实验实力雄厚,凭借其优秀的师资力量、丰富的实践教学资源和卓越的科研成果,成为国内一流的数学实验教学和科研基地
- 【火爆朋友圈的今天吃什么源码 v1.0】随机的为用户带来每一天的用餐选择和推荐.rar
- MPU6050中文版数据手册
- 上传下载手机电影下载-mobiledy.rar
- 响应式旅游网站源码下载 马尔代夫旅游网站.rar
- CMS小涴熊漫画连载系统漫画网站源码 带采集API.rar
- 福袋点点.apk
- 基于STM32的电子秤采用0.96寸OLED显示UI界面源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈