'\"macro stdmacro
.if n .pH g2.shmop @(#)shmop	40.11 of 10/10/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} shmop 2 "" "\&"
.if \nX=1 .ds x} shmop 2 ""
.if \nX=2 .ds x} shmop 2 "" "\&"
.if \nX=3 .ds x} shmop "" "" "\&"
.TH \*(x}
.SH NAME
\f4shmop\f1: \f4shmat\fP, \f4shmdt\fP \- shared memory operations
.SH SYNOPSIS
\f4#include <sys/types.h>\f1
.br
\f4#include <sys/ipc.h>\f1
.br
\f4#include <sys/shm.h>\f1
.PP
\f4void \(**shmat(int shmid, void \(**shmaddr, int shmflg);\f1
.PP
\f4int shmdt (void \(**shmaddr);\f1
.SH DESCRIPTION
\f4shmat\fP
attaches the shared memory segment associated with the shared memory identifier
specified by
.I shmid
to the data segment of the calling process.
The segment is attached at the address specified by one of the following
criteria:
.IP
If
.I shmaddr
is equal to \f4(void *) 0\f1, the segment is attached at the first available
address as selected by the system.
.IP
If
.I shmaddr
is not equal to \f4(void *) 0\f1 and
(\f2shmflg\f4&SHM_RND\f1)
is true, the segment is attached at the address given by
(\f2shmaddr \f4-\f1 
(\f2shmaddr\f1
modulus
\f4SHMLBA\f1)).
.IP
If
.I shmaddr
is not equal to \f4(void *) 0\f1 and
(\f2shmflg\f4&SHM_RND\f1)
is false, the segment is attached at the address given by
.IR shmaddr .
.PP
\f4shmdt\fP
detaches from the calling process's data segment
the shared memory segment located at the address specified by
.IR shmaddr .
.PP
The segment is attached for reading if
(\f2shmflg\f4&SHM_RDONLY\f1)
is true
.SM \%{READ}\*S,
otherwise it is attached for reading and writing
.SM \%{READ/WRITE}\*S.
.PP
\f4shmat\fP
fails and does not attach the shared memory segment if one or more of the
following are true:
.TP 15
\f4EINVAL\fP
.I shmid
is not a valid shared memory identifier.
.TP
\f4EACCES\fP
Operation permission is denied to the calling process [see
\f4intro\fP(2)].
.TP
\f4ENOMEM\fP
The available data space 
is not large enough to accommodate the shared memory segment.
.TP
\f4EINVAL\fP
.I shmaddr
is not equal to zero, and the value of
(\f2shmaddr \f4-\f1 
(\f2shmaddr\f1
modulus
\f4SHMLBA\f1)).
is an illegal address.
.TP
\f4EINVAL\fP
.I shmaddr
is not equal to zero,
(\f2shmflg\f4&SHM_RND\f1)
is false, and the value of
.I shmaddr
is an illegal address.
.TP
\f4EMFILE\fP
The number of shared memory segments attached to the calling process would
exceed the system-imposed limit.
.PP
.TP 15
\f4EINVAL\fP
\f4shmdt\fP
fails and does not detach the shared memory segment if
.I shmaddr
is not the data segment start address of a shared memory segment.
.SH SEE ALSO
\f4intro\fP(2),
\f4exec\fP(2), \f4exit\fP(2), \f4fork\fP(2),
\f4shmctl\fP(2), \f4shmget\fP(2).
.SH DIAGNOSTICS
Upon successful completion, the return value is as follows:
.ne 8v
.IP
\f4shmat\fP
returns the data segment start address of the attached shared memory segment.
.IP
\f4shmdt\fP
returns a value of 0.
.PP
Otherwise, a value of \-1 is returned and
\f4errno\fP
is set to indicate the error.
.SH NOTES
The user must explicitly remove shared memory segments
after the last reference to them has been removed.
.\"	@(#)shmop.2	6.2 of 9/6/83
.Ee
