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.
| Item | What it identifies | Valid for |
|---|---|---|
| PID | This process, while it runs | One system (or one container’s PID namespace); reusable later |
| Parent PID | The process that started it | Can change if the parent exits first |
| Address space | Its memory, through its own address mapping | This process only |
| Threads | Paths of execution inside it | Share 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.
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)
}
go run .
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.
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)
}
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?
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.
Check this idea later
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.
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.
Sign in to keep your read marks on every device. Sign in
Was this helpful?