Roadmap › How Computers Work — CPU, Memory, OS, Processes and Threads › Module: Operating System, Processes and Threads
Lessons in this course (11)

8. Processes vs threads, scheduling and context switches

Intermediate★ Must knowLesson 8 of 11
ProcessThread
WhatA running program with its own address space and resourcesA path of execution inside a process
MemoryIsolated from other processesShares the process heap with other threads; own stack and registers
Creation costHighLower (still ~1 MB stack reserved, kernel object)
CommunicationIPC: pipes, sockets, shared memory, queuesShared variables (needs synchronisation)
Crash impactOnly that processUnhandled exception kills the whole process

Scheduling: the OS gives each runnable thread a time slice on a core, then switches to another — a context switch (save registers, load another thread's state, cache gets colder). Thousands of threads → lots of switching overhead. That's why .NET uses a thread pool (reuse a small number of threads) and async/await (don't hold a thread while waiting for I/O).

Thread states: new → ready → running → waiting/blocked (I/O, lock, sleep) → ready … → terminated.

csharp
// Process-level info from .NET
var p = Process.GetCurrentProcess();
Console.WriteLine($"PID {p.Id}, threads {p.Threads.Count}, working set {p.WorkingSet64 / 1_048_576} MB");
Console.WriteLine($"Cores: {Environment.ProcessorCount}");