gtpc2m6j | C/C++ Language Support User's Guide |
This function provides a variety of operations that are used to control the
shared memory identified by the shmid parameter.
Format
#include <sys/shm.h>
int shmctl( int shmid,
int cmd,
struct shmid_ds *buf);
- shmid
- The shared memory identifier for which this operation is to take
place. The shared memory identifier is returned by the
shmget function.
- cmd
- The shared memory control operation to be performed, which is specified as
one of the following:
- IPC_STAT
- Use this operation to obtain status information for the shared memory
identified by the shmid parameter. The current value of each
field in the shmid_ds data structure associated with the
shmid parameter is placed in the data structure pointed to by the
buf parameter. The contents of this structure are defined in
the sys/shm.h header file. This operation requires
read permission for the shared memory identified by the shmid
parameter.
- IPC_SET
- Use this operation to set the values of the following fields of the
shmid_ds data structure associated with the shmid
parameter to the corresponding value in the structure pointed to by the
buf parameter:
shm_perm.uid
shm_perm.gid
shm_perm.mode (only the low-order 9 bits)
This operation can be performed only by a process that has an effective
user ID (UID) equal to one of the following:
- A process with the same privileges as the shared memory identified by the
shmid parameter
- The value of shm_perm.cuid or
shm_perm.uid in the shmid_ds data structure
associated with the shmid parameter.
- IPC_RMID
- Use this operation to remove the following from the TPF system:
- The shared memory identifier specified by the shmid parameter
- The shared memory associated with the shmid parameter
- The shmid_ds data structure associated with the
shmid parameter.
This operation can be performed only by a process that has an effective UID
equal to one of the following:
- A process with the same privileges as the shared memory identified by the
shmid parameter
- The value of shm_perm.cuid or
shm_perm.uid in the shmid_ds data structure
associated with the shmid parameter.
Removal is completed asynchronously to the return from the
shmctl function when the last attached shared memory segment is
detached. When IPC_RMID is processed, no more attaches for the
shared memory identified by the shmid parameter are allowed.
- buf
- A pointer to a shmid_ds data structure as defined in the
sys/shm.h header file.
Normal Return
If successful, the shmctl function returns a value of 0.
Error Return
If unsuccessful, the shmctl function returns a value of -1
and sets errno to one of the following:
- EACCES
- The cmd parameter is specified as IPC_STAT, but the calling
process does not have read permission for the shared memory.
- EINVAL
- The value of the shmid parameter is not a valid shared memory
identifier or the value of the cmd parameter is not a valid
operation.
- EPERM
- The cmd parameter is specified as either IPC_RMID or
IPC_SET and one of the following is true:
- The effective UID of the calling process is not equal to that of a process
with the same access privileges as for the shared memory.
- The effective UID of the calling process is not equal to the value of
shm_perm.cuid or shm_perm.uid in the
data structure associated with the shmid parameter.
Programming Considerations
- Programs require restricted authorization to use this function.
- When IPC_STAT is specified for the cmd parameter, the
buf parameter is a pointer to a shmid_ds data
structure where the current values of each member of the
shmid_ds data structure associated with the shmid
parameter are returned to the caller.
- When IPC_SET is specified for the cmd parameter, the
buf parameter is a pointer to a shmid_ds data
structure where the caller specifies values for
shm_perm.uid, shm_perm.gid, and
shm_perm.mode.
Examples
The following example attaches shared memory to the address space of the
calling process by using the shared memory identifier returned by the
shmget function. The shared memory identifier is removed
from the TPF system by using the shmctl function when it is no
longer needed.
#include <sys/ipc.h>
#include <sys/shm.h>
void main(void)
{
key_t i;
int shm;
void *addr;
struct shmid_ds buf;
i = ftok("/usr",3);
shm = shmget(i,8000,IPC_CREAT+S_IRUSR+S_IWUSR);
addr = shmat(shm,NULL,0);
i = shmctl(shm,IPC_RMID,&buf);
}
Related Information