
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <link rel="stylesheet" type="text/css" href="../../../style.css" title="style" />
    <link rel="stylesheet" type="text/css" href="../style.css" title="style" />
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />

    <title>posix_spawn.3p - Linux manual page</title>
</head>

<body>

<div class="page-top"><a id="top_of_page"></a></div>
<!--%%%TOP_BAR%%%-->
    <div class="nav-bar">
        <table class="nav-table">
            <tr>
                <td class="nav-cell">
                    <p class="nav-text">
                        <a href="http://man7.org/index.html">man7.org</a> &gt; Linux &gt; <a href="../index.html">man-pages</a>
                    </p>
                </td>
                <td class="training-cell">
                    <p class="training-text"><a class="training-link" href="http://man7.org/training/">Linux/UNIX system programming training</a></p>
                </td>
            </tr>
        </table>
    </div>

<hr class="nav-end" />

<!--%%%PAGE_START%%%-->


<table class="sec-table">
<tr>
    <td>
        <p class="section-dir">
<a href="posix_spawn.3p.html#PROLOG">PROLOG</a> | <a href="posix_spawn.3p.html#NAME">NAME</a> | <a href="posix_spawn.3p.html#SYNOPSIS">SYNOPSIS</a> | <a href="posix_spawn.3p.html#DESCRIPTION">DESCRIPTION</a> | <a href="posix_spawn.3p.html#RETURN_VALUE">RETURN&nbsp;VALUE</a> | <a href="posix_spawn.3p.html#ERRORS">ERRORS</a> | <a href="posix_spawn.3p.html#EXAMPLES">EXAMPLES</a> | <a href="posix_spawn.3p.html#APPLICATION_USAGE">APPLICATION&nbsp;USAGE</a> | <a href="posix_spawn.3p.html#RATIONALE">RATIONALE</a> | <a href="posix_spawn.3p.html#FUTURE_DIRECTIONS">FUTURE&nbsp;DIRECTIONS</a> | <a href="posix_spawn.3p.html#SEE_ALSO">SEE&nbsp;ALSO</a> | <a href="posix_spawn.3p.html#COPYRIGHT">COPYRIGHT</a>
        </p>
    </td>
    <td class="search-box">
        <div class="man-search-box">

            <form method="get" action="http://www.google.com/search">
                <fieldset class="man-search">
                    <input type="text" name="q" size="10" maxlength="255" value="" />
                    <input type="hidden" name="sitesearch" value="man7.org/linux/man-pages" />
                    <input type="submit" name="sa" value="Search online pages" />
                </fieldset>
            </form>

        </div>
    </td>
    <td> </td>
</tr>
</table>

<pre>
<span class="headline">POSIX_SPAWN(3P)           POSIX Programmer's Manual          POSIX_SPAWN(3P)</span>
</pre>
<h2><a id="PROLOG" href="posix_spawn.3p.html#PROLOG"></a>PROLOG  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       This manual page is part of the POSIX Programmer's Manual.  The Linux
       implementation of this interface may differ (consult the
       corresponding Linux manual page for details of Linux behavior), or
       the interface may not be implemented on Linux.
</pre>
<h2><a id="NAME" href="posix_spawn.3p.html#NAME"></a>NAME  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       posix_spawn, posix_spawnp — spawn a process (<b>ADVANCED REALTIME</b>)
</pre>
<h2><a id="SYNOPSIS" href="posix_spawn.3p.html#SYNOPSIS"></a>SYNOPSIS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       #include &lt;spawn.h&gt;

       int posix_spawn(pid_t *restrict <i>pid</i>, const char *restrict <i>path</i>,
           const posix_spawn_file_actions_t *<i>file_actions</i>,
           const posix_spawnattr_t *restrict <i>attrp</i>,
           char *const <i>argv</i>[restrict], char *const <i>envp</i>[restrict]);
       int posix_spawnp(pid_t *restrict <i>pid</i>, const char *restrict <i>file</i>,
           const posix_spawn_file_actions_t *<i>file_actions</i>,
           const posix_spawnattr_t *restrict <i>attrp</i>,
           char *const <i>argv</i>[restrict], char *const <i>envp</i>[restrict]);
