SHMAT(2)
β¨ SHMAT(2) β¨
okay bestie, welcome to the System Calls Manual π we're doing shared memory today and honestly? it's giving main character energy. this is the page for shmat(2) and its ride-or-die shmdt(2). π«Ά
πΈ NAME πΈ
shmat, shmdt β attach or detach shared memory π§
π LIBRARY π
Standard C Library (libc, -lc) β¨
π SYNOPSIS π
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
void *
shmat(int shmid, const void *addr, int flag);
int
shmdt(const void *addr);
π DESCRIPTION π
so. the shmat() system call attaches the shared memory segment identified by shmid to the calling process's address space. literally gluing a chunk of memory onto your process, no cap. π«Ά
where does it land? depends, bestie. the address where the segment is attached is determined as follows:
- if
addris0, the segment is attached at an address selected by the kernel π (letting the kernel pick is such a vibe) - if
addris nonzero andSHM_RNDis not specified inflag, the segment is attached the specified address π - if
addris specified andSHM_RNDis specified,addris rounded down to the nearest multiple ofSHMLBAπ
if the SHM_REMAP flag is specified and the passed addr is not NULL, any existing mappings in the virtual addresses range are cleared before the segment is attached. if the flag is not specified, addr is not NULL, and the virtual address range contains some pre-existing mappings, the shmat() call fails. π (sorry. it's just not that girl. the address is taken.)
the shmdt() system call detaches the shared memory segment at the address specified by addr from the calling process's address space. bye babe, it was real π
β RETURN VALUES β
upon success, shmat() returns the address where the segment is attached π otherwise, -1 is returned and errno is set to indicate the error. π
the shmdt() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error.
π₯ ERRORS π₯
the shmat() system call will fail if:
[EINVAL]β no shared memory segment was found corresponding toshmidπ[EINVAL]β theaddrargument was not an acceptable address π«[ENOMEM]β the specifiedaddrcannot be used for mapping, for instance due to the amount of available space being smaller than the segment size, or because pre-existing mappings are in the range and noSHM_REMAPflag was provided π΅[EMFILE]β failed to attach the shared memory segment because the per-processkern.ipc.shmsegsysctl(3)limit was reached π
the shmdt() system call will fail if:
[EINVAL]β theaddrargument does not point to a shared memory segment π»
π¦ SEE ALSO π¦
shmctl(2), shmget(2) β go read those too, bestie, they're part of the lore iykyk β¨
reminder: addresses are not a suggestion. read the flags. π
this has been SHMAT(2) from the Debian System Calls Manual, dated January 14, 2019 π mwah, drink water, detach your segments, and stay iconic πππ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1995 David Hovemeyer <daveho@infocom.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms