The semaphore type of IPC allows processes (executing programs) to communicate through the exchange of semaphore values. Since many applications require the use of more than one semaphore, the UNIX operating system has the ability to create sets or arrays of semaphores. A semaphore set can contain one or more semaphores up to a limit set by the system administrator. The tunable parameter, SEMMSL, has a default value of 25. Semaphore sets are created by using the semget (semaphore get) system call.
The process performing the semget system call becomes the owner/creator, determines how many semaphores are in the set, and sets the initial operation permissions for all processes, including itself. This process can subsequently relinquish ownership of the set or change the operation permissions using the semctl (semaphore control) system call. The creating process always remains the creator as long as the facility exists. Other processes with permission can use semctl to perform other control functions.
Any process can manipulate the semaphore(s) if the owner of the semaphore grants permission.
Each semaphore within a set can be incremented and decremented with the semop(2) system call.
To increment a semaphore, an integer value of the desired magnitude is passed to the semop system call. To decrement a semaphore, a minus (-) value of the desired magnitude is passed.
The UNIX operating system ensures that only one process can manipulate a semaphore set at any given time. Simultaneous requests are performed sequentially in an arbitrary manner.
A process can test for a semaphore value to be greater than a certain value by attempting to decrement the semaphore by one more than that value. If the process is successful, then the semaphore value is greater than that certain value. Otherwise, the semaphore value is not. While doing this, the process can have its execution suspended (IPC_NOWAIT flag not set) until the semaphore value would permit the operation (other processes increment the semaphore), or the semaphore facility is removed.
The ability to suspend execution is called a ``blocking semaphore operation.'' This ability is also available for a process which is testing for a semaphore equal to zero; only read permission is required for this test; it is accomplished by passing a value of zero to the semop (semaphore operation) system call.
On the other hand, if the process is not successful and did not request to have its execution suspended, it is called a ``nonblocking semaphore operation.'' In this case, the process is returned a known error code (-1), and the external errno variable is set accordingly.
The blocking semaphore operation allows processes to communicate based on the values of semaphores at different points in time. Remember also that IPC facilities remain in the UNIX operating system until removed by a permitted process or until the system is reinitialized.
Operating on a semaphore set is done by using the semop system call.
When a set of semaphores is created, the first semaphore in the set is semaphore number zero. The last semaphore number in the set is numbered one less than the total in the set.
A single system call can be used to perform a sequence of these ``blocking/nonblocking operations'' on a set of semaphores. When performing a sequence of operations, the blocking/nonblocking operations can be applied to any or all of the semaphores in the set. Also, the operations can be applied in any order of semaphore number. However, no operations are done until they can all be done successfully. For example, if the first three of six operations on a set of ten semaphores could be completed successfully, but the fourth operation would be blocked, no changes are made to the set until all six operations can be performed without blocking. Either the operations are successful and the semaphores are changed, or one (``nonblocking'') operation is unsuccessful and none are changed. In short, the operations are ``atomically performed.''
Remember, any unsuccessful nonblocking operation for a single semaphore or a set of semaphores causes immediate return with no operations performed at all. When this occurs, an error code (-1) is returned to the process, and the external variable errno is set accordingly.
System calls make these semaphore capabilities available to processes. The calling process passes arguments to a system call, and the system call either successfully or unsuccessfully performs its function. If the system call is successful, it performs its function and returns the appropriate information. Otherwise, a known error code (-1) is returned to the process, and the external variable errno is set accordingly.