# Process vs thread: key differences

> What is the difference between a process and a thread? When would you use multiple threads versus multiple processes?

Canonical: https://www.internhack.in/learn/interview/operating-systems-interview/os-process-vs-thread
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What is the difference between a process and a thread? When would you use multiple threads versus multiple processes?

## Answer

If you only prepare one OS question thoroughly, make it this one, because it shows up in almost every interview from service companies to Amazon and Microsoft. Let me give you an analogy first. A process is like a separate house with its own kitchen, furniture and locked front door. A thread is like a person living inside the house. Several people in the same house share the kitchen and the fridge, so cooperation is easy and cheap, but they can also get in each other's way, and if someone burns the house down, everyone inside is affected.

Technically, a process is an independent unit of execution with its own virtual address space, its own file descriptor table, and its own PCB. A thread, sometimes called a lightweight process, is a unit of CPU scheduling inside a process. All threads of a process share the code, the global data, the heap and the open files. What each thread has privately is its own program counter, register set, and stack (plus thread-local storage and its own thread ID). That last part is a favourite exam point: the stack is private, the heap is shared.

This sharing explains all the practical differences. Creating a thread is much cheaper than creating a process because the OS need not duplicate an address space or page tables. Switching between threads of the same process is cheaper because no address space switch occurs, so the TLB does not need flushing. Communication between threads is trivial: read and write shared variables. Communication between processes needs IPC such as pipes, sockets or shared memory, which is slower and more complex.

The flip side is safety. Because threads share memory, they need synchronization to avoid race conditions, and a single bad pointer write in one thread can corrupt data used by all the others. A crash of one thread (say a segmentation fault) typically kills the whole process. Processes are isolated: if one crashes, the others survive, and this is exactly why Chrome runs each tab in a separate process, trading memory for stability and security.

When should you use each? Use threads when the tasks are tightly coupled, share large data structures, and need low-overhead communication, for example a web server handling many requests with a worker pool or a GUI keeping the interface responsive while doing background work. Use processes when you need fault isolation, security boundaries, or want to use separate programs, for example a sandboxed renderer or a pre-fork web server such as classic Apache or Gunicorn workers.

Two edge cases interviewers may probe. First, in Python the Global Interpreter Lock means CPU-bound threads do not run truly in parallel, so people use multiprocessing instead. Second, on Linux both processes and threads are created via the clone() system call; threads are just tasks that share resources through flags, which shows that the kernel's view is more unified than textbooks suggest.

A good closing line is: threads improve responsiveness and resource sharing, but they trade isolation for speed, so you pay for it with synchronization complexity.

## Code examples

### Threads share globals but not stacks

```
#include <pthread.h>
#include <stdio.h>

int shared = 0;                       /* shared by all threads */

void *work(void *arg) {
    int local = *(int *)arg;          /* private to this thread's stack */
    shared += local;                  /* NOT safe in general - demo only */
    printf("thread local=%d shared=%d\n", local, shared);
    return NULL;
}

int main(void) {
    pthread_t t1, t2;
    int a = 1, b = 10;
    pthread_create(&t1, NULL, work, &a);
    pthread_join(t1, NULL);
    pthread_create(&t2, NULL, work, &b);
    pthread_join(t2, NULL);
    return 0;
}
```

The variable shared is the same memory location for both threads, while local lives on each thread's own stack. Compile with gcc -pthread.

## Key points

- Threads share code, data, heap and open files; each has its own stack, registers and PC.
- Thread creation and switching are cheaper than process creation and switching.
- A crash in one thread can take down the entire process.
- Processes need IPC to communicate; threads communicate through shared memory.
- Chrome uses process-per-tab for isolation; web servers use thread pools for throughput.

## Common follow-up questions

- What resources are shared and what is private among threads?
- Why is thread context switching faster than process context switching?
- What is the Python GIL?
- What happens to other threads when one thread calls exit()?

## Interview tips

- State clearly that the stack is private and the heap is shared; it is the most tested fact.
- Give a real-world example, such as Chrome tabs, for the process side.
- Mention synchronization as the cost of using threads.

## Concepts covered

thread, process, multithreading, shared memory

Previous: [Process states, state transitions and the Process Control Block](https://www.internhack.in/learn/interview/operating-systems-interview/os-process-states-and-pcb) · Next: [User-level threads vs kernel-level threads and threading models](https://www.internhack.in/learn/interview/operating-systems-interview/os-user-level-vs-kernel-level-threads) · [All of Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)

## More Operating Systems questions

- [What is a context switch and why is it expensive?](https://www.internhack.in/learn/interview/operating-systems-interview/os-context-switch-cost)
- [fork() and exec(): predict the output](https://www.internhack.in/learn/interview/operating-systems-interview/os-fork-exec-output-questions)
- [Zombie and orphan processes](https://www.internhack.in/learn/interview/operating-systems-interview/os-zombie-and-orphan-processes)
- [Inter-process communication: pipes, shared memory and message queues](https://www.internhack.in/learn/interview/operating-systems-interview/os-ipc-pipes-shared-memory-message-queues)
- [CPU scheduling algorithms: FCFS, SJF, SRTF, Round Robin, Priority](https://www.internhack.in/learn/interview/operating-systems-interview/os-cpu-scheduling-algorithms)
- [Worked example: Gantt charts and average waiting time for FCFS, SJF, SRTF and RR](https://www.internhack.in/learn/interview/operating-systems-interview/os-scheduling-gantt-chart-worked-example)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)
