Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


/* ipc stuff */
struct sysv_sem sysvsem;
#endif
...
};

Note that the SysV code is only compiled into the kernel if the configuration optionCONFIG_SYSVIPCis
set. Thesysv_semdata structure is used to encapsulate a further element.

sem.h
struct sysv_sem {
struct sem_undo_list *undo_list;
};

The only member,undo_list, is used to permit semaphore manipulations that can be undone. If a pro-
cess crashes after modifying a semaphore, the information held in the list is used to return the semaphore
to its state prior to modification. The mechanism is useful when the crashed process has made changes
after which processes waiting on the semaphore can no longer be woken. By undoing these actions (using
the information in the undo list), the semaphore can be returned to a consistent state, thus preventing
deadlocks. I won’t bother with the details here, however.

sem_queueis another data structure that is used to associate a semaphore with a sleeping process that
wants to perform a semaphore operation but is not allowed to do so at the moment. In other words, each
instance of the data structure is an entry in the list of pending operations.

<sem.h>
struct sem_queue {
struct sem_queue * next; /* next entry in the queue */
struct sem_queue ** prev; /* previous entry in the queue, *(q->prev) == q */
struct task_struct* sleeper; /* this process */
struct sem_undo * undo; /* undo structure */
int pid; /* process id of requesting process */
int status; /* completion status of operation */
struct sem_array * sma; /* semaphore array for operations */
int id; /* internal sem id */
struct sembuf * sops; /* array of pending operations */
int nsops; /* number of operations */
int alter; /* does the operation alter the array? */
};

For each semaphore, there is exactly one queue that manages all sleeping processes associated with the
semaphore. The queue is not implemented using standard kernel facilities but manually by means of
nextandprevpointers.

❑ sleeperis a pointer to the task structure of the process waiting for permission to perform a
semaphore operation.
❑ pidspecifies the PID of the waiting process.
❑ idholds the kernel-internal semaphore identifier.
Free download pdf