Processes vs Threads Explained: How Programs Run Concurrently

Introduction

A process is a running program with its own memory. A thread is the scheduled unit of execution inside a process. This article explains how operating systems isolate processes, how threads share memory, how the scheduler works, and how browsers, Node.js, Python, Java, and Go use both models.

Simple Explanation

Concurrency means many tasks in progress. Parallelism means tasks truly run at the same instant on different CPU cores. Threads in one process share a workshop. Separate processes are separate workshops.

How It Works Internally

Starting a program creates a process control block, a virtual address space, a heap, and a main thread stack. Linux uses fork/clone plus exec. Windows uses CreateProcess. The MMU isolates process memory. Threads share heap and globals but have private stacks and registers. The kernel scheduler context-switches threads. Same-process thread switches are cheaper than process switches. Kernel threads map 1:1. Goroutines and virtual threads multiplex many tasks onto fewer OS threads.

Real-World Examples

Browsers use multiple processes so one tab crash does not kill the browser. Node.js uses one JS thread plus a libuv pool. CPython threads are limited by the GIL for bytecode. Java uses thread pools. Go uses goroutines. PostgreSQL often uses a process per connection.

Code

from multiprocessing import Process
import threading

Use Process for isolated memory. Use Thread for shared memory plus locks.

Misconceptions

More threads are not always faster. Async is not multithreading. Every process has at least one thread. Killing a single thread is unsafe compared with exiting a process.

Takeaways

Processes isolate. Threads share. Use processes for crash and security boundaries. Use threads for cheap shared-state work. Prefer message passing. Do not block event loops.

FAQ

Difference: process owns memory; thread runs inside it. Multiple threads per process: yes. Parallel: only with multiple cores. Browsers use processes for isolation. Race: unsynchronized shared writes. Deadlock: circular lock wait.

Related

Compiler vs Interpreter

Next Post Previous Post