小编Jen*_*Jen的帖子

相互排斥和信号量

我正在写一个模拟男女皆宜的浴室的程序(用于家庭作业).一次只允许4个人,如果其他性别已经在使用浴室,男女不能进入.我的问题是允许最多4人在浴室.从输出中可以看出,一次只有一个人进入洗手间.这是我的代码:

const int Delayx = 60;
int i;
int restroom = 0;
int Menwaiting = 0;
int Womenwaiting = 0;
semaphore max_capacity;
semaphore woman;
semaphore man;
semaphore mutex;
semaphore restroomcount;
void Delay(void)
{
    int DelayTime;
    DelayTime = random(Delayx);
    for (i = 0; i<DelayTime; i++);
}

void Woman(void)
{
//  for(;;){
    Womenwaiting++;
    //wait(mutex);
    wait(woman);
    wait(max_capacity);
        //wait(woman);
        wait(mutex);
        wait(restroomcount);
        cout << "A Woman has entered Restroom"<<endl;
        cout << "People in the Restroom:" << restroom++ <<endl <<endl;
        signal(restroomcount);
        Womenwaiting--;
        Delay();
        wait(restroomcount);
        cout << …
Run Code Online (Sandbox Code Playgroud)

c++ mutex semaphore

11
推荐指数
1
解决办法
9350
查看次数

信号量和并发编程

对于家庭作业,我需要编写以下场景.这将使用BACI(即C--)使用信号量来完成

有2个男女皆宜的洗手间,每个可容纳4人.由于它是男女皆宜的,只有同性的人可以同时在洗手间,而FIFO并不重要.我脑子里有一个基本的"算法"来处理4个男人和4个女人的1个厕所.但我不知道如何编码.任何帮助将不胜感激.这就是我所拥有的.

Woman:

Check to see if there are any men in the restroom. If so "wait".
If no men check to see if there are 4 people. If so "wait".
If no men and not 4 use restroom. When leaving signal there is a vacancy.
If last woman signal the men if they are waiting if not signal the woman.


Man:

check to see if there are any woman in the restroom. if so "wait"
If no woman check …
Run Code Online (Sandbox Code Playgroud)

c concurrency semaphore

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

semaphore ×2

c ×1

c++ ×1

concurrency ×1

mutex ×1