Skip to main content
Be Bitwise

Process & Thread Models Across OSes

Operating System Fundamentals Compared · Operating Systems track

12 min read

In this module

How Operating Systems Create and Manage Execution

Every running program on your machine exists as a process: an instance of a program in execution, with its own address space, open files, and security context. How that process gets born, scheduled, and torn down looks wildly different on Linux, Windows, and macOS. Those differences ripple all the way up into application design and performance.

Linux: fork(), clone(), and the Process Tree

Linux inherits the Unix process model, where every process (except the initial init process, PID 1) is created by forking an existing process.

fork() and exec()

The fork() system call creates a new process by duplicating the calling one. The child inherits a copy of nearly everything the parent had: address space, open file descriptors, signal handlers, the lot. The two differences you'll use every time are the PID and what fork() returns (the man page lists the rest: the child gets its own parent PID, starts with no pending signals or timers, and doesn't inherit the parent's record locks or resource-usage counters):

c
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
int main(void) {
pid_t pid = fork();
if (pid == -1) {
perror("fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child: PID = %d, Parent PID = %d\n", getpid(), getppid());
execl("/bin/ls", "ls", "-la", NULL);
// If exec returns, it failed
perror("exec failed");
return 1;
} else {
// Parent process
printf("Parent: PID = %d, Child PID = %d\n", getpid(), pid);
int status;
waitpid(pid, &status, 0);
printf("Child exited with status %d\n", WEXITSTATUS(status));
}
return 0;
}

Modern Linux pulls off fork() with a trick called copy-on-write (COW). The child doesn't actually get its own physical copy of the parent's memory. Both processes share the same pages, marked read-only. The first time either side writes, the CPU traps a page fault and the kernel hands the offender its own private copy. The result: forking a process with gigabytes of heap costs almost nothing up front.

In page-table terms, using made-up frame numbers:

Copy-on-Write (COW)

A memory optimisation where fork() shares physical pages between parent and child, only copying a page when one process modifies it. This makes fork() nearly instantaneous regardless of process size, since no actual memory copying occurs until a write happens.

The clone() System Call

Under the hood, Linux's fork() is just a thin wrapper around clone() — a more general syscall that lets you dial in exactly what the child shares with the parent:

text
// clone() flags control what is shared:
// CLONE_VM - share memory space (threads)
// CLONE_FILES - share file descriptor table
// CLONE_FS - share filesystem information
// CLONE_SIGHAND - share signal handlers
// CLONE_THREAD - same thread group (for POSIX threads)
// CLONE_NEWNS - new mount namespace (containers)
// CLONE_NEWPID - new PID namespace (containers)

Set every sharing flag and clone() gives you a thread. Clear them all and you get an independent process, just like fork(). The Linux kernel doesn't really distinguish between processes and threads at all. Everything is a task (a task_struct), and what makes one a thread versus a process is purely which resources it happens to share with its siblings.

The Process Tree

Linux processes form a strict tree. Every process has exactly one parent, and any process can have children. Run pstree and you'll see the whole genealogy laid out:

When a parent dies without waiting for its children, those children become orphans and are adopted by PID 1 (systemd/init), or by the nearest ancestor that has marked itself a subreaper with prctl(PR_SET_CHILD_SUBREAPER). The whole tree is foundational to Unix — signals propagate down it, resources track up it, and wait() semantics depend on it.

Windows: CreateProcess() and the Flat Model

Windows took a wholly different road. There's no fork() equivalent at all. CreateProcess() does it in one shot — spin up the process and load an executable image, atomically:

