Note that there are some explanatory texts on larger screens.

plurals
  1. POMutual exclusion and semaphores
    text
    copied!<p>I am writing a program (for homework) that simulates a unisex bathroom. Only 4 people are allowed at a time and men and woman cannot enter if the other sex is already using the bathroom. My problem is with allowing a max of 4 people in the bathroom. As you can see from the output, only 1 person is getting into the restroom at a time. Here is my code:</p> <pre><code>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&lt;DelayTime; i++); } void Woman(void) { // for(;;){ Womenwaiting++; //wait(mutex); wait(woman); wait(max_capacity); //wait(woman); wait(mutex); wait(restroomcount); cout &lt;&lt; "A Woman has entered Restroom"&lt;&lt;endl; cout &lt;&lt; "People in the Restroom:" &lt;&lt; restroom++ &lt;&lt;endl &lt;&lt;endl; signal(restroomcount); Womenwaiting--; Delay(); wait(restroomcount); cout &lt;&lt; "A woman has exited Restroom"&lt;&lt;endl; cout &lt;&lt; "People in the Restroom:" &lt;&lt; restroom-- &lt;&lt;endl&lt;&lt;endl; signal(restroomcount); signal(mutex); signal(max_capacity); if(Menwaiting &gt; Womenwaiting){ signal(man); } else{ signal(woman); } //signal(max_capacity); //signal(man); // } } void Man(void) { // for(;;){ Menwaiting++; //wait(mutex); wait(man); wait(max_capacity); //wait(man); wait(mutex); wait(restroomcount); cout &lt;&lt;"A Man has entered the Restroom"&lt;&lt;endl; cout &lt;&lt;"People in the Restroom:" &lt;&lt; restroom++ &lt;&lt;endl&lt;&lt;endl; signal(restroomcount); Menwaiting--; //signal(mutex); Delay(); //wait(mutex); wait(restroomcount); cout &lt;&lt; "A man has exited the Restroom"&lt;&lt;endl; cout &lt;&lt;"People in the Restroom:" &lt;&lt; restroom-- &lt;&lt;endl&lt;&lt;endl; signal(restroomcount); signal(mutex); signal(max_capacity); if(Womenwaiting &gt; Menwaiting){ signal(woman); } else{ signal(man); } //signal(max_capacity); //signal(woman); //} } void main() { initialsem(woman,1); initialsem(man,1); initialsem(max_capacity,4); initialsem(mutex,1); initialsem(restroomcount,1); cobegin { Woman(); Woman(); Woman(); Woman(); Woman(); Man(); Man(); Man(); Man(); Man(); } } </code></pre> <p>This generates the following output:</p> <blockquote> <p>A Man has entered the Restroom <br>People in the Restroom:1</p> <p>A man has exited the Restroom <br>People in the Restroom:0</p> <p>A Man has entered the Restroom <br>People in the Restroom:1</p> <p>A man has exited the Restroom <br>People in the Restroom:0</p> <p>A Woman has entered Restroom <br>People in the Restroom:1</p> <p>A woman has exited Restroom <br>People in the Restroom:0</p> <p>A Woman has entered Restroom <br>People in the Restroom:1</p> <p>A woman has exited Restroom <br>People in the Restroom:0</p> </blockquote> <p>And so on, forever.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload