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
| Process | Thread | |
|---|---|---|
| What | A running program with its own address space and resources | A path of execution inside a process |
| Memory | Isolated from other processes | Shares the process heap with other threads; own stack and registers |
| Creation cost | High | Lower (still ~1 MB stack reserved, kernel object) |
| Communication | IPC: pipes, sockets, shared memory, queues | Shared variables (needs synchronisation) |
| Crash impact | Only that process | Unhandled 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}");