title: Standard atomic operations of semaphores
body: <p>I am self-learning C, and this is a practice problem on semaphores:</p>
  
  <blockquote>
    <p>Recall that a counting semaphore S is an integer variable that can
    only be accused via two standard atomic operations, P (probe) and V
    (signal), as shown in the figure:</p>
  
  <pre><code>/*The probe or wait operation */
  
  P(S) {
      while(S &lt;= 0); // do nothing
      S--;
  }
  
  /*The signal operation */
  
  V(S) {
      S++;
  }
  </code></pre>
    
    <p>The value of a counting semaphore can range over an unrestricted
    domain of integers (that is, the semaphore can hold an arbitrary
    value) whereas the value of a binary semaphore can only be 0 or 1.
    Show how counting semaphores can be implemented using only binary
    semaphores, and ordinary (that is, preemptible) machine instructions
    and data structures.</p>
    
    <p>Provide the pseudocode for the P and V operations.</p>
  </blockquote>
  
  <p>I found a related answer to this online: </p>
  
  <pre><code>struct semaphore {                                                        
      int value;                                                            
      queue L; // l list of processes                                       
  }                                                                         
  
  wait(S) {                                                                 
  
      if(s.value &gt; 0){                                                      
          s.value = s.value - 1;                                            
      } else {                                                              
          add this process to S.L;                                          
          block;                                                            
      }                                                                     
  }                                                                         
  
  signal(S){                                                                
  
      if(S.L != EMPTY) {                                                    
          remove a process P from S.L;                                      
          wakeup(P);                                                        
      } else {                                                              
          s.value = s.value + 1;                                            
      }                                                                     
  } 
  </code></pre>
  
  <p>But to be honest, I have no idea what it's trying to do. I would really appreciate it someone could explain the answer, or perhaps demonstrate in pseudo-code on how to answer this.</p>