IPC Interfaces, Key Arguments, and Creation Flags
Processes requesting access to an IPC facility must be
able to identify the facility. To identify the facility to which the
process requests access, interfaces that initialize or provide
access to an IPC facility use a key_t
key argument. The
key is an arbitrary value or one
that can be derived from a common seed at runtime. One way to derive
such a key is by using ftok
(), which converts a
file name to a key value that is unique within the system. For more
information, see the
ftok
(3C) man page.
Interfaces that initialize or get access to messages, semaphores, or
shared memory return an ID number of type int
. IPC Interfaces
that perform read, write, and control operations use this ID.
If the key argument is specified as IPC_PRIVATE
,
the call initializes a new instance of an IPC facility that is private to
the creating process.
When the IPC_CREAT
flag is supplied in the flags
argument appropriate to the call, the interface tries to create the facility
if it does not exist already.
When called with both theIPC_CREAT
and IPC_EXCL
flags, the interface fails if the facility already exists. This
behavior can be useful when more than one process might attempt to initialize
the facility. One such case might involve several server processes having
access to the same facility. If they all attempt to create the facility with IPC_EXCL
in effect, only the first attempt succeeds.
If neither of these flags is given and the facility already exists,
the interfaces return the ID of the facility to get access. If IPC_CREAT
is omitted and the facility is not already initialized, the calls
fail.
Using logical (bitwise) OR
, IPC_CREAT
and IPC_EXCL
are combined with the octal permission modes to form the
flags argument. For example, the following statement initializes a new message
queue if the queue does not exist:
msqid = msgget(ftok("/tmp", 'A'), (IPC_CREAT | IPC_EXCL | 0400));
The first argument evaluates to a key ('A'
) based
on the string ("/tmp"
). The second argument evaluates to
the combined permissions and control flags.