title: Semaphores in Sleeping Professor Problem code tracing?
body: <p>I'm working on some semaphore problems in a Sleeping Professor scenario. It uses the counting semaphores model. These are the basic guidelines:</p>
  
  <ol>
  <li>The waiting room (critical section) has 3 chairs. The professor's office has 1 chair to accommodate one student at a time.</li>
  <li>If there are no students in the office, the professor will fall asleep.</li>
  <li>When a student arrives, the professor will wake up. If the professor is asleep, the student awakes the professor.</li>
  <li>If student comes to the waiting area and all chairs are occupied, the student leaves.</li>
  <li>If professor is busy with another student, but waiting space is available, the student waits in one of the free chairs.</li>
  </ol>
  
  <p>Right now, this is the pseudocode I've written up for the Sleeping Professor Problem:</p>
  
  <pre><code>/* Counting semaphores - the integer value represents the initial count for the semaphores */
  
  Semaphore students = 0; /* Number of students waiting for service */
  Semaphore professor = 0; /* Number of professors waiting for students */
  Semaphore mutex = 1; /* Mutual exclusion when accessing the waiting room */
  
  int waiting = 0; /* Students waiting for turn with professor */
  
  Professor() {
    while (office hours) {
      wait (students); /* Go to sleep if no students */
      wait (mutex); /* Get access to waiting room */
      waiting = waiting - 1; /* Decrement number of waiting students */
      signal (professor); /* One professor is ready */
      signal (mutex); /* Releasing waiting room */
      ConsultWithStudent();
    }
  }
  
  Student() {
    wait (mutex); /* Enter critical section, which is the waiting room */
    if (waiting &lt; 3) { /* If there are free chairs in the waiting room */
      waiting = waiting + 1;
      signal (students); /* Wake up professor is necessary */
      signal (mutex); /* Release access to count of waiting students */
      wait (professor); /* Wait for professor if not available */
      GetHelpFromProfessor();
    } else {
      signal (mutex); /* Waiting area is full, leave without waiting */
    }
  }
  </code></pre>
  
  <p>I have a couple of code tracing problems that I have worked through, and was wondering if my semaphore count was right for each one:</p>
  
  <p><strong>Scenario 1:</strong> Assume the professor arrives for her office hours and then a student arrives. Assume the professor is currently consulting with this student. The semaphore counts are as follows:</p>
  
  <p><strong>Semaphore</strong> <strong>Count</strong></p>
  
  <p>Students  1</p>
  
  <p>Mutex     0</p>
  
  <p>Professor 1</p>
  
  <p><strong>Scenario 2:</strong> While the professor is talking with the first student, 4 more students arrive. Show the resulting count for the semaphores:</p>
  
  <p><strong>Semaphore</strong> <strong>Count</strong></p>
  
  <p>Students  1</p>
  
  <p>Mutex     3</p>
  
  <p>Professor 1</p>
  
  <p><strong>Scenario 3:</strong> The professor finishes talking with the first student, and that student leaves. The professor begins talking with second student. Show the resulting count for the semaphores while the professor is talking with the second student:</p>
  
  <p><strong>Semaphore</strong> <strong>Count</strong></p>
  
  <p>Students  1</p>
  
  <p>Mutex     2</p>
  
  <p>Professor 1</p>
  
  <p>Could someone please help me review my work? Yes, this is for homework and I am trying to understand semaphores after producing the pseudocode (<a href="https://www.geeksforgeeks.org/sleeping-barber-problem-in-process-synchronization/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/sleeping-barber-problem-in-process-synchronization/</a>) myself from a similar barber problem.</p>