O'Reilly logo

Operating Systems: Concurrent and Distributed Software Design by Tim Harris, Jean Bacon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required