14.2 posix_spawn, posix_spawnp—spawn a process

Synopsis

#include <spawn.h>

int posix_spawn(pid_t *pid, const char *path,
    const posix_spawn_file_actions_t *file_actions,
    const posix_spawnattr_t *attrp,
    char *const argv[], char *const envp[]);
int posix_spawnp(pid_t *pid, const char *file,
    const posix_spawn_file_actions_t *file_actions,
    const posix_spawnattr_t *attrp,
    char *const argv[], char *const envp[]);

Description
Use posix_spawn and posix_spawnp to create a new child process from the specified process image file. argc is the argument count and argv is an array of argument strings passed to the new program. envp is an array of stings, which are passed as environment to the new program.

The path argument to posix_spawn identifies the new process image file to execute. The file argument to posix_spawnp is used to construct a pathname that identifies the new process image file by duplicating the actions of the shell in searching for an executable file if the specified filename does not contain a ‘/’ character. The file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable.

The file descriptors remain open across posix_spawn and posix_spawnp except for those marked as close-on-exec. The open file descriptors in the child process can be modified by the spawn file actions object pointed to by file_actions.

The spawn attributes object type pointed to by attrp argument may contain any of the attributes defined in spawn.h.


Returns
posix_spawn and posix_spawnp return the process ID of the newly spawned child process in the variable pointed by a non-NULL *pid argument and zero as the function return value upon successful completion. Otherwise, posix_spawn and posix_spawnp return an error number as the function return value to indicate the error; the value stored into the variable pointed to by a non-NULL *pid argument is unspecified.


Portability
POSIX.1-2008 requires posix_spawn and posix_spawnp.

Supporting OS subroutines required: _close, dup2, _fcntl, _execve, execvpe, _exit, _open, sigaction, sigprocmask, waitpid, sched_setscheduler, sched_setparam, setegid, seteuid, setpgid, vfork.