c
#include <windows.h>
#include <stdio.h>
int main(void) {
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
// CreateProcess has 10 parameters
BOOL success = CreateProcess(
NULL, // lpApplicationName
"notepad.exe", // lpCommandLine
NULL, // lpProcessAttributes (security)
NULL, // lpThreadAttributes (security)
FALSE, // bInheritHandles
0, // dwCreationFlags
NULL, // lpEnvironment
NULL, // lpCurrentDirectory
&si, // lpStartupInfo
&pi // lpProcessInformation (output)
);
if (success) {
printf("Created process PID: %lu\n", pi.dwProcessId);
printf("Primary thread ID: %lu\n", pi.dwThreadId);
// Wait for the process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
return 0;
}

Three big differences from Linux:

  1. No fork/exec split: CreateProcess() does both atomically. (The native NT API can clone an address space, which is how WSL1 and some Cygwin internals emulate fork(), but Win32 programs never see it.) There's no in-between moment where a child is just a clone of the parent waiting to be re-imaged.
  2. Flat process model: Windows doesn't bother with a strict parent-child tree. The creator gets handles to the child, sure, but close those handles and the relationship effectively dissolves. Nothing gets "reparented to PID 1" because PID 1 isn't a thing here.
  3. Handle-based: CreateProcess() hands back a PROCESS_INFORMATION struct with handles to the new process and its primary thread. Those handles are kernel objects, and you have to close them yourself.
Key Concept
CreateProcess vs fork/exec

Windows CreateProcess() creates a new process and loads an executable atomically. Linux fork() first clones the parent, then exec() replaces the clone's image. The Linux approach is more flexible (the child can modify its environment between fork and exec), while the Windows approach is simpler and avoids the overhead of cloning.

Windows Thread Creation

Windows has explicit, first-class thread creation via CreateThread():

c
DWORD WINAPI ThreadFunction(LPVOID lpParam) {
int threadNum = *(int*)lpParam;
printf("Thread %d running\n", threadNum);
return 0;
}
int main(void) {
HANDLE threads[4];
int ids[4] = {0, 1, 2, 3};
for (int i = 0; i < 4; i++) {
threads[i] = CreateThread(
NULL, // Security attributes
0, // Stack size (0 = default)
ThreadFunction, // Thread function
&ids[i], // Parameter
0, // Creation flags
NULL // Thread ID output
);
}
// Wait for all threads to complete
WaitForMultipleObjects(4, threads, TRUE, INFINITE);
for (int i = 0; i < 4; i++)
CloseHandle(threads[i]);
return 0;
}

Scheduling Differences

Linux: Completely Fair Scheduler (CFS)

Linux's CFS (the default scheduler before EEVDF replaced it in kernel 6.6) tracks each task's virtual runtime (vruntime). Tasks that have used less CPU have lower vruntime, and the scheduler picks whoever's lowest. Internally, it's a red-black tree indexed by vruntime:

Task with lowest vruntime (leftmost) is scheduled next.

CFS dishes out CPU fairly while still letting you bias things via nice values (-20 to +19, lower meaning more weight). There are no fixed time slices. CFS picks a target latency and slices it proportionally across whoever's runnable.

The simulator below runs five jobs through several policies, including a simplified CFS that always picks the job with the least CPU so far. Compare its average waiting time with round-robin's, and watch how the long job, P3, keeps getting pushed back while shorter jobs catch up on their fair share. It has no Windows-style priority mode, so treat the Windows half of this section as reading only.

Non-preemptive. Processes run in arrival order until completion. Simple but can cause the convoy effect.

ProcessArrivalBurst
P106
P213
P328
P432
P544

Gantt Chart

P1
P2
P3
P4
P5
01234567891011121314151617181920212223

Avg Turnaround

12.8

Avg Waiting

8.2

Windows: Priority-Based Preemptive Scheduler

Windows uses a priority-based scheduler with 32 priority levels (0-31):

Levels 16 to 31 are the real-time range, where priorities stay fixed. Levels 1 to 15 are the dynamic range, where the kernel boosts and decays thread priorities. Level 0 is reserved for the zero-page thread. Each process's priority class sets a base, and its threads start there:

A thread can sit a couple of levels either side of its class base, and boosts can lift a dynamic thread as high as 15 but never into the real-time range.

The dispatcher always runs the highest-priority ready thread. Ties get round-robined with a time quantum. Windows also throws in priority boosting: when a thread finishes an I/O or receives user input, its priority gets a temporary nudge upward so foreground apps stay snappy.

Process Control Blocks

Every OS maintains a data structure for each process containing its state:

Linux: task_struct

c
// Simplified view of Linux task_struct
struct task_struct {
unsigned int __state; // TASK_RUNNING, TASK_INTERRUPTIBLE, etc. (was volatile long state before 5.14)
pid_t pid; // Process ID
pid_t tgid; // Thread Group ID
struct task_struct *parent; // Parent process
struct list_head children; // Child processes
struct mm_struct *mm; // Memory descriptor
struct files_struct *files; // Open file descriptors
const struct cred *cred; // Security credentials
// ... hundreds more fields
};

Windows: EPROCESS

c
// Simplified view of Windows EPROCESS (undocumented)
typedef struct _EPROCESS {
KPROCESS Pcb; // Kernel process block (scheduling)
EX_PUSH_LOCK ProcessLock;
HANDLE UniqueProcessId;
LIST_ENTRY ActiveProcessLinks; // Global process list
SIZE_T VirtualSize;
ULONG ExitStatus;
PEB *Peb; // Process Environment Block (user-visible)
TOKEN *Token; // Security token
HANDLE_TABLE *ObjectTable; // Handle table
// ... many more fields
} EPROCESS;

The big philosophical split: Linux's task_struct covers both processes and threads, with threads simply sharing the same mm_struct. Windows keeps them strictly separate, EPROCESS for processes and ETHREAD for threads, with KPROCESS and KTHREAD embedded for the scheduler half.

Practical Implications

All this trickles into how people actually build software:

  • Linux server pattern: fork() is the workhorse. Parent accepts a connection, forks a child to handle it, repeats. The pattern leans hard on COW so those forks stay cheap even with a big resident heap.
  • Windows server pattern: Windows servers lean on thread pools and I/O completion ports rather than a process per connection. CreateProcess() is heavier than fork(), and Windows threading is genuinely good, so the economics flip.
  • Container technology: Linux's clone() with namespace flags is what makes Docker and LXC possible. Windows arrived at container-shaped isolation later, via job objects and silos — a wholly different mechanism that just happens to look similar from the outside.

Knowing where these models diverge matters whenever you port code between operating systems, or design something that has to perform well on more than one of them.