Although each IPC service provides a specific type of interprocess communication, the three identifier based services share many similarities. Each service defines a mechanism through which its communications take place. For message queues, that mechanism is a message queue; for semaphore sets, it is a semaphore set; and for shared memory, it is a shared memory segment. These mechanisms are identified by a unique positive integer called, respectively, a message queue identifier (msqid), a semaphore set identifier (semid), and a shared memory identifier (shmid).
Note: Throughout the Interprocess Communication APIs, the term thread is used extensively. This does not mean that IPC objects can be used only between threads within one process, but rather that authorization checks and waits occur for the calling thread within a process.
Associated with each identifier is a data structure that contains state information for the IPC mechanism, as well as ownership and permissions information. The ownership and permissions information is defined in a structure in the <sys/ipc.h> header file as follows:
typedef struct ipc_perm { uid_t uid; /* Owner's user ID */ gid_t gid; /* Owner's group ID */ uid_t cuid; /* Creator's user ID */ gid_t cgid; /* Creator's group ID */ mode_t mode; /* Access modes */ } ipc_perm_t;
This structure is similar to a file permissions structure, and is initialized by the thread that creates the IPC mechanism. It is then checked by all subsequent IPC operations to determine if the requesting thread has the required permissions to perform the operation.
To get an identifier, a thread must either create a new IPC mechanism or access an existing mechanism. This is done through the msgget(), semget(), and shmget() functions. Each get operation takes as input a key parameter and returns an identifier. Each get operation also takes a flag parameter. This flag parameter contains the IPC permissions for the mechanism as well as bits that determine whether or not a new mechanism is created. The rules for whether a new mechanism is created or an existing one is referred to are as follows:
When a message queue, semaphore set, or shared memory segment is created, the thread that creates it determines how it can be accessed. The thread does this by passing mode information in the low-order 9 bits of the flag parameter on the msgget(), semget(), or shmget() function call. This information is used to initialize the mode field in the ipc_perm structure. The values of the bits are given below in hexadecimal notation:
Bit | Meaning |
---|---|
X'0100' | Read by user |
X'0080' | Write by user |
X'0020' | Read by group |
X'0010' | Write by group |
X'0004' | Read by others |
X'0002' | Write by others |
Subsequent IPC operations do a permission test on the calling thread before allowing the thread to perform the requested operation. This permission test is done in one of three forms:
The Identifier Based Services are divided into the following subcategories:
Top | UNIX-Type APIs | APIs by category |