1setsid(2) System Calls Manual setsid(2)
2
3
4
6 setsid - creates a session and sets the process group ID
7
9 Standard C library (libc, -lc)
10
12 #include <unistd.h>
13
14 pid_t setsid(void);
15
17 setsid() creates a new session if the calling process is not a process
18 group leader. The calling process is the leader of the new session
19 (i.e., its session ID is made the same as its process ID). The calling
20 process also becomes the process group leader of a new process group in
21 the session (i.e., its process group ID is made the same as its process
22 ID).
23
24 The calling process will be the only process in the new process group
25 and in the new session.
26
27 Initially, the new session has no controlling terminal. For details of
28 how a session acquires a controlling terminal, see credentials(7).
29
31 On success, the (new) session ID of the calling process is returned.
32 On error, (pid_t) -1 is returned, and errno is set to indicate the er‐
33 ror.
34
36 EPERM The process group ID of any process equals the PID of the call‐
37 ing process. Thus, in particular, setsid() fails if the calling
38 process is already a process group leader.
39
41 POSIX.1-2008.
42
44 POSIX.1-2001, SVr4.
45
47 A child created via fork(2) inherits its parent's session ID. The ses‐
48 sion ID is preserved across an execve(2).
49
50 A process group leader is a process whose process group ID equals its
51 PID. Disallowing a process group leader from calling setsid() prevents
52 the possibility that a process group leader places itself in a new ses‐
53 sion while other processes in the process group remain in the original
54 session; such a scenario would break the strict two-level hierarchy of
55 sessions and process groups. In order to be sure that setsid() will
56 succeed, call fork(2) and have the parent _exit(2), while the child
57 (which by definition can't be a process group leader) calls setsid().
58
59 If a session has a controlling terminal, and the CLOCAL flag for that
60 terminal is not set, and a terminal hangup occurs, then the session
61 leader is sent a SIGHUP signal.
62
63 If a process that is a session leader terminates, then a SIGHUP signal
64 is sent to each process in the foreground process group of the control‐
65 ling terminal.
66
68 setsid(1), getsid(2), setpgid(2), setpgrp(2), tcgetsid(3), creden‐
69 tials(7), sched(7)
70
71
72
73Linux man-pages 6.05 2023-03-30 setsid(2)