What is the difference between a process and a thread?
EasyConceptProcesses & Threads
Answer
A process is a program in execution with its own memory space (code, data or global variables, heap and stack) and its own resources, such as open files. A thread is a lightweight unit of execution inside a process. Threads of the same process share its code, data, heap and open files, but each thread has its own stack, program counter (the address of the next instruction) and registers.
- Isolation: processes are protected from each other, so one crashing does not affect another. A bug in one thread can crash the whole process.
- Cost: creating a thread and switching between threads is cheaper, because the memory space (the page tables) does not have to change.
- Communication: threads share memory directly, which is fast but needs locks; processes need inter-process communication (IPC) such as pipes or sockets.
Example: Google Chrome runs each tab in a separate process, so a crashing tab does not close the whole browser. Inside one tab's process, several threads share the same page data: the main thread runs JavaScript, while other threads compose and draw the page on screen.
More in Processes & Threads
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is the difference between a process and a thread? | Concept | Easy | Read the answer to What is the difference between a process and a thread? | |||
| 2 | What are the states of a process, and what is a PCB? | Concept | Easy | Read the answer to What are the states of a process, and what is a PCB? | |||
| 3 | What is a context switch, and why is it considered overhead? | Concept | Medium | Read the answer to What is a context switch, and why is it considered overhead? | |||
| 4 | What does fork() do, and what does it return? | Concept | Medium | Read the answer to What does fork() do, and what does it return? | |||
| 5 | What are zombie and orphan processes? | Concept | Medium | Read the answer to What are zombie and orphan processes? |