</pre>
<h2><a id="DESCRIPTION" href="posix_spawn.3p.html#DESCRIPTION"></a>DESCRIPTION  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions shall create a new
       process (child process) from the specified process image. The new
       process image shall be constructed from a regular executable file
       called the new process image file.

       When a C program is executed as the result of this call, it shall be
       entered as a C-language function call as follows:

           <b>int main(int </b><i>argc</i><b>, char *</b><i>argv</i><b>[]);</b>

       where <i>argc</i> is the argument count and <i>argv</i> is an array of character
       pointers to the arguments themselves. In addition, the following
       variable:

           <b>extern char **environ;</b>

       shall be initialized as a pointer to an array of character pointers
       to the environment strings.

       The argument <i>argv</i> is an array of character pointers to null-
       terminated strings. The last member of this array shall be a null
       pointer and is not counted in <i>argc</i>.  These strings constitute the
       argument list available to the new process image. The value in
       <i>argv</i>[0] should point to a filename string that is associated with the
       process image being started by the <i>posix_spawn</i>() or <i>posix_spawnp</i>()
       function.

       The argument <i>envp</i> is an array of character pointers to null-
       terminated strings. These strings constitute the environment for the
       new process image. The environment array is terminated by a null
       pointer.

       The number of bytes available for the combined argument and
       environment lists of the child process is {ARG_MAX}.  The
       implementation shall specify in the system documentation (see the
       Base Definitions volume of POSIX.1‐2008, <i>Chapter 2</i>, <i>Conformance</i>)
       whether any list overhead, such as length words, null terminators,
       pointers, or alignment bytes, is included in this total.

       The <i>path</i> argument to <i>posix_spawn</i>() is a pathname that identifies the
       new process image file to execute.

       The <i>file</i> parameter to <i>posix_spawnp</i>() shall be used to construct a
       pathname that identifies the new process image file. If the <i>file</i>
       parameter contains a &lt;slash&gt; character, the <i>file</i> parameter shall be
       used as the pathname for the new process image file. Otherwise, the
       path prefix for this file shall be obtained by a search of the
       directories passed as the environment variable <i>PATH</i> (see the Base
       Definitions volume of POSIX.1‐2008, <i>Chapter 8</i>, <i>Environment</i>
       <i>Variables</i>).  If this environment variable is not defined, the results
       of the search are implementation-defined.

       If <i>file_actions</i> is a null pointer, then file descriptors open in the
       calling process shall remain open in the child process, except for
       those whose close-on-<i>exec</i> flag FD_CLOEXEC is set (see <a href="fcntl.3p.html">fcntl(3p)</a>).
       For those file descriptors that remain open, all attributes of the
       corresponding open file descriptions, including file locks (see
       <a href="fcntl.3p.html">fcntl(3p)</a>), shall remain unchanged.

       If <i>file_actions</i> is not NULL, then the file descriptors open in the
       child process shall be those open in the calling process as modified
       by the spawn file actions object pointed to by <i>file_actions</i> and the
       FD_CLOEXEC flag of each remaining open file descriptor after the
       spawn file actions have been processed. The effective order of
       processing the spawn file actions shall be:

        1. The set of open file descriptors for the child process shall
           initially be the same set as is open for the calling process. All
           attributes of the corresponding open file descriptions, including
           file locks (see <a href="fcntl.3p.html">fcntl(3p)</a>), shall remain unchanged.

        2. The signal mask, signal default actions, and the effective user
           and group IDs for the child process shall be changed as specified
           in the attributes object referenced by <i>attrp</i>.

        3. The file actions specified by the spawn file actions object shall
           be performed in the order in which they were added to the spawn
           file actions object.

        4. Any file descriptor that has its FD_CLOEXEC flag set (see
           <a href="fcntl.3p.html">fcntl(3p)</a>) shall be closed.

       If file descriptor 0, 1, or 2 would otherwise be closed in the new
       process image created by <i>posix_spawn</i>() or <i>posix_spawnp</i>(),
       implementations may open an unspecified file for the file descriptor
       in the new process image. If a standard utility or a conforming
       application is executed with file descriptor 0 not open for reading
       or with file descriptor 1 or 2 not open for writing, the environment
       in which the utility or application is executed shall be deemed non-
       conforming, and consequently the utility or application might not
       behave as described in this standard.

       The <b>posix_spawnattr_t </b>spawn attributes object type is defined in
       <i>&lt;spawn.h&gt;</i>.  It shall contain at least the attributes defined below.

       If the POSIX_SPAWN_SETPGROUP flag is set in the <i>spawn-flags</i> attribute
       of the object referenced by <i>attrp</i>, and the <i>spawn-pgroup</i> attribute of
       the same object is non-zero, then the child's process group shall be
       as specified in the <i>spawn-pgroup</i> attribute of the object referenced
       by <i>attrp</i>.

       As a special case, if the POSIX_SPAWN_SETPGROUP flag is set in the
       <i>spawn-flags</i> attribute of the object referenced by <i>attrp</i>, and the
       <i>spawn-pgroup</i> attribute of the same object is set to zero, then the
       child shall be in a new process group with a process group ID equal
       to its process ID.

       If the POSIX_SPAWN_SETPGROUP flag is not set in the <i>spawn-flags</i>
       attribute of the object referenced by <i>attrp</i>, the new child process
       shall inherit the parent's process group.

       If the POSIX_SPAWN_SETSCHEDPARAM flag is set in the <i>spawn-flags</i>
       attribute of the object referenced by <i>attrp</i>, but
       POSIX_SPAWN_SETSCHEDULER is not set, the new process image shall
       initially have the scheduling policy of the calling process with the
       scheduling parameters specified in the <i>spawn-schedparam</i> attribute of
       the object referenced by <i>attrp</i>.

       If the POSIX_SPAWN_SETSCHEDULER flag is set in the <i>spawn-flags</i>
       attribute of the object referenced by <i>attrp</i> (regardless of the
       setting of the POSIX_SPAWN_SETSCHEDPARAM flag), the new process image
       shall initially have the scheduling policy specified in the <i>spawn-</i>
       <i>schedpolicy</i> attribute of the object referenced by <i>attrp</i> and the
       scheduling parameters specified in the <i>spawn-schedparam</i> attribute of
       the same object.

       The POSIX_SPAWN_RESETIDS flag in the <i>spawn-flags</i> attribute of the
       object referenced by <i>attrp</i> governs the effective user ID of the child
       process. If this flag is not set, the child process shall inherit the
       effective user ID of the parent process. If this flag is set, the
       effective user ID of the child process shall be reset to the parent's
       real user ID. In either case, if the set-user-ID mode bit of the new
       process image file is set, the effective user ID of the child process
       shall become that file's owner ID before the new process image begins
       execution.

       The POSIX_SPAWN_RESETIDS flag in the <i>spawn-flags</i> attribute of the
       object referenced by <i>attrp</i> also governs the effective group ID of the
       child process. If this flag is not set, the child process shall
       inherit the effective group ID of the parent process. If this flag is
       set, the effective group ID of the child process shall be reset to
       the parent's real group ID. In either case, if the set-group-ID mode
       bit of the new process image file is set, the effective group ID of
       the child process shall become that file's group ID before the new
       process image begins execution.

       If the POSIX_SPAWN_SETSIGMASK flag is set in the <i>spawn-flags</i>
       attribute of the object referenced by <i>attrp</i>, the child process shall
       initially have the signal mask specified in the <i>spawn-sigmask</i>
       attribute of the object referenced by <i>attrp</i>.

       If the POSIX_SPAWN_SETSIGDEF flag is set in the <i>spawn-flags</i> attribute
       of the object referenced by <i>attrp</i>, the signals specified in the
       <i>spawn-sigdefault</i> attribute of the same object shall be set to their
       default actions in the child process. Signals set to the default
       action in the parent process shall be set to the default action in
       the child process.

       Signals set to be caught by the calling process shall be set to the
       default action in the child process.

       Except for SIGCHLD, signals set to be ignored by the calling process
       image shall be set to be ignored by the child process, unless
       otherwise specified by the POSIX_SPAWN_SETSIGDEF flag being set in
       the <i>spawn-flags</i> attribute of the object referenced by <i>attrp</i> and the
       signals being indicated in the <i>spawn-sigdefault</i> attribute of the
       object referenced by <i>attrp</i>.

       If the SIGCHLD signal is set to be ignored by the calling process, it
       is unspecified whether the SIGCHLD signal is set to be ignored or to
       the default action in the child process, unless otherwise specified
       by the POSIX_SPAWN_SETSIGDEF flag being set in the <i>spawn_flags</i>
       attribute of the object referenced by <i>attrp</i> and the SIGCHLD signal
       being indicated in the <i>spawn_sigdefault</i> attribute of the object
       referenced by <i>attrp</i>.

       If the value of the <i>attrp</i> pointer is NULL, then the default values
       are used.

       All process attributes, other than those influenced by the attributes
       set in the object referenced by <i>attrp</i> as specified above or by the
       file descriptor manipulations specified in <i>file_actions</i>, shall appear
       in the new process image as though <i>fork</i>() had been called to create a
       child process and then a member of the <i>exec</i> family of functions had
       been called by the child process to execute the new process image.

       It is implementation-defined whether the fork handlers are run when
       <i>posix_spawn</i>() or <i>posix_spawnp</i>() is called.
