⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setpgid.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 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-2003 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, 2003 Edition<br>Copyright &copy; 2001-2003 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 ofthe calling process. The process group ID of a session leader shall not change. Upon successful completion, the process group ID ofthe 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, theprocess ID of the calling process shall be used. Also, if <i>pgid</i> is 0, the process ID of the indicated process shall beused.</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 toindicate 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 hassuccessfully 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 isnot 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 asthe 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 callingprocess.</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 orbackground, 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 ismore 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 itssession 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 processgroup.</p></li></ol><p>To address these constraints, the following actions are performed. The new processes call <i>setpgid</i>() to alter their ownprocess 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 bythe synchronization property of <a href="../functions/vfork.html"><i>vfork</i>()</a>; that is, the shell is suspended until thechild 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 wasconsidered, but it was decided instead to merely allow the parent shell process to adjust the process group of its child processesvia <i>setpgid</i>(). Both timing constraints are now satisfied by having both the parent shell and the child attempt to adjust theprocess 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><ahref="../functions/exec.html">exec</a></i>), and because the child process would already have adjusted its process group beforethis, 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 onein effect when the job control shell was executed). A job control shell does this before returning control back to its parent whenit is terminating or suspending itself as a way of restoring its job control &quot;state&quot; back to what its parent would expect. (Notethat the original process group of the job control shell typically matches the process group of its parent, but this is notnecessarily 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> , <ahref="setsid.html"><i>setsid</i>()</a> , <a href="tcsetpgrp.html"><i>tcsetpgrp</i>()</a> , the Base Definitions volume ofIEEE&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 isremoved.</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 <ahref="../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> was required for conforming implementations of previous POSIXspecifications, 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 FIPSrequirement.</p></li></ul><p>IEEE&nbsp;Std 1003.1-2001/Cor&nbsp;1-2002, item XSH/TC1/D6/56 is applied, changing the wording in the DESCRIPTION from &quot;theprocess group ID of the indicated process shall be used&quot; to &quot;the process ID of the indicated process shall be used&quot;. This changereverts the wording to as in the ISO&nbsp;POSIX-1:1996 standard; it appeared to be an unintentional change.</p></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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -