Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since you want to know <code>how to code your algorithm for 1 restroom</code>, I have done so in C. It will be a fairly simple task to convert it into C--, as all the semaphore constructs appear quite similar.</p> <p>From what I could make of your answer,</p> <pre><code>C: sem_wait() C--: wait() sem_post() signal() sem_t semaphore() sem_init() initialsem() </code></pre> <p>Bear in mind, as stated, I have worked out the problem for <strong>1-restroom</strong> only. Since this is homework, I expect you to expand it into the <strong>2-restrooms</strong> form yourself.</p> <p>Working one's way from the <a href="http://en.wikipedia.org/wiki/Readers-writers_problem" rel="nofollow">Readers-writers problem</a> to our "Unisex Restroom" problem, we make use of the following global variables:</p> <pre><code>int mcount,wcount; // count of number of men/women in restroom sem_t x,y,z; // semaphores for updating mcount &amp; wcount values safely sem_t wsem,msem; // semaphores to block other genders' entry sem_t cap; // capacity of the restroom </code></pre> <p>Incorporating these semaphores &amp; counters into the <code>man</code> thread function,</p> <pre><code>void *man(void *param) { sem_wait(&amp;z); sem_wait(&amp;msem); sem_wait(&amp;x); mcount++; if(mcount==1) { sem_wait(&amp;wsem); } // first man in, make women wait sem_post(&amp;x); sem_post(&amp;msem); sem_post(&amp;z); sem_wait(&amp;cap); //wait here, if over capacity printf("\t\tman in!\n"); delay(); printf("\t\t\tman out!\n"); sem_post(&amp;cap); //one man has left, increase capacity sem_wait(&amp;x); mcount--; if(mcount==0) {sem_post(&amp;wsem);} // no man left, signal women sem_post(&amp;x); } </code></pre> <p>Similarly, the woman thread function, substitutes <code>mcount</code> with <code>wcount</code>, <code>msem</code> with <code>wsem</code>, and <code>x</code> with <code>y</code>. Only <code>z</code> remains as is in the <code>man</code> function, so that both <code>man</code> &amp; <code>woman</code> threads queue up on the same common semaphore. (Due to this, the code invariably has <strong>FIFO-like</strong> behaviour, which ensures <strong>fairness/non-starvation</strong>)</p> <p>The complete code is as follows: (To compile, use <code>gcc filename -lpthread</code>)</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;pthread.h&gt; #include &lt;semaphore.h&gt; int mcount,wcount; sem_t x,y,z,wsem,msem,cap; void delay(void) { int i; int delaytime; delaytime = random(); for (i = 0; i&lt;delaytime; i++); } void *woman(void *param) { sem_wait(&amp;z); sem_wait(&amp;wsem); sem_wait(&amp;y); wcount++; if(wcount==1) { sem_wait(&amp;msem); } sem_post(&amp;y); sem_post(&amp;wsem); sem_post(&amp;z); sem_wait(&amp;cap); printf("woman in!\n"); delay(); printf("\twoman out!\n"); sem_post(&amp;cap); sem_wait(&amp;y); wcount--; if(wcount==0) { sem_post(&amp;msem); } sem_post(&amp;y); } void *man(void *param) { sem_wait(&amp;z); sem_wait(&amp;msem); sem_wait(&amp;x); mcount++; if(mcount==1) { sem_wait(&amp;wsem); } sem_post(&amp;x); sem_post(&amp;msem); sem_post(&amp;z); sem_wait(&amp;cap); printf("\t\tman in!\n"); delay(); printf("\t\t\tman out!\n"); sem_post(&amp;cap); sem_wait(&amp;x); mcount--; if(mcount==0) {sem_post(&amp;wsem);} sem_post(&amp;x); } int main(void) { int i; srandom(60); mcount = 0; wcount = 0; sem_init(&amp;x,0,1); // for sem_init, initial value is 3rd argument sem_init(&amp;y,0,1); sem_init(&amp;z,0,1); sem_init(&amp;wsem,0,1); sem_init(&amp;msem,0,1); sem_init(&amp;cap,0,4); // eg. cap initialized to 4 pthread_t *tid; tid = malloc(80*sizeof(pthread_t)); // You can use your cobegin statement here, instead of pthread_create() // I have forgone the use of pthread barriers although I suppose they would nicely imitate the functionality of cobegin. // This is merely to retain simplicity. for(i=0;i&lt;10;i++) { pthread_create(&amp;tid[i],NULL,woman,NULL); } for(i=10;i&lt;20;i++) { pthread_create(&amp;tid[i],NULL,man,NULL); } for(i=0;i&lt;20;i++) { pthread_join(tid[i],NULL); } return(0); } </code></pre> <p>While converting into the <strong>2-restrooms</strong> form, make note of which semaphores &amp; counter variables you would need to duplicate to satisfy all the conditions. Happy <em>semaphoring</em>!</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