Controlling a Shared Memory Segment
shmctl
()
is used to alter the permissions
and other characteristics of a shared memory segment. The
cmd
argument is one of following control
commands.
-
SHM_LOCK
-
Lock the specified shared memory segment in memory. The process must have the effective ID of superuser to perform this command.
-
SHM_UNLOCK
-
Unlock the shared memory segment. The process must have the effective ID of superuser to perform this command.
-
IPC_STAT
-
Return the status information contained in the control structure and place it in the buffer pointed to by
buf
. The process must have read permission on the segment to perform this command. -
IPC_SET
-
Set the effective user and group identification and access permissions. The process must have an effective ID of owner, creator or superuser to perform this command.
-
IPC_RMID
-
Remove the shared memory segment. The process must have an effective ID of owner, creator, or superuser to perform this command.
The following code illustrates
shmctl
().
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> ... int cmd; /* command code for shmctl() */ int shmid; /* segment ID */ struct shmid_ds shmid_ds; /* shared memory data structure to hold results */ ... shmid = ... cmd = ... if ((rtrn = shmctl(shmid, cmd, shmid_ds)) == -1) { perror("shmctl: shmctl failed"); exit(1); ...