</pre>
<h2><a id="RETURN_VALUE" href="posix_spawn.3p.html#RETURN_VALUE"></a>RETURN VALUE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       Upon successful completion, <i>posix_spawn</i>() and <i>posix_spawnp</i>() shall
       return the process ID of the child process to the parent process, in
       the variable pointed to by a non-NULL <i>pid</i> argument, and shall return
       zero as the function return value.  Otherwise, no child process shall
       be created, the value stored into the variable pointed to by a non-
       NULL <i>pid</i> is unspecified, and an error number shall be returned as the
       function return value to indicate the error. If the <i>pid</i> argument is a
       null pointer, the process ID of the child is not returned to the
       caller.
</pre>
<h2><a id="ERRORS" href="posix_spawn.3p.html#ERRORS"></a>ERRORS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       These functions may fail if:

       <b>EINVAL </b>The value specified by <i>file_actions</i> or <i>attrp</i> is invalid.

       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.

       If <i>posix_spawn</i>() or <i>posix_spawnp</i>() fail for any of the reasons that
       would cause <i>fork</i>() or one of the <i>exec</i> family of functions to fail, an
       error value shall be returned as described by <i>fork</i>() and <i>exec</i>,
       respectively (or, if the error occurs after the calling process
       successfully returns, the child process shall exit with exit status
       127).

       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 as described by <i>setpgid</i>() (or, if the error occurs after the
       calling process successfully returns, the child process shall exit
       with exit status 127).

       If POSIX_SPAWN_SETSCHEDPARAM is set and POSIX_SPAWN_SETSCHEDULER is
       not set in the <i>spawn-flags</i> attribute of the object referenced by
       <i>attrp</i>, then if <i>posix_spawn</i>() or <i>posix_spawnp</i>() fails for any of the
       reasons that would cause <i>sched_setparam</i>() to fail, an error value
       shall be returned as described by <i>sched_setparam</i>() (or, if the error
       occurs after the calling process successfully returns, the child
       process shall exit with exit status 127).

       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
       <i>sched_setscheduler</i>() to fail, an error value shall be returned as
       described by <i>sched_setscheduler</i>() (or, if the error occurs after the
       calling process successfully returns, the child process shall exit
       with exit status 127).

       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 be performed, and if <i>posix_spawn</i>() or
       <i>posix_spawnp</i>() fails for any of the reasons that would cause <i>close</i>(),
       <i>dup2</i>(), or <i>open</i>() to fail, an error value shall be returned as
       described by <i>close</i>(), <i>dup2</i>(), and <i>open</i>(), 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 <i>close</i>() or
       <i>dup2</i>(), in addition to those described by <i>open</i>().

       <i>The following sections are informative.</i>
