10.5. Styles of use of semaphores

Semaphores can be used for a range of purposes. In the following sections we will see that they can be used to achieve (1) mutual exclusion; (2) synchronization of cooperating processes and; (3) management of multiple resource allocation. These different usages are achieved through varying the initial integer value of the semaphore as well as working with different orderings of the semWait () and semSignal () methods.

10.5.1. Mutual exclusion

A semaphore initialized to 1 may be used to provide exclusive access to a shared resource such as a data structure.

MyClass DataStructure // shared data structure 

Semaphore lock = new SemaphoreImplementation(1);

To use a semaphore:

 non-critical instructions lock.semWait(); ...

Get Operating Systems: Concurrent and Distributed Software Design 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.