← Runtimes
Runtimes

Processes and address spaces

Know which process a change reached, and which it never will.

A process owns the memory and resources behind one running instance of a program.

Outcome

Explain what a process is to the operating system, predict which processes and threads observe a write, and tell what a PID or printed address identifies.

Terminology (7)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Process
A running instance of a program, as the operating system tracks it: an ID, a parent, an address space, open resources and one or more threads.
PID
The number the kernel assigns to a live process. It identifies that process on one system while it runs; the kernel can reuse it after the process ends.
Address space
The range of virtual addresses a process can use, together with the kernel’s mapping from those addresses to physical memory.
Virtual memory
Per-process addresses that the kernel and hardware translate to physical memory. The same address in two processes can refer to different memory.
Thread
An independent path of execution inside a process. Threads of one process share its address space.
Spawn
Starting a new program in a new process (POSIX fork followed by exec, or posix_spawn). The new program begins at its entry point with fresh memory.
Copy-on-write
An implementation of private memory that lets processes share physical pages until a write requires separation. Each process still observes private values; memory use and timing can expose the optimization.

What a process owns

To the operating system, a running program is a process. The kernel records a process ID (PID), the parent that started it, its open resources, its threads and an address space: the memory addresses the process can use and what they refer to.

A thread is a path of execution within a process. Sharing an operating-system address space and sharing ordinary language variables are different questions: the runtime also decides how its heaps and values are exposed. Keep the process, the thread and the language environment distinct when following a write.

01Same program, separate processes

Each running copy is its own process

A job service must stop accepting a revoked API key in every worker. The supervisor records the revocation, but requests sent to one worker still succeed. Before changing the update path, you need to know what a worker inherits when it starts and which later writes it can see.

Start by checking the deployment: these workers are separate processes, rather than tasks inside the supervisor. Each has its own address space. Ordinary global variables belong to that process; writing one does not update another process’s global. Deliberately shared memory needs a separate arrangement, which these examples do not use.

What the kernel keeps for each process
ItemWhat it identifiesValid for
PIDThis process, while it runsOne system (or one container’s PID namespace); reusable later
Parent PIDThe process that started itCan change if the parent exits first
Address spaceIts memory, through its own address mappingThis process only
ThreadsPaths of execution inside itShare this process’s address space

02Start a second process

What the new process begins with depends on how it starts

First check the worker’s starting state. A small probe replaces the service’s key table with one variable, mode, so startup and later writes can be distinguished without the request handler. It sets mode to ready, starts a worker and waits. The worker reports its value and changes it; the parent then reports its value. Assume each start succeeds.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import (
	"fmt"
	"os"
	"os/exec"
)

var mode = "starting"

func runWorker() {
	fmt.Printf("worker sees mode=%s\n", mode)
	mode = "changed-by-worker"
}

func main() {
	if len(os.Args) > 1 && os.Args[1] == "worker" {
		runWorker()
		return
	}

	mode = "ready"
	worker := exec.Command(os.Args[0], "worker")
	output, err := worker.Output()
	if err != nil {
		fmt.Println("worker failed:", err)
		return
	}

	fmt.Print(string(output))
	fmt.Printf("worker pid differs: %t\n", worker.Process.Pid != os.Getpid())
	fmt.Printf("parent sees mode=%s\n", mode)
}
terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
go run .
Run the program with the shown command. What do the worker line and the parent’s final line report?

03Threads share a process

A thread is not another process

Could keeping workers inside one process remove that update boundary? Repeat the probe with a goroutine or thread, leaving the read and write unchanged. The main path waits before its final print, so simultaneous access does not obscure the result. Node’s worker API is also worth checking before assuming that an operating-system thread shares ordinary language variables.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
package main

import (
	"fmt"
	"sync"
)

var mode = "starting"

func runWorker() {
	fmt.Printf("thread sees mode=%s\n", mode)
	mode = "changed-by-thread"
}

func main() {
	mode = "ready"

	var done sync.WaitGroup
	done.Add(1)
	go func() {
		defer done.Done()
		runWorker()
	}()
	done.Wait()

	fmt.Printf("main thread sees mode=%s\n", mode)
}
What do the thread line and the main thread’s final line report?

04Addresses and PIDs

An address means something only inside its process

While checking the worker logs, do not use a matching printed address as proof of shared state. Programs on this Linux system use virtual addresses. Hardware translates them using the current address mapping, with cached translations as well as page tables. Equal virtual addresses in two processes can refer to different physical pages. The C++ probe prints equal addresses and independent values; it does not inspect the physical pages.

Linux can avoid eagerly copying private pages at fork by using copy-on-write. Consider an ordinary private page that both processes still share. Their mappings prevent an immediate write to that shared page. What must happen when either process writes, if each is still to observe private memory?

After a fork, parent and child share the same physical page, marked read-only. The parent then writes a new value on that page. What does the child read there?

05Practice

Model a supervisor and its workers

Before wiring a real delivery channel, model what successful delivery must change. ProcessTable is an authored, synchronous model: start snapshots the supervisor’s entries, set changes one process and update represents a change already delivered to every running process. It models fork-like private state, including for natives whose real process API starts a fresh program. It does not implement messages, acknowledgments or a production revocation guarantee.

The entries can stand for a worker’s region, request limit or revoked-key flag. Give a worker its starting state, apply service changes without erasing its other entries, then preserve those observations when memories share storage until written. The last task uses whole maps as the sharing unit, rather than Linux pages. Its output establishes the model’s values, not native page allocation or physical performance.

A Go map value refers to its underlying map. Copying the value does not duplicate the entries; creating another map and inserting the entries does. The model’s Space.users counts PIDs using that space, so a start, stop or move between spaces changes that count.

One supervisor, several workers

A model of a supervisor and its worker processes. Give a new worker its starting memory, reach running workers with a configuration change, then keep the same behavior when workers share memory until written.

  1. easy · R-PROC-V4-1 Start a worker
  2. medium · R-PROC-V4-2 Reach running workers
  3. hard · R-PROC-V4-3 Share until written
Check this idea later
A web service runs four worker processes started by one parent. A request handled by worker 2 clears its in-memory cache. Which caches are now empty?
Two processes each print the address of a global counter. Both print 0x55d0c040, but the counters hold 3 and 8. What explains this?
In a Go service, a background goroutine refreshes a package-level configuration value (with proper locking). Which code sees the refreshed value?
Sources
In the frontend A cache-clear request reaches one server process. Optional

Suppose a cache-clear endpoint increments a module variable. In a long-lived Node deployment, subsequent requests in that process can see its new value. A second server process has a different variable. One successful HTTP response does not establish that every instance discarded its cached data.

app/api/cache-epoch/route.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
export const runtime = 'nodejs';
let epoch = 0;

export function POST() {
  epoch += 1;

  return Response.json({ epoch });
}

This App Router Route Handler changes one module instance. Multiple Node server instances need a shared authoritative epoch or an explicit invalidation channel. Next.js documents additional cache coordination for multiple instances; incrementing this variable does not perform that coordination.

Sources: Next.js: self-hosting and multiple instances,SvelteKit: avoid shared state on the server.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.