man-pages/man2/setpgid.2.html
2021-03-31 01:06:50 +01:00

479 lines
9.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SETPGID</TITLE>
</HEAD><BODY>
<H1>SETPGID</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
setpgid, getpgid, setpgrp, getpgrp - set/get process group
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<P>
<B>int setpgid(pid_t </B><I>pid</I><B>, pid_t </B><I>pgid</I><B>);</B>
<BR>
<B>pid_t getpgid(pid_t </B><I>pid</I><B>);</B>
<P>
<B>pid_t getpgrp(void);</B> /* POSIX.1 version */
<BR>
<B>pid_t getpgrp(pid_t </B><I>pid</I><B>);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</B>
/* BSD version */
<P>
<B>int setpgrp(void);</B> /* System V version */
<BR>
<B>int setpgrp(pid_t </B><I>pid</I><B>, pid_t </B><I>pgid</I><B>);&nbsp;</B>
/* BSD version */
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>getpgid</B>():
<DL COMPACT><DT id="1"><DD>
_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Since&nbsp;glibc&nbsp;2.12:&nbsp;*/&nbsp;_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
</DL>
<P>
<B>setpgrp</B>() (POSIX.1):
<PRE>
_XOPEN_SOURCE&nbsp;&gt;=&nbsp;500
|| /* Since glibc 2.19: */ _DEFAULT_SOURCE
|| /* Glibc versions &lt;= 2.19: */ _SVID_SOURCE
</PRE>
<P>
<B>setpgrp</B>()&nbsp;(BSD),
<B>getpgrp</B>()&nbsp;(BSD):
<PRE>
[These are available only before glibc 2.19]
_BSD_SOURCE &amp;&amp;
!&nbsp;(_POSIX_SOURCE || _POSIX_C_SOURCE || _XOPEN_SOURCE ||
_GNU_SOURCE || _SVID_SOURCE)
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
All of these interfaces are available on Linux,
and are used for getting and setting the
process group ID (PGID) of a process.
The preferred, POSIX.1-specified ways of doing this are:
<B>getpgrp</B>(void),
for retrieving the calling process's PGID; and
<B>setpgid</B>(),
for setting a process's PGID.
<P>
<B>setpgid</B>()
sets the PGID of the process specified by
<I>pid</I>
to
<I>pgid</I>.
If
<I>pid</I>
is zero, then the process ID of the calling process is used.
If
<I>pgid</I>
is zero, then the PGID of the process specified by
<I>pid</I>
is made the same as its process ID.
If
<B>setpgid</B>()
is used to move a process from one process
group to another (as is done by some shells when creating pipelines),
both process groups must be part of the same session (see
<B><A HREF="/cgi-bin/man/man2html?2+setsid">setsid</A></B>(2)
and
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7)).
In this case,
the <I>pgid</I> specifies an existing process group to be joined and the
session ID of that group must match the session ID of the joining process.
<P>
The POSIX.1 version of
<B>getpgrp</B>(),
which takes no arguments,
returns the PGID of the calling process.
<P>
<B>getpgid</B>()
returns the PGID of the process specified by
<I>pid</I>.
If
<I>pid</I>
is zero, the process ID of the calling process is used.
(Retrieving the PGID of a process other than the caller is rarely
necessary, and the POSIX.1
<B>getpgrp</B>()
is preferred for that task.)
<P>
The System&nbsp;V-style
<B>setpgrp</B>(),
which takes no arguments, is equivalent to
<I>setpgid(0,&nbsp;0)</I>.
<P>
The BSD-specific
<B>setpgrp</B>()
call, which takes arguments
<I>pid</I>
and
<I>pgid</I>,
is a wrapper function that calls
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;setpgid(pid,&nbsp;pgid)
<P>
Since glibc 2.19, the BSD-specific
<B>setpgrp</B>()
function is no longer exposed by
<I>&lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</I>;
calls should be replaced with the
<B>setpgid</B>()
call shown above.
<P>
The BSD-specific
<B>getpgrp</B>()
call, which takes a single
<I>pid</I>
argument, is a wrapper function that calls
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;getpgid(pid)
<P>
Since glibc 2.19, the BSD-specific
<B>getpgrp</B>()
function is no longer exposed by
<I>&lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</I>;
calls should be replaced with calls to the POSIX.1
<B>getpgrp</B>()
which takes no arguments (if the intent is to obtain the caller's PGID),
or with the
<B>getpgid</B>()
call shown above.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>setpgid</B>()
and
<B>setpgrp</B>()
return zero.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<P>
The POSIX.1
<B>getpgrp</B>()
always returns the PGID of the caller.
<P>
<B>getpgid</B>(),
and the BSD-specific
<B>getpgrp</B>()
return a process group on success.
On error, -1 is returned, and
<I>errno</I>
is set appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="2"><B>EACCES</B>
<DD>
An attempt was made to change the process group ID
of one of the children of the calling process and the child had
already performed an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
(<B>setpgid</B>(),
<B>setpgrp</B>()).
<DT id="3"><B>EINVAL</B>
<DD>
<I>pgid</I>
is less than 0
(<B>setpgid</B>(),
<B>setpgrp</B>()).
<DT id="4"><B>EPERM</B>
<DD>
An attempt was made to move a process into a process group in a
different session, or to change the process
group ID of one of the children of the calling process and the
child was in a different session, or to change the process group ID of
a session leader
(<B>setpgid</B>(),
<B>setpgrp</B>()).
<DT id="5"><B>ESRCH</B>
<DD>
For
<B>getpgid</B>():
<I>pid</I>
does not match any process.
For
<B>setpgid</B>():
<I>pid</I>
is not the calling process and not a child of the calling process.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>setpgid</B>()
and the version of
<B>getpgrp</B>()
with no arguments
conform to POSIX.1-2001.
<P>
POSIX.1-2001 also specifies
<B>getpgid</B>()
and the version of
<B>setpgrp</B>()
that takes no arguments.
(POSIX.1-2008 marks this
<B>setpgrp</B>()
specification as obsolete.)
<P>
The version of
<B>getpgrp</B>()
with one argument and the version of
<B>setpgrp</B>()
that takes two arguments derive from 4.2BSD,
and are not specified by POSIX.1.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
A child created via
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
inherits its parent's process group ID.
The PGID is preserved across an
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<P>
Each process group is a member of a session and each process is a
member of the session of which its process group is a member.
(See
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7).)
<P>
A session can have a controlling terminal.
At any time, one (and only one) of the process groups
in the session can be the foreground process group
for the terminal;
the remaining process groups are in the background.
If a signal is generated from the terminal (e.g., typing the
interrupt key to generate
<B>SIGINT</B>),
that signal is sent to the foreground process group.
(See
<B><A HREF="/cgi-bin/man/man2html?3+termios">termios</A></B>(3)
for a description of the characters that generate signals.)
Only the foreground process group may
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
from the terminal;
if a background process group tries to
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2)
from the terminal, then the group is sent a
<B>SIGTTIN</B>
signal, which suspends it.
The
<B><A HREF="/cgi-bin/man/man2html?3+tcgetpgrp">tcgetpgrp</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+tcsetpgrp">tcsetpgrp</A></B>(3)
functions are used to get/set the foreground
process group of the controlling terminal.
<P>
The
<B>setpgid</B>()
and
<B>getpgrp</B>()
calls are used by programs such as
<B><A HREF="/cgi-bin/man/man2html?1+bash">bash</A></B>(1)
to create process groups in order to implement shell job control.
<P>
If the termination of a process causes a process group to become orphaned,
and if any member of the newly orphaned process group is stopped, then a
<B>SIGHUP</B>
signal followed by a
<B>SIGCONT</B>
signal will be sent to each process
in the newly orphaned process group.
An orphaned process group is one in which the parent of
every member of process group is either itself also a member
of the process group or is a member of a process group
in a different session (see also
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7)).
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+getuid">getuid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+setsid">setsid</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+tcgetpgrp">tcgetpgrp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+tcsetpgrp">tcsetpgrp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+termios">termios</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+credentials">credentials</A></B>(7)
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAH">NOTES</A><DD>
<DT id="13"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>