Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/setpgid.html
2024-02-19 00:21:47 -05:00

210 lines
8.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>setpgid</title>
</head>
<body bgcolor="white">
<basefont size="3"> <a name="setpgid"></a> <a name="tag_03_658"></a><!-- setpgid -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_658_01"></a>NAME</h4>
<blockquote>setpgid - set process group ID for job control</blockquote>
<h4><a name="tag_03_658_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/unistd.h.html">unistd.h</a>&gt;<br>
<br>
int setpgid(pid_t</tt> <i>pid</i><tt>, pid_t</tt> <i>pgid</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_658_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>setpgid</i>() function shall either join an existing process group or create a new process group within the session of
the calling process. The process group ID of a session leader shall not change. Upon successful completion, the process group ID of
the process with a process ID that matches <i>pid</i> shall be set to <i>pgid</i>. As a special case, if <i>pid</i> is 0, the
process ID of the calling process shall be used. Also, if <i>pgid</i> is 0, the process group ID of the indicated process shall be
used.</p>
</blockquote>
<h4><a name="tag_03_658_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>setpgid</i>() shall return 0; otherwise, -1 shall be returned and <i>errno</i> shall be set to
indicate the error.</p>
</blockquote>
<h4><a name="tag_03_658_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>setpgid</i>() function shall fail if:</p>
<dl compact>
<dt>[EACCES]</dt>
<dd>The value of the <i>pid</i> argument matches the process ID of a child process of the calling process and the child process has
successfully executed one of the <i><a href="../functions/exec.html">exec</a></i> functions.</dd>
<dt>[EINVAL]</dt>
<dd>The value of the <i>pgid</i> argument is less than 0, or is not a value supported by the implementation.</dd>
<dt>[EPERM]</dt>
<dd>The process indicated by the <i>pid</i> argument is a session leader.</dd>
<dt>[EPERM]</dt>
<dd>The value of the <i>pid</i> argument matches the process ID of a child process of the calling process and the child process is
not in the same session as the calling process.</dd>
<dt>[EPERM]</dt>
<dd>The value of the <i>pgid</i> argument is valid but does not match the process ID of the process indicated by the <i>pid</i>
argument and there is no process with a process group ID that matches the value of the <i>pgid</i> argument in the same session as
the calling process.</dd>
<dt>[ESRCH]</dt>
<dd>The value of the <i>pid</i> argument does not match the process ID of the calling process or of a child process of the calling
process.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_658_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_658_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_658_08"></a>RATIONALE</h4>
<blockquote>
<p>The <i>setpgid</i>() function shall group processes together for the purpose of signaling, placement in foreground or
background, and other job control actions.</p>
<p>The <i>setpgid</i>() function is similar to the <a href="../functions/setpgrp.html"><i>setpgrp</i>()</a> function of 4.2 BSD,
except that 4.2 BSD allowed the specified new process group to assume any value. This presents certain security problems and is
more flexible than necessary to support job control.</p>
<p>To provide tighter security, <i>setpgid</i>() only allows the calling process to join a process group already in use inside its
session or create a new process group whose process group ID was equal to its process ID.</p>
<p>When a job control shell spawns a new job, the processes in the job must be placed into a new process group via
<i>setpgid</i>(). There are two timing constraints involved in this action:</p>
<ol>
<li>
<p>The new process must be placed in the new process group before the appropriate program is launched via one of the <i><a href=
"../functions/exec.html">exec</a></i> functions.</p>
</li>
<li>
<p>The new process must be placed in the new process group before the shell can correctly send signals to the new process
group.</p>
</li>
</ol>
<p>To address these constraints, the following actions are performed. The new processes call <i>setpgid</i>() to alter their own
process groups after <a href="../functions/fork.html"><i>fork</i>()</a> but before <i><a href=
"../functions/exec.html">exec</a></i>. This satisfies the first constraint. Under 4.3 BSD, the second constraint is satisfied by
the synchronization property of <a href="../functions/vfork.html"><i>vfork</i>()</a>; that is, the shell is suspended until the
child has completed the <i><a href="../functions/exec.html">exec</a></i>, thus ensuring that the child has completed the
<i>setpgid</i>(). A new version of <a href="../functions/fork.html"><i>fork</i>()</a> with this same synchronization property was
considered, but it was decided instead to merely allow the parent shell process to adjust the process group of its child processes
via <i>setpgid</i>(). Both timing constraints are now satisfied by having both the parent shell and the child attempt to adjust the
process group of the child process; it does not matter which succeeds first.</p>
<p>Since it would be confusing to an application to have its process group change after it began executing (that is, after <i><a
href="../functions/exec.html">exec</a></i>), and because the child process would already have adjusted its process group before
this, the [EACCES] error was added to disallow this.</p>
<p>One non-obvious use of <i>setpgid</i>() is to allow a job control shell to return itself to its original process group (the one
in effect when the job control shell was executed). A job control shell does this before returning control back to its parent when
it is terminating or suspending itself as a way of restoring its job control &quot;state&quot; back to what its parent would expect. (Note
that the original process group of the job control shell typically matches the process group of its parent, but this is not
necessarily always the case.)</p>
</blockquote>
<h4><a name="tag_03_658_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_658_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="exec.html"><i><a href="../functions/exec.html">exec</a></i>()</a> , <a href="getpgrp.html"><i>getpgrp</i>()</a> , <a
href="setsid.html"><i>setsid</i>()</a> , <a href="tcsetpgrp.html"><i>tcsetpgrp</i>()</a> , the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a>, <a href=
"../basedefs/unistd.h.html"><i>&lt;unistd.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_658_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 3. Included for alignment with the POSIX.1-1988 standard.</p>
</blockquote>
<h4><a name="tag_03_658_12"></a>Issue 6</h4>
<blockquote>
<p>In the SYNOPSIS, the optional include of the <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> header is
removed.</p>
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
<ul>
<li>
<p>The requirement to include <a href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> has been removed. Although <a
href="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIX
specifications, it was not required for UNIX applications.</p>
</li>
<li>
<p>The <i>setpgid</i>() function is mandatory since _POSIX_JOB_CONTROL is required to be defined in this issue. This is a FIPS
requirement.</p>
</li>
</ul>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>