1SETSID(2)                  Linux Programmer's Manual                 SETSID(2)
2
3
4

NAME

6       setsid - creates a session and sets the process group ID
7

SYNOPSIS

9       #include <unistd.h>
10
11       pid_t setsid(void);
12

DESCRIPTION

14       setsid()  creates a new session if the calling process is not a process
15       group leader.  The calling process is the leader  of  the  new  session
16       (i.e., its session ID is made the same as its process ID).  The calling
17       process also becomes the process group leader of a new process group in
18       the session (i.e., its process group ID is made the same as its process
19       ID).
20
21       The calling process will be the only process in the new  process  group
22       and in the new session.
23
24       Initially, the new session has no controlling terminal.  For details of
25       how a session acquires a controlling terminal, see credentials(7).
26

RETURN VALUE

28       On success, the (new) session ID of the calling  process  is  returned.
29       On  error, (pid_t) -1 is returned, and errno is set to indicate the er‐
30       ror.
31

ERRORS

33       EPERM  The process group ID of any process equals the PID of the  call‐
34              ing process.  Thus, in particular, setsid() fails if the calling
35              process is already a process group leader.
36

CONFORMING TO

38       POSIX.1-2001, POSIX.1-2008, SVr4.
39

NOTES

41       A child created via fork(2) inherits its parent's session ID.  The ses‐
42       sion ID is preserved across an execve(2).
43
44       A  process  group leader is a process whose process group ID equals its
45       PID.  Disallowing a process group leader from calling setsid() prevents
46       the possibility that a process group leader places itself in a new ses‐
47       sion while other processes in the process group remain in the  original
48       session;  such a scenario would break the strict two-level hierarchy of
49       sessions and process groups.  In order to be sure  that  setsid()  will
50       succeed,  call  fork(2)  and  have the parent _exit(2), while the child
51       (which by definition can't be a process group leader) calls setsid().
52
53       If a session has a controlling terminal, and the CLOCAL flag  for  that
54       terminal  is  not  set,  and a terminal hangup occurs, then the session
55       leader is sent a SIGHUP signal.
56
57       If a process that is a session leader terminates, then a SIGHUP  signal
58       is sent to each process in the foreground process group of the control‐
59       ling terminal.
60

SEE ALSO

62       setsid(1),  getsid(2),  setpgid(2),  setpgrp(2),  tcgetsid(3),  creden‐
63       tials(7), sched(7)
64

COLOPHON

66       This  page  is  part of release 5.12 of the Linux man-pages project.  A
67       description of the project, information about reporting bugs,  and  the
68       latest     version     of     this    page,    can    be    found    at
69       https://www.kernel.org/doc/man-pages/.
70
71
72
73Linux                             2021-03-22                         SETSID(2)
Impressum