![]() |
![]() |
![]() |
Create and execute a new child process
#include <spawn.h> pid_t spawn( const char * path, int fd_count, const int fd_map[ ], const struct inheritance * inherit, char * const argv[ ], char * const envp[ ] );
libc
The spawn() function creates and executes a new child process, named in path.
The spawn() function is a QNX function (based on the POSIX 1003.1d draft standard). The C library also includes several specialized spawn*() functions. Their names consist of spawn followed by several letters:
This suffix: | Indicates the function takes these arguments: |
---|---|
e | An array of environment variables. |
l | A NULL-terminated list of arguments to the program. |
p | A relative path. If the path doesn't contain a slash, the PATH environment variable is searched for the program. |
v | A vector of arguments to the program. |
As shown below, these functions eventually call spawn(), which in turn sends a message to the process manager.
Most of the spawn*() functions do a lot of work before a message is sent to procnto.
The path argument is the full pathname of the executable.
If fd_count isn't 0, fd_map must contain at least fd_count file descriptors, up to OPEN_MAX FDs. If fd_count is 0, fd_map is ignored.
If fd_count is 0, all file descriptors (except for the ones created with fcntl()'s FD_CLOEXEC flag) are inherited by the child process.
The structure specified by inherit contains at least these members:
If SPAWN_SETGROUP is set in inherit.flags and inherit.pgroup is set to SPAWN_NEWPGROUP, the child process starts a new process group with the process group ID set to its process ID.
The child process inherits the following attributes of the parent process:
The child process has several differences from the parent process:
The argv argument is a pointer to an argument vector. The value in argv[0] should point to the filename of program being loaded, but can be NULL if no arguments are being passed. The last member of argv must be a NULL pointer. The value of argv can't be NULL.
The envp argument is a pointer to an array of character pointers, each pointing to a string defining an environment variable. The array is terminated with a NULL pointer. Each pointer points to a character string of the form:
variable=value
that's used to define an environment variable. If the value of envp is NULL, then the child process inherits the environment of the parent process.
The child process can access the parent process's environment by using the environ global variable (found in <unistd.h>).
If the path is on a filesystem mounted with the ST_NOSUID flag set, the effective user ID, effective group ID, saved set-user ID and saved set-group ID are unchanged for the child process. Otherwise, if the set-user ID mode bit is set, the effective user ID of the child process is set to the owner ID of path. Similarly, if the set-group ID mode bit is set, the effective group ID of the child process is set to the group ID of path. The real user ID, real group ID and supplementary group IDs of the child process remain the same as those of the parent process. The effective user ID and effective group ID of the child process are saved as the saved set-user ID and the saved set-group ID used by the setuid().
![]() |
A parent/child relationship doesn't imply that the child process dies when the parent process dies. |
The process ID of the child process, or -1 if an error occurs (errno is set).
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), getenv(), putenv(), setenv(), sigaddset(), sigdelset(), sigemptyset(), sigfillset(), spawnl(), spawnle(), spawnlp(), spawnlpe(), spawnp(), spawnv(), spawnve(), spawnvp(), spawnvpe(), wait(), waitpid()
![]() |
![]() |
![]() |