Semaphores

In practice, semaphores can take on one of two forms:

  1. Binary semaphores can take on one of two values, 0 or 1. They can be used to solve the critical section problem and can be used as mutexes on systems that do not provide a separate mutex mechanism. The right-hand image on the previous slide is an example of such a binary semaphore.
  2. Counting semaphores can take on some non-zero integer value N (0, 1, 2, 3, ..., N), and are usually used to count the number of remaining limited resources. The counter is initialized to the number of such resources available in the system, and whenever the counting semaphore is greater than zero, a process can enter a critical section and use one of the resources. Otherwise, the process blocks until another process frees up a resource and increments the counting semaphore with a signal() call.
    A good real-life analogy is when people are trying to enter a room, elevator, etc., but there is space only for a limited number of individuals at a time. When the room's capacity is full, the other people must wait outside until someone exits the room.
    A binary semaphore is just a special case of a counting semaphore when N = 1.