</pre>
<h2><a id="EXAMPLES" href="posix_spawn.3p.html#EXAMPLES"></a>EXAMPLES  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       None.
</pre>
<h2><a id="APPLICATION_USAGE" href="posix_spawn.3p.html#APPLICATION_USAGE"></a>APPLICATION USAGE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       These functions are part of the Spawn option and need not be provided
       on all implementations.

       See also the APPLICATION USAGE section for <a href="../man1/exec.1p.html">exec(1p)</a>.
</pre>
<h2><a id="RATIONALE" href="posix_spawn.3p.html#RATIONALE"></a>RATIONALE  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       The <i>posix_spawn</i>() function and its close relation <i>posix_spawnp</i>() have
       been introduced to overcome the following perceived difficulties with
       <i>fork</i>(): the <i>fork</i>() function is difficult or impossible to implement
       without swapping or dynamic address translation.

        *  Swapping is generally too slow for a realtime environment.

        *  Dynamic address translation is not available everywhere that
           POSIX might be useful.

        *  Processes are too useful to simply option out of POSIX whenever
           it must run without address translation or other MMU services.

       Thus, POSIX needs process creation and file execution primitives that
       can be efficiently implemented without address translation or other
       MMU services.

       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 <i>fork</i>()/<i>exec</i> pairs, their goal is to provide useful process
       creation primitives for systems that have difficulty with <i>fork</i>(), not
       to provide drop-in replacements for <i>fork</i>()/<i>exec</i>.

       This view of the role of <i>posix_spawn</i>() and <i>posix_spawnp</i>() influenced
       the design of their API. It does not attempt to provide the full
       functionality of <i>fork</i>()/<i>exec</i> in which arbitrary user-specified
       operations of any sort are permitted between the creation of the
       child process and the execution of the new process image; any attempt
       to reach that level would need to provide a programming language as
       parameters. Instead, <i>posix_spawn</i>() and <i>posix_spawnp</i>() are process
       creation primitives like the <i>Start_Process</i> and <i>Start_Process_Search</i>
       Ada language bindings package <i>POSIX_Process_Primitives</i> and also like
       those in many operating systems that are not UNIX systems, but with
       some POSIX-specific additions.

       To achieve its coverage goals, <i>posix_spawn</i>() and <i>posix_spawnp</i>() have
       control of six types of inheritance: file descriptors, process group
       ID, user and group ID, signal mask, scheduling, and whether each
       signal ignored in the parent will remain ignored in the child, or be
       reset to its default action in the child.

       Control of file descriptors is required to allow an independently
       written child process image to access data streams opened by and even
       generated or read by the parent process without being specifically
       coded to know which parent files and file descriptors are to be used.
       Control of the process group ID is required to control how the job
       control of the child process relates to that of the parent.

       Control of the signal mask and signal defaulting is sufficient to
       support the implementation of <i>system</i>().  Although support for
       <i>system</i>() is not explicitly one of the goals for <i>posix_spawn</i>() and
       <i>posix_spawnp</i>(), it is covered under the ``at least 50%'' coverage
       goal.

       The intention is that the normal file descriptor inheritance across
       <i>fork</i>(), the subsequent effect of the specified spawn file actions,
       and the normal file descriptor inheritance across one of the <i>exec</i>
       family of functions should fully specify open file inheritance. The
       implementation need make no decisions regarding the set of open file
       descriptors when the child process image begins execution, those
       decisions having already been made by the caller and expressed as the
       set of open file descriptors and their FD_CLOEXEC flags at the time
       of the call 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 may be implemented very
       easily.

       We can identify several problems with <i>posix_spawn</i>() and
       <i>posix_spawnp</i>(), but there does not appear to be a solution that
       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 is more costly than similar
       functionality with <i>fork</i>()/<i>exec</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 those invocations of
       <i>posix_spawn</i>() and <i>posix_spawnp</i>() that use the additional
       functionality. Since extensive modifications are 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.

       The <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions do not have all the
       power of <i>fork</i>()/<i>exec</i>.  This is to be expected. The <i>fork</i>() function is
       a wonderfully powerful operation. We do not expect to duplicate its
       functionality 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.

   <b>Requirements</b>
       The requirements for <i>posix_spawn</i>() and <i>posix_spawnp</i>() are:

        *  They must be implementable without an MMU or unusual hardware.

        *  They must be compatible with existing POSIX standards.

       Additional goals are:

        *  They should be efficiently implementable.

        *  They should be able to replace at least 50% of typical executions
           of <i>fork</i>().

        *  A system with <i>posix_spawn</i>() and <i>posix_spawnp</i>() and without <i>fork</i>()
           should be useful, at least for realtime applications.

        *  A system with <i>fork</i>() and the <i>exec</i> family should be able to
           implement <i>posix_spawn</i>() and <i>posix_spawnp</i>() as library routines.

   <b>Two-Syntax</b>
       POSIX <i>exec</i> has several calling sequences with approximately the same
       functionality. These appear to be required for compatibility with
       existing practice. Since the existing practice for the <i>posix_spawn*</i>()
       functions is otherwise substantially unlike POSIX, we feel that
       simplicity outweighs compatibility. There are, therefore, only two
       names for the <i>posix_spawn*</i>() functions.

       The parameter list does not differ between <i>posix_spawn</i>() and
       <i>posix_spawnp</i>(); <i>posix_spawnp</i>() interprets the second parameter more
       elaborately than <i>posix_spawn</i>().

   <b>Compatibility with POSIX.5 (Ada)</b>
       The <i>Start_Process</i> and <i>Start_Process_Search</i> procedures from the
       <i>POSIX_Process_Primitives</i> package from the Ada language binding to
       POSIX.1 encapsulate <i>fork</i>() and <i>exec</i> functionality in a manner similar
       to that of <i>posix_spawn</i>() and <i>posix_spawnp</i>().  Originally, in keeping
       with our simplicity goal, the standard developers had limited the
       capabilities of <i>posix_spawn</i>() and <i>posix_spawnp</i>() to a subset of the
       capabilities of <i>Start_Process</i> and <i>Start_Process_Search</i>; certain non-
       default capabilities were not supported. However, based on
       suggestions by the ballot group to improve file descriptor mapping or
       drop it, and on the advice of an Ada Language Bindings working group
       member, the standard developers decided that <i>posix_spawn</i>() and
       <i>posix_spawnp</i>() should be sufficiently powerful to implement
       <i>Start_Process</i> and <i>Start_Process_Search</i>.  The rationale is that if the
       Ada language binding to such a primitive had already been approved as
       an IEEE standard, there can be little justification for not approving
       the functionally-equivalent parts of a C binding. The only three
       capabilities provided by <i>posix_spawn</i>() and <i>posix_spawnp</i>() that are
       not provided by <i>Start_Process</i> and <i>Start_Process_Search</i> are optionally
       specifying the child's process group ID, the set of signals to be
       reset to default signal handling in the child process, and the
       child's scheduling policy and parameters.

       For the Ada language binding for <i>Start_Process</i> to be implemented with
       <i>posix_spawn</i>(), that binding would need to explicitly pass an empty
       signal mask and the parent's environment to <i>posix_spawn</i>() whenever
       the caller of <i>Start_Process</i> allowed these arguments to default, since
       <i>posix_spawn</i>() does not provide such defaults. The ability of
       <i>Start_Process</i> to mask user-specified signals during its execution is
       functionally unique to the Ada language binding and must be dealt
       with in the binding separately from the call to <i>posix_spawn</i>().

   <b>Process Group</b>
       The process group inheritance field can be used to join the child
       process with an existing process group. By assigning a value of zero
       to the <i>spawn-pgroup</i> attribute of the object referenced by <i>attrp</i>, the
       <i>setpgid</i>() mechanism will place the child process in a new process
       group.

   <b>Threads</b>
       Without the <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions, systems
       without address translation can still use threads to give an
       abstraction of concurrency. In many cases, thread creation suffices,
       but it is not always a good substitute. The <i>posix_spawn</i>() and
       <i>posix_spawnp</i>() functions are considerably ``heavier'' than thread
       creation. Processes have several important attributes that threads do
       not. Even without address translation, a process may have base-and-
       bound memory protection. Each process has a process environment
       including security attributes and file capabilities, and powerful
       scheduling attributes.  Processes abstract the behavior of non-
       uniform-memory-architecture multi-processors better than threads, and
       they are more convenient to use for activities that are not closely
       linked.

       The <i>posix_spawn</i>() and <i>posix_spawnp</i>() functions may not bring support
       for multiple processes to every configuration. Process creation is
       not the only piece of operating system support required to support
       multiple processes. The total cost of support for multiple processes
       may be quite high in some circumstances. Existing practice shows that
       support for multiple processes is uncommon and threads are common
       among ``tiny kernels''.  There should, therefore, probably continue
       to be AEPs for operating systems with only one process.

   <b>Asynchronous Error Notification</b>
       A library implementation of <i>posix_spawn</i>() or <i>posix_spawnp</i>() may not
       be able to detect all possible errors before it forks the child
       process. POSIX.1‐2008 provides for an error indication returned from
       a child process which could not successfully complete the spawn
       operation via a special exit status which may be detected using the
       status value returned by <i>wait</i>(), <i>waitid</i>(), and <i>waitpid</i>().

       The <i>stat_val</i> interface and the macros used to interpret it are not
       well suited to the purpose of returning API errors, but they are the
       only path available to a library implementation. Thus, an
       implementation may cause the child process to exit with exit status
       127 for any error detected during the spawn process after the
       <i>posix_spawn</i>() or <i>posix_spawnp</i>() function has successfully returned.

       The standard developers had proposed using two additional macros to
       interpret <i>stat_val</i>.  The first, WIFSPAWNFAIL, would have detected a
       status that indicated that the child exited because of an error
       detected during the <i>posix_spawn</i>() or <i>posix_spawnp</i>() operations rather
       than during actual execution of the child process image; the second,
       WSPAWNERRNO, would have extracted the error value if WIFSPAWNFAIL
       indicated a failure. Unfortunately, the ballot group strongly opposed
       this because it would make a library implementation of <i>posix_spawn</i>()
       or <i>posix_spawnp</i>() dependent on kernel modifications to <i>waitpid</i>() to
       be able to embed special information in <i>stat_val</i> to indicate a spawn
       failure.

       The 8 bits of child process exit status that are guaranteed by
       POSIX.1‐2008 to be accessible to the waiting parent process are
       insufficient to disambiguate a spawn error from any other kind of
       error that may be returned by an arbitrary process image. No other
       bits of the exit status are required to be visible in <i>stat_val</i>, so
       these macros could not be strictly implemented at the library level.
       Reserving an exit status of 127 for such spawn errors is consistent
       with the use of this value by <i>system</i>() and <i>popen</i>() to signal failures
       in these operations that occur after the function has returned but
       before a shell is able to execute. The exit status of 127 does not
       uniquely identify this class of error, nor does it provide any
       detailed information on the nature of the failure. Note that a kernel
       implementation of <i>posix_spawn</i>() or <i>posix_spawnp</i>() is permitted (and
       encouraged) to return any possible error as the function value, thus
       providing more detailed failure information to the parent process.

       Thus, no special macros are available to isolate asynchronous
       <i>posix_spawn</i>() or <i>posix_spawnp</i>() errors. Instead, errors detected by
       the <i>posix_spawn</i>() or <i>posix_spawnp</i>() operations in the context of the
       child process before the new process image executes are reported by
       setting the child's exit status to 127.  The calling process may use
       the WIFEXITED and WEXITSTATUS macros on the <i>stat_val</i> stored by the
       <i>wait</i>() or <i>waitpid</i>() functions to detect spawn failures to the extent
       that other status values with which the child process image may exit
       (before the parent can conclusively determine that the child process
       image has begun execution) are distinct from exit status 127.
