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, it increments the semaphore count. When all clients complete, the count increments to the initial count of 3. In this manner, the semaphore tracks the number of available resources.

The act of decrementing a semaphore count is known as waiting on the semaphore. This makes sense when you consider that when the count reaches zero, the requestor must wait for the resource to become available.

The act of incrementing a semaphore count is known as notifying the semaphore. The increment of the count causes processes that are waiting for a free resource to be notified that it is now available.

Individual semaphores work well for controlling individual resources. However, obtaining several resources at once is often required. Imagine a small bowling alley that has 50 pairs of bowling shoes, 30 bowling balls, and 6 bowling alleys available. To bowl, a patron needs one pair of shoes, a bowling ball, and an alley. However, a patron cannot bowl if any of the resources—shoes, balls, or alley—are unavailable. A semaphore set permits the caller to request all of the resources at once. In this way, there is no potential for deadlocks, since the request either completely succeeds or it fails (waits).

Figure 22.5 illustrates a semaphore set, which tracks 30 bowling balls, 50 pairs of bowling shoes, and 12 bowling alleys.

Figure 22.5. A semaphore set.


The figure illustrates one semaphore set. Within the set, semaphore 0 controls the resource "bowling balls," semaphore 1 controls the resource "bowling shoes," and semaphore 2 controls "bowling alleys." It is not necessary to request all of the resources in a semaphore set. A patron may choose to bring his own shoes or bowling ball. A group of patrons usually shares a bowling alley, and so the total number shoes, bowling balls, and one bowling alley would be requested.

The benefit of grouping these resources into one set is that the caller can obtain all resources needed in one system call, without worrying about deadlock situations. If any of the requested resources are not available, the caller simply waits until all resources become available.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.151.141