title: How to interpret semaphore values on a Linux system from a php process?
body: <p>The basis for my problem is essentially the <a href="http://linux.die.net/man/2/semop" rel="nofollow">man page of semop</a>, which performs operations on semaphores on a <code>2.6.30-gentoo-r4</code> Linux system. To investigate a problem regarding the use of semaphores in <a href="http://www.php.net/manual/en/book.sem.php" rel="nofollow">php</a> I am looking at the values <code>semval</code>, <code>semzcnt</code>, <code>semncnt</code> and <code>sempid</code> for the semaphore defined by <code>semid=0</code> and <code>semnum=0</code> (see <a href="http://linux.die.net/man/2/semctl" rel="nofollow">semctl</a>). These four values are checked by a small C program and written to screen whenever one of their values are changing. Here is an example output, including timestamp: </p>
  
  <pre><code>Status at 1415851183.974700:  semval=0  semzcnt=0  semncnt=0  sempid=1244
  Status at 1415851183.977678:  semval=1  semzcnt=0  semncnt=0  sempid=1244
  Status at 1415851184.630576:  semval=0  semzcnt=0  semncnt=0  sempid=1207
  Status at 1415851184.721588:  semval=1  semzcnt=0  semncnt=0  sempid=1207
  </code></pre>
  
  <p>My interpretation is, that the first line indicates process 1244 took the only available resource from the semaphore, and returned it 3 ms later. About one second after that, another process with PID 1207 took the resource (decrementing the semaphore value from 1 to zero), and returned it about 100ms later. </p>
  
  <p>However, I also saw the following output: </p>
  
  <pre><code>Status at 1415851188.925930:  semval=0  semzcnt=0  semncnt=0  sempid=4869
  Status at 1415851188.928922:  semval=1  semzcnt=0  semncnt=0  sempid=4869
  Status at 1415851188.965917:  semval=0  semzcnt=0  semncnt=0  sempid=4173
  Status at 1415851189.046912:  semval=0  semzcnt=0  semncnt=1  sempid=4173
  Status at 1415851189.237873:  semval=0  semzcnt=0  semncnt=2  sempid=4173
  Status at 1415851189.249882:  semval=0  semzcnt=0  semncnt=3  sempid=4173
  Status at 1415851189.275878:  semval=0  semzcnt=0  semncnt=4  sempid=4173
  Status at 1415851189.311886:  semval=0  semzcnt=0  semncnt=5  sempid=4173
  </code></pre>
  
  <p>Here it seems that process with PID 4173 is using the resource (<code>semval</code> decremented to zero) but not returning the resource. After that, the same resource is required (seemingly by the same process), but the semaphore blocks since the first resource is not returned yet. </p>
  
  <p>The problem here is the statement of the documentation of <a href="http://linux.die.net/man/2/semop" rel="nofollow">semop</a> for semid: </p>
  
  <blockquote>
  <pre><code>pid_t           sempid;   /* ID of process that did last op */
  </code></pre>
  </blockquote>
  
  <p>This can't be true and it is not true! Because I do a parallel logging on the semaphore operations in the PHP code (in which different PHP processes use the same function), I see that those <code>semop</code>'s act on different processes with different <code>pid</code>'s. I would argue that the documentation for <code>semop</code> is not quite correct, as <code>sempid</code> seems to indicate the process PID of the last <em>successful</em> op, or something like that. </p>
  
  <p>However, is my interpretation correct that the process 4173 somehow blocks the resource/semaphore, and therefore all the other processes which would like to get the resource are blocked? Furthermore, from debugging <code>php</code> code I see that<code>sem_acquire</code> (around time 1415851188.965917) never gets back to the <code>php</code> interpreter, and thus blocks the process 4173. But, as we see, process 4173 successfully decreases <code>semval</code> by one to zero, and everything should be ok. Maybe this is a bug in php?</p>