next up previous contents
Next: 3.2.2.2 Message Passing Up: 3.2.2 Semaphores Previous: 3.2.2 Semaphores   Contents

3.2.2.1 Concept of semaphores

A semaphore is a structured data type consisting of

variables:
$ \bullet$ $ \mathbb{S}$- value (integer)
$ \bullet$ Queue
and operations ($ \bullet$ init):
$ \bullet$ $ \mathbb{P}$roberen $ \equiv$ WAIT
$ \bullet$ $ \mathbb{V}$erhogen $ \equiv$ SIGNAL


Operations $ \mathbb{P}$ and $ \mathbb{V}$ are atomic.

The operations are linked with the variables as follows :

$ \mathbb{P}$(s):   $ \mathbb{S}$-value $ =$ 0   $ \longrightarrow$  process in queue [train on the line]
    $ \mathbb{S}$-value $ >$ 0   $ \longrightarrow$  ( $ \mathbb{S}$-value)$  := 0$ and resources are available
         [empty line, enter it and set signal on ''red'']
$ \mathbb{V}$(s)  queue is empty   $ \longrightarrow$  ( $ \mathbb{S}$-value)$  := 1$ [set signal on ''green'']
   queue not empty   $ \longrightarrow$  activate a (waiting) process
         [let one stopped train enter the line, signal remains on ''red'']

The status query $ \mathbb{P}$(s) will be done only once per process, therefore the queue is necessary.

Examples for the use of semaphores

Dijkstra`s solution of ''Dinner for Five''

In the universe we assume declared
  1. the semaphore mutex initially $ 1$
  2. the INTEGER ARRAY C[0:4] initially all elements 0
  3. the semaphore INTEGER ARRAY prisem[0:4] initially all elements 0
  4. there exists a procedure
     procedure test (integer value k);
       if C[(k-1) mod 5] <> 2   and   C[k] == 1   
                                and   C[(k+1) mod 5] <> 2
          begin  C[k] := 2,  V(prisem[k])  end
     end
    
    Remark :
    $C[k] \;=\; \begin{cases}0 & \qquad\text{vacant seat} \\
1 & \qquad\text{occup...
...sopher} \\
2 & \qquad\text{occupied seat, 2 forks in hands} \\
\end{cases} $

    $ prisem[k] ==  1 \;\;\;$ philosopher $ k$ may start eating.

    For philosopher $ k$ the above procedure consists in the queries
    • Is it true that the left neighbor has not two forks in his hands , i.e, that there is no fork at all in his hands ? ($ =$ he does not eat)
    • Do I sit on the table and do I have hunger ?
    • Is it true that the right neighbor has not two forks in his hands ?
    If the answer to all queries is ''yes'', then philosopher $ k$ takes the 2 forks and starts to eat.
  5. In this the live of philosophers can be coded (philosopher $ w$):
     1:     cycle begin  think
     2:                  P(mutex)
     3:                    C[w] := 1;  test(w)
     4:                  V(mutex)
     5:                  P(prisem[w]);  eat
     6:                  P(mutex)
     7:                    C[w] := 0;  test[(w+1) mod 5];  
                                       test[(w-1) mod 5]
     8:                  V(mutex)
     9:           end
    
    Explanation:
    3:
    Sit down and take 2 forks if possibly.
    5:
    Do I have permission to eat ? If answer is ''yes'', then start eating. Otherwise, wait until the permission will be given (i.e., semaphore prisem[w] is deleted)
    7:
    After the meal is finished, leave the seat and your neighbors will check whether both forks are vacant.
Further explanations can be found in the journal c't 12/90 at page 246.
next up previous contents
Next: 3.2.2.2 Message Passing Up: 3.2.2 Semaphores Previous: 3.2.2 Semaphores   Contents
Gundolf Haase 2000-03-20