</pre>
<h2><a id="FUTURE_DIRECTIONS" href="posix_spawn.3p.html#FUTURE_DIRECTIONS"></a>FUTURE DIRECTIONS  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       None.
</pre>
<h2><a id="SEE_ALSO" href="posix_spawn.3p.html#SEE_ALSO"></a>SEE ALSO  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       <a href="alarm.3p.html">alarm(3p)</a>, <a href="chmod.3p.html">chmod(3p)</a>, <a href="close.3p.html">close(3p)</a>, <a href="dup.3p.html">dup(3p)</a>, <a href="../man1/exec.1p.html">exec(1p)</a>, <a href="exit.3p.html">exit(3p)</a>,
       <a href="fcntl.3p.html">fcntl(3p)</a>, <a href="fork.3p.html">fork(3p)</a>, <a href="fstatat.3p.html">fstatat(3p)</a>, <a href="kill.3p.html">kill(3p)</a>, <a href="open.3p.html">open(3p)</a>,
       <a href="posix_spawn_file_actions_addclose.3p.html">posix_spawn_file_actions_addclose(3p)</a>,
       <a href="posix_spawn_file_actions_adddup2.3p.html">posix_spawn_file_actions_adddup2(3p)</a>,
       <a href="posix_spawn_file_actions_destroy.3p.html">posix_spawn_file_actions_destroy(3p)</a>, <a href="posix_spawnattr_destroy.3p.html">posix_spawnattr_destroy(3p)</a>,
       <a href="posix_spawnattr_getsigdefault.3p.html">posix_spawnattr_getsigdefault(3p)</a>, <a href="posix_spawnattr_getflags.3p.html">posix_spawnattr_getflags(3p)</a>,
       <a href="posix_spawnattr_getpgroup.3p.html">posix_spawnattr_getpgroup(3p)</a>, <a href="posix_spawnattr_getschedparam.3p.html">posix_spawnattr_getschedparam(3p)</a>,
       <a href="posix_spawnattr_getschedpolicy.3p.html">posix_spawnattr_getschedpolicy(3p)</a>, <a href="posix_spawnattr_getsigmask.3p.html">posix_spawnattr_getsigmask(3p)</a>,
       <a href="sched_setparam.3p.html">sched_setparam(3p)</a>, <a href="sched_setscheduler.3p.html">sched_setscheduler(3p)</a>, <a href="setpgid.3p.html">setpgid(3p)</a>, <a href="setuid.3p.html">setuid(3p)</a>,
       <a href="times.3p.html">times(3p)</a>, <a href="wait.3p.html">wait(3p)</a>, <a href="waitid.3p.html">waitid(3p)</a>

       The  Base  Definitions volume of POSIX.1‐2008, <i>Chapter 8</i>, <i>Environment</i>
       <i>Variables</i>, <a href="../man0/spawn.h.0p.html">spawn.h(0p)</a>
