Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


0 specifies that we want to manipulate the semaphore with identifier 0 in our semaphore set (this is the
only semaphore in our set). The meaning ofSETVAL, 1is obvious — the semaphore value is to be set to 1.^9

The familiarupanddownoperations are implemented by procedures of the same name. How the
semaphore value is modified in the SysV scheme is interesting. Operations are performed using the
semopsystem call, and, as usual, thesemidvariable is used to identify the desired semaphore. Of
particular note are the last two arguments. One is a pointer to an array withsembufelements, each of
which represents a semaphore operation. The number of operations in the array is defined by an integer
argument because the kernel cannot otherwise identify the operations.

Entries in thesembufarray consist of three elements with the following meanings:


  1. The first entry serves to select the semaphore in the semaphore set.

  2. The second entry specifies the desired operation. 0 waits until the value of the semaphore
    reaches 0; a positive number is added to thevalue of the semaphore (and corresponds to
    releasing a resource; the process cannot go to sleep with this action); a negative number is
    used to request resources. If the absolute value is less than the value of the semaphore, its
    (absolute) value is subtracted from the current semaphore value without sleeping on the
    semaphore; otherwise, the process blocks until the semaphore value reaches a value that
    allows the operation to be performed.

  3. The third entry is a flag used for fine control of the operation.


The behavior of a semaphore can be simulated by using 1 and-1as numeric arguments.downtries to
subtract 1 from the semaphore counter (and goes to sleep when the semaphore value reaches 0), whileup
adds 1 to the semaphore value and therefore corresponds to releasing a resource.

The code yields the following result:

wolfgang@meitner>./sema
Create the semaphore
Before the critical code
In the critical code

The program creates the semaphore, enters the critical code, and waits there for 10 seconds. Before the
code is entered, adownoperation is performed to decrement the semaphore value to 0. A second process
started during the wait period is not allowed to enter critical code.

wolfgang@meitner>./sema
Before the critical code

Any attempt to enter critical code triggers adownoperation, which tries to subtract 1 from the semaphore
value. This fails because the current value is 0. The process goes to sleep on the semaphore. It is not
woken until the first process has released the resource by means of anupoperation (and the semaphore
value has reverted to 1). It can then decrement the semaphore value and enter the critical code.

Data Structures


The kernel uses several data structures to describe the current status of all registered semaphores and
to build a kind of network. They are responsible not only for managing the semaphores and their

(^9) For the sake of simplicity, we do not query for errors in our sample program.

Free download pdf