Conceptintro
Process
Also called OS process, running program, heavyweight process, child process, process isolation.
A process is a running program with its own address space. Its memory is separate from other processes, so processes share data only through the operating system.
- Applies to
- core-languages
Idea
A process is a running program.1 The program on the disk is only a set of instructions and data. The operating system loads it into memory and runs it, and the result is a process. Each process has its own address space, which is the set of memory locations that the process or its threads can reference.2 POSIX defines a live process as "An address space with one or more threads executing within that address space, and the required system resources for those threads".2 On Windows, a process also has open handles to system objects, a security context, a unique process identifier, environment variables and "at least one thread of execution".3
Because each process has its own memory, one process cannot change a variable of another process. In Python, the module multiprocessing starts a child process that runs a function. Here, the child adds 100 to a global variable and ends with the exit code 3. The parent waits for the child with join() and then reads its own copy of the variable.
import multiprocessing
import os
import sys
counter = 0
def work():
global counter
counter += 100
print("child: counter =", counter)
sys.exit(3)
if __name__ == "__main__":
child = multiprocessing.Process(target=work)
child.start()
child.join()
print("child exit code:", child.exitcode)
print("child pid differs:", child.pid != os.getpid())
print("parent: counter =", counter)
child: counter = 100
child exit code: 3
child pid differs: True
parent: counter = 0
The child changed its own copy of counter, and the parent still sees 0. The value that sys.exit gives becomes the exit code of the child.4 The parent prints only after join() returns, so the output is the same on every run. Two processes that must share data send it through the operating system, for example through a pipe, a socket, a file or a region of shared memory. Thread are the other choice: the threads of one process share its memory.
How it works
The state of a process
The operating system keeps the machine state of each process: what the program can read or change while it runs. This state includes the memory of the process, which is its address space, and the registers of the processor, among them the program counter.1 The operating system keeps a process list with an entry for each process. Some texts call this entry a process control block.1
A process is in one of three basic states:1
- Running. The process executes instructions on a processor.
- Ready. The process can run, but the operating system has chosen not to run it at the moment.
- Blocked. The process waits for an event, for example the end of a disk read. Another process can use the processor in the meantime.
scheduled
Ready ------------> Running
^ <------------ |
| descheduled | starts I/O
| v
+--- I/O done --- Blocked
With only a few processors, the operating system gives the illusion of many. It runs one process, stops it, runs another, and so on. This technique is time sharing of the CPU.1
Creation: fork, exec and spawn
On POSIX systems, fork creates a new process, the child, as a copy of the calling process, the parent. The child gets a unique process ID. It has its own copy of the file descriptors of the parent, and each copy refers to the same open file description as in the parent.5 fork returns 0 in the child and the process ID of the child in the parent, so the two copies can take different paths.5
The functions of the exec family replace the program of the current process with a new program from an executable file. A successful exec does not return, because the new program overlays the old one.6 A shell runs a command with both calls. It forks, the child calls exec, and the parent waits for the child. OSTEP explains why the two calls are separate. The child can run code between fork and exec, and this code can change the environment of the new program, for example to redirect its output to a file.7
POSIX also lists posix_spawn and posix_spawnp as ways to create a child process.2 They create the child and start the new program in one call. The process APIs of most languages, such as ProcessBuilder in Java or Command in Rust, also start a program in one step.
Exit status and zombies
A process ends when its program ends or when a signal stops it. It leaves an exit status for its parent. Until the parent consumes this status, the remains of the process stay in the system as a zombie process.2 The lifetime of a process ends when its process ID goes back to the system.2 A parent that never waits for its children therefore keeps zombies, and each one holds a process ID.
Processes and threads
| Processes | Threads of one process | |
|---|---|---|
| Memory | each has its own address space | all share the address space of the process |
| Shared data | only through the operating system | any variable that both threads can reach |
| A bad write | stays inside the process | can corrupt the data of every thread |
| Context switch | changes the address space | keeps the address space |
| Typical use | separate programs; isolation | parallel work on shared data |
When the operating system switches between two threads of one process, the address space stays the same, so it does not change the page table.8 POSIX says that many resources are shared among all threads of a process, among them the process ID and the parent process ID.2 OSTEP gives the rule for a choice. Threads share an address space, which makes it easy to share data. Processes "are a more sound choice for logically separate tasks where little sharing of data structures in memory is needed."8
Process-based concurrency
Several processes can run at the same time on several processors. Python uses this to get parallel work in the default build of CPython, where only one thread at a time runs Python bytecode. The multiprocessing module offers "effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads".4 A ProcessPoolExecutor gives the same through the concurrent.futures API.9
The price is the copy. The arguments of a multiprocessing.Process usually need to be picklable, because the module sends them to the child.4 A ProcessPoolExecutor can execute and return only picklable objects.9
Python has three start methods for a child process:4
- spawn starts a fresh interpreter. The child inherits only the resources that it needs to run the target. This method is slower than the other two.
- fork forks the interpreter, so the child begins as a copy of the parent and inherits all its resources.
- forkserver starts a server process, which then forks a child for each request. It became the default on POSIX platforms in Python 3.14, to keep the speed of fork and avoid the problems of forking a multi-threaded process.
In languages
| Language | Verdict | Note |
|---|---|---|
| Python | Standard library | |
| C++ | Standard library | |
| Java | Standard library | |
| C | Standard library | The POSIX functions fork, the exec family, waitpid and posix_spawn, as in the C++ example; no separate example file. |
| C# | Standard library | |
| JavaScript | Standard library | |
| TypeScript | Standard library | TypeScript on Node.js uses the module node:child_process, as in the JavaScript example. No example file exists. |
| Go | Standard library | |
| Rust | Standard library | |
| Kotlin | Standard library | On the JVM, Kotlin uses java.lang.ProcessBuilder and Process, as in the Java example; no example file because kotlinc is not installed on the machine that recorded the examples. |
| SQL | Does not apply | A query runs inside the database server and does not start processes. |
| Bash | Native | Each external command runs in its own process; & starts it in the background, $! gives its process ID and wait collects its exit status. No example file exists. |
| PowerShell | Standard library | Start-Process starts a program and Get-Process lists processes; no example file because pwsh is not installed on the machine that recorded the examples. |
| HTML | Does not apply | Markup has no processes. |
| CSS | Does not apply | Style rules have no processes. |
| Regular expressions | Does not apply | A pattern has no processes. |
The example files start a child process and show that the child works on its own copy of the data. The C++ example uses fork. The other examples start sh with an environment variable, and the child changes its copy of the variable. The table gives the verdict for each tier 1 language.
| Language | Verdict | Mechanism |
|---|---|---|
| Python | stdlib | subprocess, multiprocessing, os.fork |
| JavaScript | stdlib | node:child_process in Node.js |
| TypeScript | stdlib | as JavaScript |
| Java | stdlib | ProcessBuilder, Process, ProcessHandle |
| Kotlin | stdlib | the Java classes on the JVM |
| C# | stdlib | System.Diagnostics.Process |
| Go | stdlib | os/exec; os.StartProcess |
| Rust | stdlib | std::process::Command |
| C | stdlib | the POSIX calls fork, exec, waitpid and posix_spawn |
| C++ | stdlib | the POSIX calls of C |
| Bash | native | each command runs in a process; &, $! and wait |
| PowerShell | stdlib | Start-Process, Get-Process |
| SQL | n/a | a query runs inside the database server |
| Regex | n/a | a pattern has no processes |
| HTML/CSS | n/a | markup and style rules have no processes |
- Java.
ProcessBuilderis the class that is "used to create operating system processes". Itsstart()method returns a newProcess, and it can be called many times to start more processes with the same attributes.10 - Go. The package
os/execruns external commands. It does not start a shell: it "intentionally does not invoke the system shell and does not expand any glob patterns".11 - Rust.
Commandis "A process builder, providing fine-grained control over how a new process should be spawned".12 - C#. The class
Process"Provides access to local and remote processes and enables you to start and stop local system processes."13 - JavaScript. Node.js has the module
node:child_process, which spawns subprocesses "in a manner that is similar, but not identical, to popen(3)".14
import os
import subprocess
SCRIPT = 'echo "child sees GREETING=$GREETING"; GREETING=changed; echo "child set GREETING=$GREETING"; exit 3'
greeting = "hello"
env = dict(os.environ, GREETING=greeting)
child = subprocess.Popen(["sh", "-c", SCRIPT], env=env, stdout=subprocess.PIPE, text=True)
out, _ = child.communicate()
print(out, end="")
print("child exit status:", child.returncode)
print("child pid differs:", child.pid != os.getpid())
print("parent greeting:", greeting)
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: True
parent greeting: hello
- Verdict: stdlib.
subprocessstarts other programs,multiprocessingruns Python functions in child processes, andos.forkis the POSIX call. Popenstarts the child and returns at once.communicate()reads the output of the child and waits for it to end, soreturncodeis set after it.- The child gets a copy of the environment, with
GREETINGadded. The assignment in the shell changes only that copy. - For CPU-bound Python code,
multiprocessingandProcessPoolExecutorrun work in parallel in separate processes, as the Idea section shows.
#include <cstdio>
#include <print>
#include <sys/wait.h>
#include <unistd.h>
int counter = 0;
int main() {
std::fflush(stdout);
pid_t pid = fork();
if (pid == -1) {
return 1;
}
if (pid == 0) {
counter += 100;
std::println("child: counter = {}", counter);
std::fflush(stdout);
_exit(3);
}
int status = 0;
waitpid(pid, &status, 0);
std::println("child exit status: {}", WIFEXITED(status) ? WEXITSTATUS(status) : -1);
std::println("child pid differs: {}", pid != getpid());
std::println("parent: counter = {}", counter);
}
child: counter = 100
child exit status: 3
child pid differs: true
parent: counter = 0
- Verdict: stdlib through POSIX. The example calls
forkandwaitpidfrom the POSIX C API. forkreturns twice: 0 in the child and the ID of the child in the parent. The child changes its own copy ofcounter.- The child ends with
_exit, which does not flush the buffers ofstdout, so the child flushes them first. The parent also flushes beforefork, so the child does not inherit and print a copy of buffered output. waitpidwaits for the child and collects its status, so no zombie stays.WEXITSTATUStakes the exit code out of the status.
import java.io.IOException;
public class Main {
static final String SCRIPT =
"echo \"child sees GREETING=$GREETING\"; GREETING=changed; echo \"child set GREETING=$GREETING\"; exit 3";
public static void main(String[] args) throws IOException, InterruptedException {
String greeting = "hello";
ProcessBuilder builder = new ProcessBuilder("sh", "-c", SCRIPT);
builder.environment().put("GREETING", greeting);
Process child = builder.start();
String out = new String(child.getInputStream().readAllBytes());
int status = child.waitFor();
System.out.print(out);
System.out.println("child exit status: " + status);
System.out.println("child pid differs: " + (child.pid() != ProcessHandle.current().pid()));
System.out.println("parent greeting: " + greeting);
}
}
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: true
parent greeting: hello
- Verdict: stdlib.
ProcessBuilderholds the command, the environment and the directory;start()returns aProcess. environment()is a copy of the environment of the JVM, soputchanges only what the child gets.- The program reads all the output before
waitFor(). A child that fills the pipe while the parent waits without reading can block both. ProcessHandle.current()describes the JVM process itself, andProcess.pid()gives the ID of the child.
using System;
using System.Diagnostics;
const string Script =
"echo \"child sees GREETING=$GREETING\"; GREETING=changed; echo \"child set GREETING=$GREETING\"; exit 3";
string greeting = "hello";
var info = new ProcessStartInfo("sh") { RedirectStandardOutput = true };
info.ArgumentList.Add("-c");
info.ArgumentList.Add(Script);
info.Environment["GREETING"] = greeting;
using var child = Process.Start(info)!;
string output = child.StandardOutput.ReadToEnd();
child.WaitForExit();
Console.Write(output);
Console.WriteLine($"child exit status: {child.ExitCode}");
Console.WriteLine($"child pid differs: {child.Id != Environment.ProcessId}");
Console.WriteLine($"parent greeting: {greeting}");
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: True
parent greeting: hello
- Verdict: stdlib.
System.Diagnostics.Processstarts, watches and stops processes;ProcessStartInfodescribes the program to start. ArgumentListpasses each argument as it is, with no quoting rules to apply by hand.EnvironmentinProcessStartInfostarts as a copy of the environment of the parent. The change goes only to the child.Processholds operating system handles, so theusingdeclaration disposes of it.
import { spawnSync } from "node:child_process";
const script =
'echo "child sees GREETING=$GREETING"; GREETING=changed; echo "child set GREETING=$GREETING"; exit 3';
const greeting = "hello";
const child = spawnSync("sh", ["-c", script], {
env: { ...process.env, GREETING: greeting },
encoding: "utf8",
});
process.stdout.write(child.stdout);
console.log("child exit status:", child.status);
console.log("child pid differs:", child.pid !== process.pid);
console.log("parent greeting:", greeting);
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: true
parent greeting: hello
- Verdict: stdlib. The language has no processes; Node.js gives the module
node:child_process. spawnSyncblocks until the child ends and returns its output, its exit status and its process ID.spawnis the form that does not block, with events for the output and the exit.- The
envoption gives the child its own environment. The default is a copy ofprocess.env. - Each Node.js process has its own event loop and its own heap. Worker threads share the process but not the heap.
package main
import (
"errors"
"fmt"
"os"
"os/exec"
)
const script = `echo "child sees GREETING=$GREETING"; GREETING=changed; echo "child set GREETING=$GREETING"; exit 3`
func main() {
greeting := "hello"
cmd := exec.Command("sh", "-c", script)
cmd.Env = append(os.Environ(), "GREETING="+greeting)
out, err := cmd.Output()
var exitErr *exec.ExitError
if err != nil && !errors.As(err, &exitErr) {
fmt.Println("start failed:", err)
return
}
fmt.Print(string(out))
fmt.Println("child exit status:", cmd.ProcessState.ExitCode())
fmt.Println("child pid differs:", cmd.Process.Pid != os.Getpid())
fmt.Println("parent greeting:", greeting)
}
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: true
parent greeting: hello
- Verdict: stdlib.
exec.Commandbuilds aCmd;Outputstarts it, reads its standard output and waits for it. - A non-zero exit status comes back as an
*exec.ExitError.ProcessState.ExitCode()gives the number. cmd.Envis the whole environment of the child. The code appendsGREETINGto a copy of the environment of the parent.os/execdoes not start a shell, so the example namesshitself.
use std::process::{Command, Stdio};
const SCRIPT: &str =
r#"echo "child sees GREETING=$GREETING"; GREETING=changed; echo "child set GREETING=$GREETING"; exit 3"#;
fn main() -> std::io::Result<()> {
let greeting = String::from("hello");
let child = Command::new("sh")
.args(["-c", SCRIPT])
.env("GREETING", &greeting)
.stdout(Stdio::piped())
.spawn()?;
let pid = child.id();
let output = child.wait_with_output()?;
print!("{}", String::from_utf8_lossy(&output.stdout));
println!("child exit status: {}", output.status.code().unwrap_or(-1));
println!("child pid differs: {}", pid != std::process::id());
println!("parent greeting: {greeting}");
Ok(())
}
child sees GREETING=hello
child set GREETING=changed
child exit status: 3
child pid differs: true
parent greeting: hello
- Verdict: stdlib.
std::process::Commandis a builder;spawnstarts the child and returns aChild. wait_with_outputtakes theChild, reads the piped output and waits for the exit.status.code()isNonewhen a signal stopped the child.envadds one variable to the environment that the child inherits. It does not change the environment of the parent.- A
Childthat is dropped is not waited for. Callwaitorwait_with_outputso that no zombie stays behind.
Variations and related ideas
- Thread. Threads run inside one process and share its memory. They are cheaper to switch between, but a data race in one thread can break the others.
- Lightweight processes in a runtime. Erlang calls its unit of concurrency a process, but the Erlang runtime creates and schedules it. Erlang processes "are lightweight (grow and shrink dynamically) with a small memory footprint, fast to create and terminate".15 See Green threads.
- Process pools. A pool keeps a set of worker processes and gives them tasks, so the program does not pay for a new process for each task.
ProcessPoolExecutoris one.9 - Inter-process communication. Pipes, sockets, files and shared memory carry data between processes. A design that sends values and shares nothing follows Message passing.
- Run a subprocess. The task shows how to start a command, pass its arguments safely and read its output and exit status.
Pitfalls
- Expecting shared variables. A child process gets a copy of the data, or a fresh start. A change in the child does not reach the parent, as the example in the Idea section shows. Send results back through a pipe, a queue or the exit status.
- fork in a multi-threaded process. The child contains a replica of the calling thread only, with the whole address space. A lock that another thread held at the time of
forkcan stay locked forever in the child. POSIX therefore says that the child must only execute async-signal-safe operations until it callsexec.5 The Python documentation also says that "safely forking a multithreaded process is problematic".4 - The main module and spawn. With the spawn and forkserver start methods, a new interpreter imports the main module. Code at the top level that starts a process would run again in the child. Protect the entry point of the program with
if __name__ == '__main__':.4 - Zombies. A parent that does not wait for its children leaves zombie processes.2 Call
wait,join()or the equivalent for every child. - The shell. A command string that passes through a shell can expand variables, globs and other syntax in the arguments. Pass a list of arguments when possible;
os/execin Go does not start a shell at all.11
Cost
- Start. A new process needs its own address space and a new copy, or a fresh load, of the program. Python's spawn start method is "rather slow compared to using fork or forkserver".4
- Switch. A context switch between processes changes the address space. A switch between threads of one process does not.8
- Communication. Data between processes is copied, and in Python it is pickled first.4 For large data, the copy can cost more than the work itself.
History
The process comes from the multiprogrammed and time-shared systems of the 1960s. OSTEP gives two early sources for its definition of the process and its states. One is a paper on the supervisor of Multics, from 1965, which "described many of the basic ideas and terms that we find in modern systems". The other is a paper by Jack B. Dennis and Earl C. Van Horn, from 1966, which "defined many of the early terms and concepts around building multiprogrammed systems."1 The Thread page tells how Saltzer's thesis of 1966 gave the process the alternative name "thread".
References
- Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 4, The Abstraction: The Process, 4.1 The Abstraction; 4.4 Process States; 4.5 Data Structures; References
- POSIX.1-2024, Base Definitions, chapter 3, Definitions, 3.5 Address Space; 3.64 Child Process; 3.189 Live Process; 3.285 Process Lifetime; 3.426 Zombie Process
- Microsoft Learn, Win32 apps, About Processes and Threads, the first two paragraphs
- The Python Standard Library, multiprocessing, Process-based parallelism, Introduction; Contexts and start methods; Programming guidelines (Safe importing of main module); Process.exitcode
- POSIX.1-2024, fork, DESCRIPTION; RETURN VALUE
- POSIX.1-2024, exec, DESCRIPTION, paragraph 1
- Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 5, Interlude: Process API, 5.4 Why? Motivating The API
- Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, chapter 26, Concurrency: An Introduction, the introduction (context switch between threads); 26.1 Why Use Threads?
- The Python Standard Library, concurrent.futures, ProcessPoolExecutor
- Java SE 21 API, java.lang.ProcessBuilder, the class description
- Go standard library, package os/exec, Overview
- The Rust Standard Library, Struct std::process::Command, the struct description
- .NET API reference, Process Class, the summary of the class
- Node.js documentation, Child process, the introduction
- Erlang System Documentation, Processes, the introduction
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 9HX2Q0 Edit this page History Report an error Markdown