setsid(2)

๐Ÿ’– setsid(2) ๐Ÿ’–

okay bestie, welcome to the System Calls Manual section, we're talking about setsid today and honestly? it's lowkey iconic. sit down, grab a strawberry, let's get into it. ๐Ÿ“


โœจ NAME โœจ

setsid โ€” creates a session and sets the process group ID

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, link with -lc)

๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <unistd.h>

pid_t setsid(void);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

okay so here's the tea: setsid() creates a new session โ€” but only if the calling process is not a process group leader. that's the vibe check. ๐Ÿซถ

and then boom: the calling process becomes the leader of the new session. like, its session ID gets set to the same value as its process ID. no cap. ๐ŸŽ€

AND that's not even all of it โ€” the calling process ALSO becomes the leader of a brand new process group inside that new session. meaning its process group ID is also made the same as its process ID. it's giving main character energy fr fr. ๐Ÿ‘‘

so after all that: the calling process is the only process in the new process group, and the only process in the new session. literally just them. alone. thriving. โœจ

initially, the new session has no controlling terminal. if you wanna know the details of how a session actually acquires a controlling terminal, go read credentials(7) โ€” that's where the lore lives. ๐Ÿ“–

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

on success, setsid() returns the (new) session ID of the calling process. slay. ๐Ÿ’–

on error, it returns (pid_t) -1, and errno gets set to tell you what went wrong. ๐Ÿ’€

๐Ÿ˜ญ ERRORS ๐Ÿ˜ญ

  • EPERM โ€” this one means the process group ID of any process equals the PID of the calling process. so like, in particular? setsid() fails if the calling process is already a process group leader. no getting around it, bestie, it's just gonna fail. ๐Ÿ™ˆ

โญ STANDARDS โญ

POSIX.1-2024. periodt.

๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

POSIX.1-2001, SVr4. a classic, an OG, we respect the elders. ๐Ÿ’œ

๐Ÿ‘‘ NOTES ๐Ÿ‘‘

a child created via fork(2) inherits its parent's session ID. and that session ID is preserved across an execve(2). good to know, right? ๐ŸŒˆ


๐Ÿงš what even IS a process group leader ๐Ÿงš

okay so a process group leader is a process whose process group ID equals its PID. simple as that.

now, why do we disallow a process group leader from calling setsid()? because if a process group leader could yeet itself into a new session while all the other processes in its process group stayed behind in the original session, that would break the strict two-level hierarchy of sessions and process groups. and we do not break the hierarchy. that's the whole point. ๐Ÿ’…

so if you need to be sure that setsid() will succeed? here's the move: call fork(2), have the parent call _exit(3), and let the child โ€” which by definition can't be a process group leader โ€” call setsid(). iconic workaround. ๐ŸŽ€


๐Ÿšจ terminal drama ๐Ÿšจ

if a session has a controlling terminal, and the CLOCAL flag for that terminal is not set, and a terminal hangup occurs, then the session leader gets sent a SIGHUP signal. ๐Ÿ˜ญ
and if a process that is a session leader terminates, then a SIGHUP signal is sent to each process in the foreground process group of the controlling terminal. that's a lot of sighups bestie, everyone's getting one. ๐Ÿ’€

๐ŸŒŸ SEE ALSO ๐ŸŒŸ


and that's the whole page, bestie!! ๐Ÿ’–โœจ๐ŸŽ€๐Ÿฆ‹๐ŸŒธ

this was brought to you by Linux man-pages 6.20, dated 2026-06-05. drink some water, call your session leader, bye!! ๐Ÿซถ๐Ÿ‘‘๐Ÿ’…

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms