Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


that can be shared by several processes at the same time. This would seem to be logical for IPC mecha-
nisms but nevertheless should not be taken for granted. For example, the mechanisms could have been
designed in such a way as to ensure that only the threads of a program or a structure generated by afork
are able to access shared SysV objects.

Before individual SysV elements can be accessed by various independent processes, they must be
uniquely identifiable in the system. To this end, each IPC structure is assigned a number when it
is created. Each program with knowledge of this magic number is able to access the corresponding
structure. If independent applications are to communicate with each other, this number is usually
permanently compiled into the individual programs. An alternative is to dynamically generate a magic
number that is guaranteed to be unique (statically assigned numbers cannot be guaranteed to be unique).
The standard library provides several functions to do this (see the relevant system programming
manuals for detailed information).

A privilege system based on the system adopted for file access permissions is used to access IPC objects.
Each object has a user ID and a group ID that depend on the UID/GID under which the program that
generated the IPC object is running. Read and write permissions are assigned at initialization. As with
normal files, these govern access for three different user classes — owner, group, and others. Detailed
information on how this is done is provided in the corresponding system programming manuals.

The flag 0666 must be specified to create a semaphore that grants all possible access permissions (owner,
group, and all other users have read and write permissions).

5.3.2 Semaphores


System V semaphores are implemented usingsem/sem.cin conjunction with the header file<sem.h>.
These semaphores are not related in any way to the kernel semaphores described above.

Using SystemV Semaphores


The System V interface for using semaphores is anything but intuitive because the concept of a
semaphore has been expanded well beyond its actual definition. Semaphores are no longer treated as
simple variables to support atomic execution of predefined operations. Instead, a System V semaphore
now refers to a whole set of semaphores, which allows not just one but several operations to be
performed at the same time (although they appear to be atomic to users). It is, of course, possible to
request a semaphore set with just a single semaphoreand to define functions that simulate the behavior
of simple operations. The following sample program shows how semaphores are used:

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>

#define SEMKEY 1234L /* Identifier */
#define PERMS 0666 /* Access permission: rwrwrw */

struct sembuf op_down[1] = { 0, -1 , 0 };
struct sembuf op_up[1] = { 0, 1 , 0 };

int semid = -1; /* Semaphore identifier */
int res; /* Result of semaphore operations */
Free download pdf