SHM_UNLINK(3POSIX)

okay bestie, sit down, hydrate, because today we're learning how to delete a shared memory object's name and honestly? it's giving unhinged-but-make-it-portable. this is page SHM_UNLINK(3POSIX) from the POSIX Programmer's Manual, and we're gonna read it together like it's a text from your situationship. πŸ’–


🚨 PROLOG 🚨

bestie. listen. this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β€” so go consult the corresponding Linux manual page for the deets on Linux behavior. or, plot twist, the interface might not even be implemented on Linux at all. πŸ™ˆ

✨ NAME ✨

shm_unlink β€” remove a shared memory object (REALTIME). yes she's realtime, she's got places to be. πŸ”₯


🌸 SYNOPSIS 🌸

#include <sys/mman.h>

int shm_unlink(const char *name);

literally just one include and one function call. low maintenance queen. πŸ‘‘


πŸ’… DESCRIPTION πŸ’…

so the shm_unlink() function shall remove the name of the shared memory object named by the string pointed to by name. that's it, that's the whole vibe. it's a name-removal service. πŸ’€

but here's the part where it gets juicy: if one or more references to the shared memory object exist when the object is unlinked, then the name shall be removed before shm_unlink() returns β€” but the removal of the memory object contents shall be postponed until all open and map references to the shared memory object have been removed. it's giving "you can leave the group chat but your messages stay until everyone's read them." 😭

and even if the object continues to exist after the last shm_unlink(), reuse of the name shall subsequently cause shm_open() to behave as if no shared memory object of this name exists. that means: shm_open() will fail if O_CREAT is not set, or it will create a brand new shared memory object if O_CREAT is set. fresh start, new era, no memory of the old you. πŸ¦‹


πŸŽ€ RETURN VALUE πŸŽ€

upon successful completion, a value of zero shall be returned. slay.

otherwise, a value of -1 shall be returned and errno set to indicate the error. and like, important: if -1 is returned, the named shared memory object shall not be changed by this function call. no half-committed chaos here, she's clean. ✨


πŸ”₯ ERRORS πŸ”₯

the shm_unlink() function shall fail if:

  • EACCES β€” permission is denied to unlink the named shared memory object. not your name, not your business, bestie. 🚫
  • ENOENT β€” the named shared memory object does not exist. she's not here, she never was, stop looking. πŸ’€

the shm_unlink() function may fail if:

  • ENAMETOOLONG β€” the length of the name argument exceeds {_POSIX_PATH_MAX} on systems that do not support the XSI option, or exceeds {_XOPEN_PATH_MAX} on XSI systems, or has a pathname component that is longer than {_POSIX_NAME_MAX} on systems that do not support the XSI option or longer than {_XOPEN_NAME_MAX} on XSI systems.
okay but real talk, there's a lil protection here: a call to shm_unlink() with a name argument that contains the same shared memory object name as was previously used in a successful shm_open() call shall not give an [ENAMETOOLONG] error. so if it worked before, it works now. iconic consistency. 🫢

the following sections are informative. (translation: the plot-relevant stuff is done, this is the bonus content. πŸ“)


🌈 EXAMPLES 🌈

None.

(no examples. the void. the silence. she simply doesn't do demos. 🧚)


πŸ’œ APPLICATION USAGE πŸ’œ

names of memory objects that were allocated with open() are deleted with unlink() in the usual fashion. names of memory objects that were allocated with shm_open() are deleted with shm_unlink(). different tool, different job, know the difference iykyk. πŸ’…

and note β€” note this!! β€” the actual memory object is not destroyed until the last close and unmap on it have occurred, if it was already in use. she's not gone, she's just not taking new calls. 😭


⭐ RATIONALE ⭐

None.

(no rationale. we simply do it because the standard said so. periodt. πŸ‘‘)


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

a future version might require the shm_open() and shm_unlink() functions to have semantics similar to normal file system operations. so like... watch this space? potential glow-up incoming. 🌸


πŸ’– SEE ALSO πŸ’–

  • close()
  • mmap()
  • munmap()
  • shmat()
  • shmctl()
  • shmdt()
  • shm_open()

and the Base Definitions volume of POSIX.1‐2017, <sys_mman.h>. go read the friends, they all hang out together. 🫢


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 .


ok that's the whole page bestie, you ate that, no notes. πŸ’…βœ¨

and that's the sign-off: this absolute masterpiece is brought to you by IEEE/The Open Group, dated 2017, filed under SHM_UNLINK(3POSIX). go unlink something responsibly, love u, bye. πŸ’–πŸŽ€πŸ¦‹