1. Process vs Thread

Program

Before understanding Process, let's first define what a Program is.

A program is a file that contains instructions or code.

It is stored on disk, but it is not running yet.

Examples: Chrome, VS Code, Microsoft Word, Spotify.

When you install Chrome on your laptop, Chrome exists as a program.

But at this point, it is just software stored on disk.

Now the question is:

What happens when you double-click Chrome and it starts running?

At that moment, the Operating System loads that program into memory and starts executing it.

A program in execution is called a Process.

Process

A process is an instance of a running program.

In simple words, a process is a running program.

Examples:

If you open Chrome and VS Code, the OS creates two separate processes.


Thread

A thread is the smallest unit of execution within a process.

Examples:


Key Difference

Process Thread
A process is an independent running program. A thread is the smallest unit of execution within a process.
Each process has its own memory and resources. Threads share the memory and resources of their process.
Processes are isolated from each other. Threads can directly communicate through shared memory.
A process can contain one or more threads. A thread cannot exist without a process.

Context Switching: The process of the CPU saving the state of the currently running process/thread and loading the state of another so execution can continue.

Process context switch is slower because the OS also switches memory address spaces.

Thread context switch is faster because threads share the same memory, so only the execution state needs to be switched.

The key idea is: the CPU pauses one execution, saves its place, and resumes another.

When to Prefer a Process

Use a Process when:

Examples:


When to Prefer Threads

Use Threads when:

Examples: