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