SHMAT(3POSIX)
π SHMAT π β the POSIX Programmer's Manual, but make it a group chat
okay bestie, welcome to SHMAT(3POSIX), straight out of the POSIX Programmer's Manual, no cap. this is the page that teaches you how to attach a shared memory segment to your process's address space, and honestly? it's giving main character energy for interprocess communication. β¨
πΈ PROLOG πΈ
real talk before we start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so like, consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux at all. iykyk. π
β¨ NAME β¨
shmat β the XSI shared memory attach operation. that's it, that's the whole name. iconic.
π SYNOPSIS π
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
look at her. three arguments. one job. slay. π
π¦ DESCRIPTION π¦
so the shmat() function operates on XSI shared memory (peep the Base Definitions volume of POSIX.1β2017, Section 3.346, Shared Memory Object if you wanna get academic about it). and um, important disclaimer: it is unspecified whether this function interoperates with the realtime interprocess communication facilities defined in Section 2.8, Realtime. so don't come for her if that's a vibe mismatch. π«Ά
what shmat() actually does: it attaches the shared memory segment associated with the shared memory identifier specified by shmid to the address space of the calling process. that's the whole glow-up. β¨
now, the segment gets attached at the address specified by one of the following criteria β choose your fighter:
- if
shmaddris a null pointer, the segment is attached at the first available address as selected by the system. the system is picking for you, bestie. trust the process. π - if
shmaddris not a null pointer and(shmflg &SHM_RND)is non-zero, the segment is attached at the address given by(shmaddr -((uintptr_t)shmaddr %SHMLBA)). and for the record, the character'%'is the C-language remainder operator. (yes that's real, yes it matters, no you can't just vibe past it.) π₯ - if
shmaddris not a null pointer and(shmflg &SHM_RND)is0, the segment is attached at the address given byshmaddr. no math, no rounding, just raw address. periodt. - the segment is attached for reading if
(shmflg &SHM_RDONLY)is non-zero and the calling process has read permission. otherwise, if it is0and the calling process has read and write permission, the segment is attached for reading and writing. so basically: permissions are the vibe check and you must pass it. π
β RETURN VALUE β
upon successful completion, shmat() shall increment the value of shm_nattch in the data structure associated with the shared memory ID of the attached shared memory segment, and return the segment's start address. also, the shm_atime timestamp shall be set to the current time, as described in Section 2.7.1, IPC General Description. she's updating her stats, as she should. π
otherwise (and this is the sad part π), the shared memory segment shall not be attached, shmat() shall return (void *)-1, and errno shall be set to indicate the error. (void *)-1 is the "we are NOT okay" signal. remember it.
π ERRORS π
the shmat() function shall fail if:
EACCESβ operation permission is denied to the calling process; see Section 2.7, XSI Interprocess Communication. you got gatekept, bestie.EINVALβ the value ofshmidis not a valid shared memory identifier; theshmaddris not a null pointer, and the value of(shmaddr -((uintptr_t)shmaddr %SHMLBA))is an illegal address for attaching shared memory; or theshmaddris not a null pointer,(shmflg &SHM_RND)is0, and the value ofshmaddris an illegal address for attaching shared memory. translation: something about your address math is Not It. π«EMFILEβ the number of shared memory segments attached to the calling process would exceed the system-imposed limit. you're collecting too many segments, girl, put some back. πENOMEMβ the available data space is not large enough to accommodate the shared memory segment. not enough room, the closet is full. π
and then the following sections are informative. (read: optional lore, not law.)
π EXAMPLES π
None. β yeah. nothing. no examples. we're all just out here figuring it out. π
π§ APPLICATION USAGE π§
the POSIX Realtime Extension defines alternative interfaces for interprocess communication. so if you're an application developer who needs to use IPC, the move is to design your applications so that modules using the IPC routines described in Section 2.7, XSI Interprocess Communication can be easily modified to use the alternative interfaces. future-proofing your code? that's the energy. β¨
π RATIONALE π
None. β no rationale provided. she simply is. π
π FUTURE DIRECTIONS π
None. β she's not going anywhere, and honestly, we're obsessed. β
πΈ SEE ALSO πΈ
- Section 2.7, XSI Interprocess Communication
- Section 2.8, Realtime
execexit()fork()shmctl()shmdt()shmget()shm_open()shm_unlink()
and from the Base Definitions volume of POSIX.1β2017:
- Section 3.346, Shared Memory Object
<sys_shm.h>
go read those too, bestie, we love a well-rounded education. π
π COPYRIGHT π
okay legal moment, pls pay attention because this part is not a vibe, it's a fact:
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie! π this one came to you from IEEE/The Open Group, dated 2017, under the name SHMAT(3POSIX). now go attach that shared memory and be the main character of your own address space. mwah. π
β¨π¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms