Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


❑ sopsis a pointer to an array that holds the pending semaphore operations (a further data struc-
ture discussed below is used to describe the operations themselves). The number of operations
(i.e., the size of the array) is defined insops.
❑ alterindicates whether the operations alter the value of the semaphore (e.g., a status query
leaves the value unchanged).

smaholds a pointer to an instance of the data structure used to manage the semaphore status.



struct sem_array {
struct kern_ipc_perm sem_perm; /* permissions .. see ipc.h */
time_t sem_otime; /* last semop time */
time_t sem_ctime; /* last change time */
struct sem *sem_base; /* ptr to first semaphore in array */
struct sem_queue *sem_pending; /* pending operations to be processed */
struct sem_queue **sem_pending_last; /* last pending operation */
struct sem_undo *undo; /* undo requests on this array */
unsigned long sem_nsems; /* no. of semaphores in array */
};

There is exactly one instance of this data structure in the system for each semaphore set. The instance is
used to manage all the semaphores that make up the set.


❑ Semaphore access permissions are held insem_permof the familiarkern_ipc_permtype. This
must be located at the beginning of the structure so that a trick can be used involving the
ipc_ids->entriesarrays employed to manage all semaphore sets. Because the individual
elements point to areas in which sufficient memory is reserved not only forkern_ipc_permbut
also forsem_array, the kernel can switch between both representations by means of typecasts.
This trick is also used for other SysV-IPC objects, as you will see further below.
❑ sem_nsemsspecifies the number of semaphores in a user semaphore.
❑ sem_baseis an array, each of whose entries describes a semaphore in the set. It holds the current
semaphore value and the PID of the process that last accessed it.
<sem.h>
struct sem {
int semval; /* current value */
int sempid; /* pid of last operation */
};

❑ sem_otimespecifies the time of the last access to the semaphore in jiffies (including, e.g., infor-
mation queries).sem_ctimespecifies the time the semaphore value was last changed.
❑ sem_pendingpoints to a linked list of pending semaphore operations. The list consists of
sem_queueinstances.sem_pending_lastis used to quickly access the last element in the list,
whereassem_pendingpoints to the start of the list.

Figure 5-2 shows the interplay between the data structures involved.

Free download pdf