The URLs of the form `help://man/<section>/<page>` link to another help page inside the help application. All previous relative page links are replaced by this new form. This doesn't change any behavior but it looks much nicer :^) Note that man doesn't handle these new links, but the previous relative links didn't work either.
2.7 KiB
Name
posix_spawn - launch a new process
Synopsis
#include <spawn.h>
int posix_spawn(pid_t* pid, const char* executable_path, const posix_spawn_file_actions_t*, const posix_spawnattr_t*, char* const argv[], char* const envp[]);
int posix_spawnp(pid_t* pid, const char* executable_path, const posix_spawn_file_actions_t*, const posix_spawnattr_t*, char* const argv[], char* const envp[]);
Description
Spawn a new process reading the binary executable_path
, passing argv
as arguments to main()
and setting envp
as argument.
Places the process ID of the new process in pid
.
If executable_path
passed to posix_spawn
is a relative path, it is resolved relative to the current working directory.
If executable_path
passed to posix_spawnp
is a relative path, it is resolved by searching through directories specified in the PATH
environment variable.
The posix_spawn_file_actions_t
and posix_spawnattr_t
arguments may be nullptr
. If they aren't, see posix_spawn_file_actions
(2) and posix_spawnattr
(2) for what they do.
The last entry in argv
and envp
has to be nullptr
.
The new process is started as if the following steps are executed in this order:
- A new process is started as if
fork()
was called. - If the
posix_spawnattr_t
parameter is non-nullptr, it takes effect. - If the
posix_spawn_file_actions_t
parameter is non-nullptr, it takes effect. executable_path
is loaded and starts running, as ifexecve
orexecvpe
was called.
Return value
If the process is successfully forked, returns 0.
Otherwise, returns an error number. This function does not return -1 on error and does not set errno
like most other functions, it instead returns what other functions set errno
to as result.
If the process forks successfully but spawnattr or file action processing or exec fail, posix_spawn
returns 0 and the child exits with exit code 127
.
Example
This simple example launches /bin/Calculator
.
To make the child process use the parent's environment, it passes environ
from unistd.h
.
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <spawn.h>
int main()
{
const char* argv[] = { "Calculator", nullptr };
pid_t child_pid;
if ((errno = posix_spawn(&child_pid, "/bin/Calculator", nullptr, nullptr, const_cast<char**>(argv), environ)))
perror("posix_spawn");
}