Semaphores

A UNIX semaphore keeps track of a count and notifies the interested processes when the count changes. The simplest semaphore is the binary semaphore, which can only hold the count of 0 or 1. A mutex is a simple form of a binary semaphore, which is used when programming with threads.

Other semaphores allow you to track n instances of a particular resource. For example, if you have three transaction servers available to serve client processes, you would initialize the semaphore to the count of 3. As clients attach to and reserve a transaction server, you would decrement this count. When the count reaches zero, the semaphore indicates that no remaining resources exist at this time. Later, when a client finishes with a transaction server, ...

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.