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 addr is 0, the segment is attached at an address selected by the kernel πŸ‘‘ (letting the kernel pick is such a vibe)
  • if addr is nonzero and SHM_RND is not specified in flag, the segment is attached the specified address πŸ“
  • if addr is specified and SHM_RND is specified, addr is rounded down to the nearest multiple of SHMLBA πŸŒ€

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 to shmid πŸ™ˆ
  • [EINVAL] β€” the addr argument was not an acceptable address 🚫
  • [ENOMEM] β€” the specified addr cannot 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 no SHM_REMAP flag was provided 😡
  • [EMFILE] β€” failed to attach the shared memory segment because the per-process kern.ipc.shmseg sysctl(3) limit was reached πŸ’€

the shmdt() system call will fail if:

  • [EINVAL] β€” the addr argument 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