/*********************************************************************** * sempahore.c --- Concise illustration of POSIX unnamed semaphore * operations. * * To compile: * * gcc -o semaphore semaphore.c -lpthread * * (Semaphores are implemented as an extension of the Pthreads library, * hence the inclusion of the library.) ***********************************************************************/ /* Must include this header file. */ #include int main() { /* Declaring a semaphore. */ sem_t semaphore; /* Note that semaphores are passed by address. */ /* Semaphores must be initialized before first use. The first parameter * is the address of the semaphore to initialize. For us, the * second parameter should always be 0. The third parameter is the * semaphore's initial value: 0 or 1 for a binary semaphore; * 0 to n for a counting semaphore. */ sem_init(&semaphore, 0, 0); /* sem_post() is the semaphore signal operation */ sem_post(&semaphore); /* The semaphore wait operation, of course */ sem_wait(&semaphore); return 0; }