#define N 20
#define NULL 0
#define BOY 1
#define GIRL 2
typedef int semaphore;
semaphore mutex=1;
semaphore empty=N;
semaphore full=0;
bool mutexFlag;
int state;
void want_to_bathe(sex){
if(state==NULL){
down(&mutex);
down(&empty);
up(&full);
state=sex;
bathe();
down(&full);
up(&empty);
upOrNot(&mutex);
}
if(state==sex){
down(&empty);
up(&full);
bathe();
down(&full);
up(&empty);
upOrNot(&mutex);
}
if(state!=NULL&&state!=sex){
down(&mutex);
}
}
Void upOrtNot(int mutex){
mutexFlag=(full==0&&empty==N)?true:false;
if(mutexFlag){
state=NULL;
up(&mutex);
}
}
进程间通信 男女生洗澡问题
最新推荐文章于 2022-04-24 22:32:07 发布