Chapter 5: Locking and Interprocess Communication
ipcs_idr
struct sem_array
sem_pending
_last
sem_base
sem_
pending
struct
kern_ipc_perm
ID to pointer mapping
sma sma sma sma
struct task_struct
struct sem_queue
sembuf->sleeper
struct sem[]
struct
ipc_ids
Figure 5-2: Interplay between the semaphore data structures.
Starting from thesem_idsinstance obtained from the current namespace, the kernel travels viaipcs_idr
to the ID-to-pointer database and looks up the required instance ofkern_ipc_perm.Thekern_ipc_perm
entry can be type-cast into an instance of typesem_array. The current status of the semaphore is indicated
by linking with two further structures.
❑ The pending operations are managed in a linked list ofsem_queueinstances. Processes that are
sleeping while waiting for operation execution can also be determined from this list.
❑ An array ofstruct seminstances is used to hold the values of the individual semaphores of
the set.
Not shown is the information for managing undo operations because it is not particularly interesting and
would complicate matters unnecessarily.
kern_ipc_permis the first element of the data structure for managing IPC objects not only for semaphores
but also for message queues and shared memory objects. It enables the kernel to use the same code to
check access permissions in all three cases.
Eachsem_queueelement contains a pointer to an array ofsem_opsinstances that describe in detail the
operations to be performed on the semaphore. (An array ofsem_opsinstances is used because several
operations can be performed on the semaphores in a semaphore set using a singlesemctlcall.)
<sem.h>
struct sembuf {
unsigned short sem_num; /* semaphore index in array */
short sem_op; /* semaphore operation */
short sem_flg; /* operation flags */
};