.TH shmat() "" "" "General Function (libc)"
.PC "Attach a shared-memory segment to a process"
.B "#include <sys/types.h>"
.B "#include <sys/ipc.h>"
.B "#include <sys/shm.h>"
\fBchar *shmat (\fIshmid\^\fB, \fIshmaddr\^\fB, \fIshmflg\^\fB)\fR
\fBint \fIshmid\^\fB, \fIshmflag\^\fB; char *\fIshmaddr\^\fB;\fR
.PP
.B shmat()
attaches the shared-memory segment associated with the identifier
.I shmid
with the \fB.data\fR segment of the calling process.
.PP
.B shmat()
selects the address at which to attach the shared-memory segment.
If
.DM
	\fIshmflg\fP & SHM_RDONLY
.DE
.PP
is true, the attached memory is read-only; otherwise, it is read-write.
.PP
.B shmat()
fails if any of the following is true:
.IP \(bu 0.3i
.I shmid
is not a valid shared-memory identifier.
.B shmat()
sets
.B errno
to
.BR EINVAL .
.IP \(bu
The calling process lacks appropriate permission (\fBEACCES\fR).
.IP \(bu
Not enough memory is available to hold the shared-memory segment
(\fBENOMEM\fR).
.IP \(bu
The process already has the maximum number of shared-memory segments
attached to it (\fBEMFILE\fR).
.PP
You can attach more than one shared-memory segment to a process, up to a
maximum of six.
\*(CO assigns each segment its own address.
.PP
If all went well,
.B shmat()
returns the address of the newly attached shared-memory segment;
otherwise, it returns \-1 and sets
.B errno
to an appropriate value.
.SH Example
For an example of this function, see the Lexicon entry for
.BR shmget() .
.SH "See Also"
.Xr "libc," libc
.Xr "shmctl()," shmctl
.Xr "shmdt()," shmdt
.Xr "shmget()" shmget
.SH Notes
The \*(CO implementation of shared memory does not yet support
attaching a shared-memory segment to a user-defined address.
Therefore, you should always set
.I shmaddr
to zero.
