Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


characteristics (value, read, and write permissions,etc.), but also for associating semaphores with waiting
processes by means of a waiting list.

Starting with kernel 2.6.19, the IPC mechanism is aware of namespaces (see Chapter 2 for more informa-
tion about this concept). Managing IPC namespaces is, however, simple because they are not hierarchi-
cally related. A given task belongs to the namespace pointed at bytask_struct->nsproxy->ipc_ns,and
the initial default namespace is implemented by the staticipc_namespaceinstanceinit_ipc_ns.Each
namespace carries essentially the following information:

<ipc.h>
struct ipc_namespace {
...
struct ipc_ids *ids[3];

/* Resource limits */
...
}

I have omitted a large number of elements devotedto observing resource consumption and setting
resource limits. The kernel, for instance, restricts the maximum number of shared memory pages, the
maximum size for a shared memory segment, the maximum number of message queues, and so on.
All restrictions apply on a per-namespace basis and are documented in the manual pagesmsgget(2),
shmget(2),andsemget(2), so I will not discuss them further here. All are implemented by simple
counters.

More interesting is the arrayids. One array position per IPC mechanism — shared memory, semaphores,
and messages — exists, and each points to an instance ofstruct ipc_idsthat is the basis to keep track
of the existing IPC objects per category. To preventgetting lost in search of the proper array index per
category, the kernel provides the auxiliary functionsmsg_ids,shm_ids,andsem_ids. But just in case
you were wondering, semaphores live in position 0, followed by message queues and then by shared
memory.

struct ipc_idsis defined as follows:

ipc/util.h
struct ipc_ids {
int in_use;
unsigned short seq;
unsigned short seq_max;
struct rw_semaphore rw_mutex;
struct idr ipcs_idr;
};

The first elements hold general information on the status of the IPC objects:

❑ in_useholds the number of IPC objects currently in use.
❑ seqandseq_idallow generating userspace IPC identifiers sequentially. Note that the identifiers
are not identical with the sequence numbers, though. The kernel identifies an IPC object inter-
nally with an identifier managed per resource type, that is, one identifier for message queues,
one for semaphores, and one for shared memory objects. Each time a new IPC object is created,
the sequence number is incremented by1 (wrapping is handled automatically).
Free download pdf