System V Shared Memory 1011
48.7 Shared Memory Control Operations
The shmctl() system call performs a range of control operations on the shared mem-
ory segment identified by shmid.
The cmd argument specifies the control operation to be performed. The buf argu-
ment is required by the IPC_STAT and IPC_SET operations (described below), and
should be specified as NULL for the remaining operations.
In the remainder of this section, we describe the various operations that can be
specified for cmd.
Generic control operations
The following operations are the same as for other types of System V IPC objects.
Further details about these operations, including the privileges and permissions
required by the calling process, are described in Section 45.3.
IPC_RMID
Mark the shared memory segment and its associated shmid_ds data structure
for deletion. If no processes currently have the segment attached, deletion
is immediate; otherwise, the segment is removed after all processes have
detached from it (i.e., when the value of the shm_nattch field in the shmid_ds
data structure falls to 0). In some applications, we can make sure that a
shared memory segment is tidily cleared away on application termination by
marking it for deletion immediately after all processes have attached it to
their virtual address space with shmat(). This is analogous to unlinking a
file once we’ve opened it.
On Linux, if a shared segment has been marked for deletion using IPC_RMID,
but has not yet been removed because some process still has it attached, then it
is possible for another process to attach that segment. However, this behavior is
not portable: most UNIX implementations prevent new attaches to a segment
marked for deletion. (SUSv3 is silent on what behavior should occur in this sce-
nario.) A few Linux applications have come to depend on this behavior, which is
why Linux has not been changed to match other UNIX implementations.
IPC_STAT
Place a copy of the shmid_ds data structure associated with this shared
memory segment in the buffer pointed to by buf. (We describe this data
structure in Section 48.8.)
IPC_SET
Update selected fields of the shmid_ds data structure associated with this
shared memory segment using values in the buffer pointed to by buf.
#include <sys/types.h> /* For portability */
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf);
Returns 0 on success, or –1 on error