shmat(), shmdt() - shared memory operations
void *shmat(int shmid, void *shmaddr, int shmflg);
int shmdt(void *shmaddr);
shmat() attaches the shared memory segment associated with the shared
memory identifier specified by shmid to the data segment of the
The segment is attached for reading if (shmflg &&&& SHM_RDONLY) is
"true"; otherwise, it is attached for reading and writing. It is not
possible to attach a segment for write only.
If the shared memory segment has never been attached to by any process
prior to the current shmat() call, shmaddr must be specified as zero
and the segment is attached at a location selected by the operating
system. That location is identical in all processes accessing that
shared memory object. Once the operating system selects a location
for a shared memory segment, the same location will be used across any
subsequent shmat() and shmdt() calls on the segment until it is
removed by the IPC_RMID operation of shmctl().
If this is not the first shmat() call on the shared memory segment
throughout the system, shmaddr must either be zero or contain a
nonzero address that is identical to the one returned from previous
shmat() calls for that segment. Even if no processes are currently
attached to the segment, as long as the segment has been attached
before, the same rule applies.
If the calling process is already attached to the shared memory
segment, shmat() fails and returns SHM_FAILED regardless of what value
is passed in shmaddr.
shmdt() detaches from the calling process's data segment the shared
memory segment located at the address specified by shmaddr.
shmat() returns the following values:
n Successful completion. n is the data segment start address
of the attached shared memory segment.
Failure. The shared memory segment is not attached. errno
is set to indicate the error. The symbol SHM_FAILED is
defined in the header <sys/shm.h>. No successful return
Hewlett-Packard Company - 1 - HP-UX Release 11i: November 2000
from shmat() will return the value SHM_FAILED.
shmdt() returns the following values:
0 Successful completion.
-1 Failure. errno is set to indicate the error.
If shmat() fails, errno is set to one of the following values.
[EACCES] Operation permission is denied to the calling
[EINVAL] shmid is not a valid shared memory identifier,
(possibly because the shared memory segment was
already removed using shmctl(2) with IPC_RMID), or
the calling process is already attached to shmid.
[EINVAL] shmaddr is not zero and the machine does not
permit nonzero values, or shmaddr is not equal to
the current attach location for the shared memory
[ENOMEM] The available data space is not large enough to
accommodate the shared memory segment.
[EMFILE] The number of shared memory segments attached to
the calling process exceed the system-imposed
If shmdt() fails, errno is set to one of the following values.
[EINVAL] shmaddr is not the data segment start address of a
shared memory segment.
The following call to shmat() attaches the shared memory segment to
the process. This example assumes the process has a valid shmid,
which can be obtained by calling shmget(2).
shmptr = (char *) shmat(myshmid, 0, 0);
The following call to shmdt() then detaches the shared memory segment.
ipcs(1), exec(2), exit(2), fork(2), ftok(3C), shmctl(2), shmget(2).
Hewlett-Packard Company - 2 - HP-UX Release 11i: November 2000
shmat(): SVID2, SVID3, XPG2, XPG3, XPG4
shmdt(): SVID2, SVID3, XPG2, XPG3, XPG4
Hewlett-Packard Company - 3 - HP-UX Release 11i: November 2000