</pre>
<h2><a id="COPYRIGHT" href="posix_spawn.3p.html#COPYRIGHT"></a>COPYRIGHT  &nbsp; &nbsp; &nbsp; &nbsp; <a href="posix_spawn.3p.html#top_of_page"><span class="top-link">top</span></a></h2><pre>
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information
       Technology -- Portable Operating System Interface (POSIX), The Open
       Group Base Specifications Issue 7, Copyright (C) 2013 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1
       applied.) In the event of any discrepancy between this version and
       the original IEEE and The Open Group Standard, the original IEEE and
       The Open Group Standard is the referee document. The original
       Standard can be obtained online at <a href="http://www.unix.org/online.html">http://www.unix.org/online.html</a> .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the
       source files to man page format. To report such errors, see
       <a href="https://www.kernel.org/doc/man-pages/reporting_bugs.html">https://www.kernel.org/doc/man-pages/reporting_bugs.html</a> .

<span class="footline">IEEE/The Open Group                 2013                     POSIX_SPAWN(3P)</span>
</pre>

<hr class="end-man-text" />
<p>Pages that refer to this page: 
    <a href="../man0/spawn.h.0p.html">spawn.h(0p)</a>,&nbsp; 
    <a href="exec.3p.html">exec(3p)</a>,&nbsp; 
    <a href="fdopen.3p.html">fdopen(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_destroy.3p.html">posix_spawnattr_destroy(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getflags.3p.html">posix_spawnattr_getflags(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getpgroup.3p.html">posix_spawnattr_getpgroup(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getschedparam.3p.html">posix_spawnattr_getschedparam(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getschedpolicy.3p.html">posix_spawnattr_getschedpolicy(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getsigdefault.3p.html">posix_spawnattr_getsigdefault(3p)</a>,&nbsp; 
    <a href="posix_spawnattr_getsigmask.3p.html">posix_spawnattr_getsigmask(3p)</a>,&nbsp; 
    <a href="posix_spawn_file_actions_addclose.3p.html">posix_spawn_file_actions_addclose(3p)</a>,&nbsp; 
    <a href="posix_spawn_file_actions_adddup2.3p.html">posix_spawn_file_actions_adddup2(3p)</a>,&nbsp; 
    <a href="posix_spawn_file_actions_destroy.3p.html">posix_spawn_file_actions_destroy(3p)</a>,&nbsp; 
    <a href="posix_spawnp.3p.html">posix_spawnp(3p)</a>
</p>
<hr/>

 
<hr class="start-footer" />

<div class="footer"> 

<table class="colophon-table">
    <tr>
    <td class="pub-info">
        <p>
            HTML rendering created 2018-02-02
            by <a href="http://man7.org/mtk/index.html">Michael Kerrisk</a>, 
            author of 
            <a href="http://man7.org/tlpi/"><em>The Linux Programming Interface</em></a>, 
            maintainer of the 
            <a href="https://www.kernel.org/doc/man-pages/">Linux <em>man-pages</em> project</a>.
        </p>
        <p>
            For details of in-depth
            <strong>Linux/UNIX system programming training courses</strong>
            that I teach, look <a href="http://man7.org/training/">here</a>.
        </p>
        <p>
            Hosting by <a href="http://www.jambit.com/index_en.html">jambit GmbH</a>.
        </p>
        <p>
            <a href="http://validator.w3.org/check?uri=referer">
            <img src="http://www.w3.org/Icons/valid-xhtml11"
                alt="Valid XHTML 1.1" height="31" width="88" />
            </a>
        </p>
    </td>
    <td class="colophon-divider">
    </td>
    <td class="tlpi-cover">
        <a href="http://man7.org/tlpi/"><img src="../../../tlpi/cover/TLPI-front-cover-vsmall.png" alt="Cover of TLPI" /></a>
    </td>
    </tr>
</table>

</div>

<hr class="end-footer" />



<!--BEGIN-SITETRACKING-->
<!-- SITETRACKING.man7.org_linux_man-pages -->

<!-- Start of StatCounter Code (xhtml) -->

<script type="text/javascript">
//<![CDATA[
var sc_project=7422636; 
var sc_invisible=1; 
var sc_security="9b6714ff"; 
//]]>
</script>
<script type="text/javascript"
src="http://www.statcounter.com/counter/counter_xhtml.js"></script>
<noscript><div class="statcounter"><a title="website
statistics" href="http://statcounter.com/"
class="statcounter"><img class="statcounter"
src="http://c.statcounter.com/7422636/0/9b6714ff/1/"
alt="website statistics" /></a></div></noscript>

<!-- End of StatCounter Code -->


<!-- Start of Google Analytics Code -->

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-9830363-8']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

<!-- End of Google Analytics Code -->

<!--END-SITETRACKING-->

</body>
</html>
