Using Semaphores

The previous section focused on affecting changes in the semaphore and set attributes, and on obtaining information. This section will cover the aspects of using semaphores to perform the following:

  • Wait operations

  • Notify operations

  • Wait for zero operations

These operations work on the entire set of semaphores or on a subset. You can also operate on individual semaphores in the set according to your application needs.

Semaphore operations are performed by the semop(2) system call. Its function synopsis is as follows:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int semop(int semid, struct sembuf array[], unsigned nops);

The argument semid contains the IPC ID of the semaphore set, which is returned by the ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.