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 theCLOCALflag for that terminal is not set, and a terminal hangup occurs, then the session leader gets sent aSIGHUPsignal. ๐ญ
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
- Copyright 2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms