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

NameLinuxWindowsMeaning
SIGHUP11
SIGINT22
SIGQUIT33
SIGILL44
SIGTRAP55
SIGABRT66
SIGBUS77
SIGFPE88
SIGKILL99
SIGUSR11010
SIGSEGV1111
SIGUSR21212
SIGPIPE1313
SIGALRM1414
SIGTERM1515
SIGSTKFLT1616
SIGCHLD1717
SIGCONT1818
SIGSTOP1919
SIGTSTP2020
SIGTTIN2121
SIGTTOU2222
SIGURG2323
SIGXCPU2424
SIGXFSZ2525
SIGVTALRM2626
SIGPROF2727
SIGWINCH2828
SIGPOLL2929
SIGIO2929
SIGPWR3030
SIGSYS3131
CHILD_STACK_SIZE(16 * 1024)(16 * 1024)
ECHILD-10-10
ESRCH-3-3
WNOHANG11
RLIMIT_NOFILE7—The limit on open file descriptors, for sys_prlimit64.

Functions

def fork() -> int

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.

def raw_envp() -> str

Pointer to the process's envp array (as captured at start), for execve.

def run_shell_command(cmd: str) -> int

Runs cmd through /bin/sh -c replacing the current process. Use system to get control back.

def get_pid() -> int

The process id.

def get_ppid() -> int

The parent process id.

def get_uid() -> int

Real user id.

def get_euid() -> int

Effective user id.

def get_gid() -> int

Real group id.

def get_egid() -> int

Effective group id.

def sys_fork() -> int

Raw fork(2).

def sys_kill(pid: int, sig: int) -> int

Raw kill(2).

def sys_prctl(option: int, arg2: long) -> int

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.

def sys_prlimit64(pid: int, resource: int, new_limit: Rlimit, old_limit: Rlimit) -> int

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.

def sys_vfork() -> int

Raw vfork(2).

def sys_execve(path_name: str, argv: str, envp: str) -> int

Raw execve(2): replaces the current process image; never returns on success.

def sys_wait4(pid: int, wstatus: &int, options: int, rusage: long) -> int

Raw wait4(2) with the status written by address.

def exec_shell(cmd: str) -> int

Replaces the current process with /bin/sh -c cmd; never returns on success.

def wait_for_pid(pid: int) -> int

Waits for a child to exit and returns its exit code (0–255).

def reap_zombie_children() -> int

Collects every already-terminated child without blocking (WNOHANG) and returns how many were reaped; for servers that fork per connection.

def system(command: str) -> int

Runs command with /bin/sh -c in a child, waits for it and returns its exit code, like C's system().