Team-FLY |
The POSIX interprocess communication (IPC) is part of the POSIX:XSI Extension and has its origin in UNIX System V interprocess communication. IPC, which includes message queues, semaphore sets and shared memory, provides mechanisms for sharing information among processes on the same system. These three communication mechanisms have a similar structure, and this chapter emphasizes the common elements of their use. Table 15.1 summarizes the POSIX:XSI interprocess communication functions. Table 15.1. POSIX:XSI interprocess communication functions.
15.1.1 Identifying and accessing IPC objectsPOSIX:XSI identifies each IPC object by a unique integer that is greater than or equal to zero and is returned from the get function for the object in much the same way as the open function returns an integer representing a file descriptor. For example, msgget returns an integer identifier for message queue objects. Similarly, semget returns an integer identifier for a specified semaphore set, and shmget returns an integer identifier for a shared memory segment. These identifiers are associated with additional data structures that are defined in sys/msg.h, sys/sem.h or sys/shm.h , respectively. The integer identifiers within each IPC object type are unique, but you might well have an integer identifier 1 for two different types of objects, say, a semaphore set and a message queue. When creating or accessing an IPC object, you must specify a key to designate the particular object to be created or accessed. Pick a key in one of these three ways.
The ftok function allows independent processes to derive the same key based on a known pathname. The file corresponding to the pathname must exist and be accessible to the processes that want to access an IPC object. The combination of path and id uniquely identifies the IPC object. The id parameter allows several IPC objects of the same type to be keyed from a single pathname. SYNOPSIS #include <sys/ipc.h> key_t ftok(const char *path, int id); POSIX:XSI If successful, ftok returns a key. If unsuccessful , ftok returns (key_t)-1 and sets errno . The following table lists the mandatory errors for ftok .
Example 15.1The following code segment derives a key from the filename /tmp/trouble.c . if ((thekey = ftok("tmp/trouble.c", 1)) == (key_t)-1)) perror("Failed to derive key from /tmp/trouble.c"); 15.1.2 Accessing POSIX:XSI IPC resources from the shellThe POSIX:XSI Extension for shells and utilities defines shell commands for examining and deleting IPC resources, a convenient feature that is missing for the POSIX:SEM semaphores. The ipcs command displays information about POSIX:XSI interprocess communication resources. If you forget which ones you created, you can list them from the shell command line. SYNOPSIS ipcs [-qms][-a -bcopt] POSIX:XSI,Shell and Utilities If no options are given, ipcs outputs, in an abbreviated format, information about message queues, shared memory segments and semaphore sets. You can restrict the display to specific types of IPC resources with the -q, -m and -s options for message queues, shared memory and semaphores, respectively. The -a option displays a long format giving all information available. The -bcopt options specify which components of the available information to print. Example 15.2The following command displays all the available information about the semaphores currently allocated on the system. ipcs -s -a You can remove an individual resource by giving either an ID or a key. Use the ipcrm command to remove POSIX:XSI interprocess communication resources. SYNOPSIS ipcrm [-q msgid -Q msgkey -s semid -S semkey -m shmid -M shmkey] .... POSIX:XSI,Shell and Utilities The lower case -q , -s and -m options use the object ID to specify the removal of a message queue, semaphore set or shared memory segment, respectively. The uppercase options use the original creation key. |
Team-FLY |