.TH setsid() "" "" "System Call (libc)"
.PC "Set session identifier"
.B "#include <sys/types.h>"
.B "#include <unistd.h>"
.B "pid_t setsid (\|);"
.PP
If the calling process is not a process-group leader,
.B setsid()
sets its process-group and session identifiers to its process identifier,
and releases the its controlling terminal.
.PP
If all goes well,
.B setsid()
returns the calling process's session identifier.
If the calling process is already a process-group leader,
or if process-group identifier of another process equals
that of the calling process,
.B setsid()
returns \-1 and sets
.B errno
to
.BR EPERM .
.SH "See Also"
.Xr "libc," libc
.Xr "unistd.h" unistd.h
.br
\*(PX Standard, \(sc4.3.2
.SH Notes
If the calling process is the last member of a pipeline started by a
job-control shell,
the shell may make the calling process a process-group leader.
The other processes of the pipeline become members of that process group.
If this happens, the call to
.B setsid()
fails.
.PP
For this reason, a process that calls
.B setsid()
and expects to be part of a pipeline should first fork:
the parent should exit and the child should call
.BR setsid() .
This will ensure that the process works reliably
when started by both job-control shells and non-job-control shells.
