Process
- A program is an executable file which contains code that is stored as a file on a disk
- A program when in execution is a process which reside in RAM
- Process has a self-contained execution environment
- Each process has a complete, private set of basic run-time resources
- Each process has its own memory space
- To facilitate communication between processes, most operating systems support Inter Process Communication (IPC) resources like:
- https://www.wikiwand.com/en/articles/Inter-process_communication#Approaches
- Pipes (for example: command line pipes
command1 | command2) - Sockets
- Message queue
- Shared Memory
- IPC is used not just for communication between processes on the same system, but processes on different systems
Process vs Threads
- Threads are sometimes called lightweight processes
- Both processes and threads provide an execution environment, but creating a new thread requires fewer resources than creating a new process
- Threads exist within a process — every process has at least one
- Threads share the process’s resources, including memory and open files
- Threads communicate via Inter-Thread Communication (ITC) while processes communicate via IPC
- If a process misbehaves then other processes are not affected by it
- For example chrome tabs, if one tab is unresponsive other tabs work just fine
- If a thread misbehaves then whole process can go down
| Process | Thread | |
|---|---|---|
| Definition | Program in execution | Lightweight unit of program execution |
| Memory | Owns own memory | Shares memory with other threads |
| Communication | Inter-Process Communication (IPC) | Inter-Thread communication |
| Context Switching | More expensive | Less expensive |
| Creation overhead | More expensive | Less expensive |
| Examples | Google Chrome - Multiple tabs This helps Google chrome be responsive even if there is crash in one of the tabs There is also Task manager in Google chrome. Also OS task manager shows multiple PID for each tab | MS Word (or any word processor) - Spell/Grammar check - Autosave - Rendering UI Web Browser - Rendering UI |
Process and Thread Scheduling
- https://stackoverflow.com/questions/72423227/is-a-schedulable-unit-of-cpu-time-slice-process-or-thread
- OS schedules task
- A task can be threads, group of threads or processes or anything else to keep it generic
- In modern multi-core era, CPU almost always schedules threads not processes