📄 posix_spawnp.html
字号:
<p>All process attributes, other than those influenced by the attributes set in the object referenced by <i>attrp</i> as specifiedabove or by the file descriptor manipulations specified in <i>file_actions</i>, shall appear in the new process image as though <ahref="../functions/fork.html"><i>fork</i>()</a> had been called to create a child process and then a member of the <i><a href="../functions/exec.html">exec</a></i> family of functions had been called by the child process to execute the new processimage.</p><p><sup>[<a href="javascript:open_code('THR')">THR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">It is implementation-defined whether the fork handlers are run when <i>posix_spawn</i>() or <i>posix_spawnp</i>() is called. <imgsrc="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_427_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>posix_spawn</i>() and <i>posix_spawnp</i>() shall return the process ID of the child process tothe parent process, in the variable pointed to by a non-NULL <i>pid</i> argument, and shall return zero as the function returnvalue. Otherwise, no child process shall be created, the value stored into the variable pointed to by a non-NULL <i>pid</i> isunspecified, and an error number shall be returned as the function return value to indicate the error. If the <i>pid</i> argumentis a null pointer, the process ID of the child is not returned to the caller.</p></blockquote><h4><a name="tag_03_427_05"></a>ERRORS</h4><blockquote><p>The <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions may fail if:</p><dl compact><dt>[EINVAL]</dt><dd>The value specified by <i>file_actions</i> or <i>attrp</i> is invalid.</dd></dl><p>If this error occurs after the calling process successfully returns from the <i>posix_spawn</i>() or <i>posix_spawnp</i>()function, the child process may exit with exit status 127.</p><p>If <i>posix_spawn</i>() or <i>posix_spawnp</i>() fail for any of the reasons that would cause <a href="../functions/fork.html"><i>fork</i>()</a> or one of the <i><a href="../functions/exec.html">exec</a></i> family of functions tofail, an error value shall be returned as described by <a href="../functions/fork.html"><i>fork</i>()</a> and <i><a href="../functions/exec.html">exec</a></i>, respectively (or, if the error occurs after the calling process successfully returns, thechild process shall exit with exit status 127).</p><p>If POSIX_SPAWN_SETPGROUP is set in the <i>spawn-flags</i> attribute of the object referenced by <i>attrp</i>, and<i>posix_spawn</i>() or <i>posix_spawnp</i>() fails while changing the child's process group, an error value shall be returned asdescribed by <a href="../functions/setpgid.html"><i>setpgid</i>()</a> (or, if the error occurs after the calling processsuccessfully returns, the child process shall exit with exit status 127).</p><p><sup>[<a href="javascript:open_code('PS')">PS</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> IfPOSIX_SPAWN_SETSCHEDPARAM is set and POSIX_SPAWN_SETSCHEDULER is not set in the <i>spawn-flags</i> attribute of the objectreferenced by <i>attrp</i>, then if <i>posix_spawn</i>() or <i>posix_spawnp</i>() fails for any of the reasons that would cause <ahref="../functions/sched_setparam.html"><i>sched_setparam</i>()</a> to fail, an error value shall be returned as described by <ahref="../functions/sched_setparam.html"><i>sched_setparam</i>()</a> (or, if the error occurs after the calling process successfullyreturns, the child process shall exit with exit status 127).</p><p>If POSIX_SPAWN_SETSCHEDULER is set in the <i>spawn-flags</i> attribute of the object referenced by <i>attrp</i>, and if<i>posix_spawn</i>() or <i>posix_spawnp</i>() fails for any of the reasons that would cause <a href="../functions/sched_setscheduler.html"><i>sched_setscheduler</i>()</a> to fail, an error value shall be returned as described by <ahref="../functions/sched_setscheduler.html"><i>sched_setscheduler</i>()</a> (or, if the error occurs after the calling processsuccessfully returns, the child process shall exit with exit status 127). <img src="../images/opt-end.gif" alt="[Option End]"border="0"></p><p>If the <i>file_actions</i> argument is not NULL, and specifies any <i>close</i>, <i>dup2</i>, or <i>open</i> actions to beperformed, and if <i>posix_spawn</i>() or <i>posix_spawnp</i>() fails for any of the reasons that would cause <a href="../functions/close.html"><i>close</i>()</a>, <a href="../functions/dup2.html"><i>dup2</i>()</a>, or <a href="../functions/open.html"><i>open</i>()</a> to fail, an error value shall be returned as described by <a href="../functions/close.html"><i>close</i>()</a>, <a href="../functions/dup2.html"><i>dup2</i>()</a>, and <a href="../functions/open.html"><i>open</i>()</a>, respectively (or, if the error occurs after the calling process successfully returns,the child process shall exit with exit status 127). An open file action may, by itself, result in any of the errors described by <ahref="../functions/close.html"><i>close</i>()</a> or <a href="../functions/dup2.html"><i>dup2</i>()</a>, in addition to thosedescribed by <a href="../functions/open.html"><i>open</i>()</a>.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_427_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_427_07"></a>APPLICATION USAGE</h4><blockquote><p>These functions are part of the Spawn option and need not be provided on all implementations.</p></blockquote><h4><a name="tag_03_427_08"></a>RATIONALE</h4><blockquote><p>The <i>posix_spawn</i>() function and its close relation <i>posix_spawnp</i>() have been introduced to overcome the followingperceived difficulties with <a href="../functions/fork.html"><i>fork</i>()</a>: the <a href="../functions/fork.html"><i>fork</i>()</a> function is difficult or impossible to implement without swapping or dynamic addresstranslation.</p><ul><li><p>Swapping is generally too slow for a realtime environment.</p></li><li><p>Dynamic address translation is not available everywhere that POSIX might be useful.</p></li><li><p>Processes are too useful to simply option out of POSIX whenever it must run without address translation or other MMUservices.</p></li></ul><p>Thus, POSIX needs process creation and file execution primitives that can be efficiently implemented without address translationor other MMU services.</p><p>The <i>posix_spawn</i>() function is implementable as a library routine, but both <i>posix_spawn</i>() and <i>posix_spawnp</i>()are designed as kernel operations. Also, although they may be an efficient replacement for many <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i> pairs, their goal is to provide usefulprocess creation primitives for systems that have difficulty with <a href="../functions/fork.html"><i>fork</i>()</a>, not toprovide drop-in replacements for <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i>.</p><p>This view of the role of <i>posix_spawn</i>() and <i>posix_spawnp</i>() influenced the design of their API. It does not attemptto provide the full functionality of <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i> in which arbitrary user-specified operations of any sort are permitted between the creationof the child process and the execution of the new process image; any attempt to reach that level would need to provide aprogramming language as parameters. Instead, <i>posix_spawn</i>() and <i>posix_spawnp</i>() are process creation primitives likethe <i>Start_Process</i> and <i>Start_Process_Search</i> Ada language bindings package <i>POSIX_Process_Primitives</i> and alsolike those in many operating systems that are not UNIX systems, but with some POSIX-specific additions.</p><p>To achieve its coverage goals, <i>posix_spawn</i>() and <i>posix_spawnp</i>() have control of six types of inheritance: filedescriptors, process group ID, user and group ID, signal mask, scheduling, and whether each signal ignored in the parent willremain ignored in the child, or be reset to its default action in the child.</p><p>Control of file descriptors is required to allow an independently written child process image to access data streams opened byand even generated or read by the parent process without being specifically coded to know which parent files and file descriptorsare to be used. Control of the process group ID is required to control how the child process' job control relates to that of theparent.</p><p>Control of the signal mask and signal defaulting is sufficient to support the implementation of <a href="../functions/system.html"><i>system</i>()</a>. Although support for <a href="../functions/system.html"><i>system</i>()</a> is notexplicitly one of the goals for <i>posix_spawn</i>() and <i>posix_spawnp</i>(), it is covered under the "at least 50%" coveragegoal.</p><p>The intention is that the normal file descriptor inheritance across <a href="../functions/fork.html"><i>fork</i>()</a>, thesubsequent effect of the specified spawn file actions, and the normal file descriptor inheritance across one of the <i><a href="../functions/exec.html">exec</a></i> family of functions should fully specify open file inheritance. The implementation need makeno decisions regarding the set of open file descriptors when the child process image begins execution, those decisions havingalready been made by the caller and expressed as the set of open file descriptors and their FD_CLOEXEC flags at the time of thecall and the spawn file actions object specified in the call. We have been assured that in cases where the POSIX<i>Start_Process</i> Ada primitives have been implemented in a library, this method of controlling file descriptor inheritance maybe implemented very easily.</p><p>We can identify several problems with <i>posix_spawn</i>() and <i>posix_spawnp</i>(), but there does not appear to be a solutionthat introduces fewer problems. Environment modification for child process attributes not specifiable via the <i>attrp</i> or<i>file_actions</i> arguments must be done in the parent process, and since the parent generally wants to save its context, it ismore costly than similar functionality with <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i>. It is also complicated to modify the environment of a multi-threaded process temporarily,since all threads must agree when it is safe for the environment to be changed. However, this cost is only borne by thoseinvocations of <i>posix_spawn</i>() and <i>posix_spawnp</i>() that use the additional functionality. Since extensive modificationsare not the usual case, and are particularly unlikely in time-critical code, keeping much of the environment control out of<i>posix_spawn</i>() and <i>posix_spawnp</i>() is appropriate design.</p><p>The <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions do not have all the power of <a href="../functions/fork.html"><i>fork</i>()</a>/ <i><a href="../functions/exec.html">exec</a></i>. This is to be expected. The <a href="../functions/fork.html"><i>fork</i>()</a> function is a wonderfully powerful operation. We do not expect to duplicate itsfunctionality in a simple, fast function with no special hardware requirements. It is worth noting that <i>posix_spawn</i>() and<i>posix_spawnp</i>() are very similar to the process creation operations on many operating systems that are not UNIX systems.</p><h5><a name="tag_03_427_08_01"></a>Requirements</h5><p>The requirements for <i>posix_spawn</i>() and <i>posix_spawnp</i>() are:</p><ul><li><p>They must be implementable without an MMU or unusual hardware.</p></li><li><p>They must be compatible with existing POSIX standards.</p></li></ul><p>Additional goals are:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -