libstd/process
Process identity, fork/exec/wait, running shell commands, reaping children, and the raw clone primitives on which libstd/thread is built.
#include <libstd/process>
Includes libstd/macro. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/process and wrapper/windows/process (see Linux and Windows). Links the shared library advapi32.dll (FFI).
Example
#include <libstd/stdio>
#include <libstd/process>
pid = fork()
if pid == 0:
print("child ", get_pid(), "\n")
exit(3)
code = wait_for_pid(pid)
print("child exited with ", code, "\n")
rc = system("ls -1 | head -3") // runs, waits, returns here
Constants
| Name | Linux | Windows | Meaning |
|---|---|---|---|
SIGHUP | 1 | 1 | |
SIGINT | 2 | 2 | |
SIGQUIT | 3 | 3 | |
SIGILL | 4 | 4 | |
SIGTRAP | 5 | 5 | |
SIGABRT | 6 | 6 | |
SIGBUS | 7 | 7 | |
SIGFPE | 8 | 8 | |
SIGKILL | 9 | 9 | |
SIGUSR1 | 10 | 10 | |
SIGSEGV | 11 | 11 | |
SIGUSR2 | 12 | 12 | |
SIGPIPE | 13 | 13 | |
SIGALRM | 14 | 14 | |
SIGTERM | 15 | 15 | |
SIGSTKFLT | 16 | 16 | |
SIGCHLD | 17 | 17 | |
SIGCONT | 18 | 18 | |
SIGSTOP | 19 | 19 | |
SIGTSTP | 20 | 20 | |
SIGTTIN | 21 | 21 | |
SIGTTOU | 22 | 22 | |
SIGURG | 23 | 23 | |
SIGXCPU | 24 | 24 | |
SIGXFSZ | 25 | 25 | |
SIGVTALRM | 26 | 26 | |
SIGPROF | 27 | 27 | |
SIGWINCH | 28 | 28 | |
SIGPOLL | 29 | 29 | |
SIGIO | 29 | 29 | |
SIGPWR | 30 | 30 | |
SIGSYS | 31 | 31 | |
CHILD_STACK_SIZE | (16 * 1024) | (16 * 1024) | |
ECHILD | -10 | -10 | |
ESRCH | -3 | -3 | |
WNOHANG | 1 | 1 | |
RLIMIT_NOFILE | 7 | — | The limit on open file descriptors, for sys_prlimit64. |
Functions
Forks the process: returns 0 in the child, the child's pid in the parent, negative on error. Note that the arena state is copied, so both processes continue with consistent memory.
Pointer to the process's envp array (as captured at start), for execve.
Runs cmd through /bin/sh -c replacing the current process. Use system to get control back.
The process id.
The parent process id.
Real user id.
Effective user id.
Real group id.
Effective group id.
Raw fork(2).
Raw kill(2).
Linux only.
Raw prctl(2) with one argument, the others 0. sys_prctl(PR_SET_PDEATHSIG, SIGTERM as long): the process receives SIGTERM when its parent ends.
Linux only.
Raw prlimit64(2): reads and/or changes a resource limit of process pid (0 for the current one). Rlimit has rlim_cur (the limit in force) and rlim_max (how far the process may raise it, -1 for no limit). Pass 0 as Rlimit for new_limit to only read, or for old_limit to only write. libstd/http uses it to raise RLIMIT_NOFILE to 4096.
Raw vfork(2).
Raw execve(2): replaces the current process image; never returns on success.
Raw wait4(2) with the status written by address.
Replaces the current process with /bin/sh -c cmd; never returns on success.
Waits for a child to exit and returns its exit code (0–255).
Collects every already-terminated child without blocking (WNOHANG) and returns how many were reaped; for servers that fork per connection.
Runs command with /bin/sh -c in a child, waits for it and returns its exit code, like C's system().