# Threads & Processes

### Basic terms

**Program:** A program is a set of instructions or code written in a high-level programming language or machine language that defines a specific task or computation. Programs are static and do not have an execution context on their own. They are stored on storage media, such as hard drives or memory, until they are loaded into memory and executed as processes.

**Process:** A process is an instance of a program that is loaded into the computer's memory and actively executing. It has its own memory space, resources, and execution context. Each process is independent of others and runs in isolation. Processes are managed by the operating system and can run concurrently, making it possible to execute multiple programs simultaneously. Processes provide a level of isolation and security since they cannot directly access each other's memory.

**Thread:** A thread is a smaller unit of a process, representing a single sequence of instructions within the process. Threads share the same memory space and resources as the parent process, allowing them to communicate and share data more efficiently than separate processes. Threads are the basic units of CPU scheduling and can run concurrently within a single process. They are lighter in terms of resource consumption compared to processes.

<figure><img src="https://1794968486-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fmr8TiLFLTzUjsTWGBqMp%2Fuploads%2FotGU0DX0mfoccCOkcw2N%2Fimage.png?alt=media&#x26;token=ee317ebb-f5a5-4123-a3b0-0b8ea8cb5165" alt=""><figcaption></figcaption></figure>

**Multithreading:** Multithreading is a programming and execution technique that involves creating and using multiple threads within a single process. Multithreading allows a program to perform multiple tasks concurrently, taking advantage of multi-core processors and improving overall system efficiency. Threads within the same process can communicate with each other more easily than separate processes because they share the same memory space. Multithreading can lead to improved responsiveness and better resource utilization in software applications.

In summary:

* A program is a set of instructions.
* A process is an instance of a program in execution with its own memory and resources.
* A thread is a smaller unit of a process that can run concurrently with other threads within the same process.
* Multithreading is the technique of using multiple threads within a single process to achieve concurrency and better resource utilization.

### Threads VS Processes

| Characteristic       | Thread                                                                                                             | Process                                                                                                              |
| -------------------- | ------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------- |
| Definition           | Smaller unit of a process.                                                                                         | An instance of a program in execution.                                                                               |
| Resource Consumption | Lightweight; shares resources with threads.                                                                        | Heavier; each process has its resources.                                                                             |
| Memory Space         | Shares the same memory space with threads in the same process.                                                     | Has its separate memory space.                                                                                       |
| Communication        | Easier communication with other threads in the same process due to shared memory.                                  | Communication between processes is more complex and typically requires inter-process communication (IPC) mechanisms. |
| Isolation            | Less isolation as threads within the same process can directly access each other's memory.                         | More isolation between processes, as they cannot directly access each other's memory.                                |
| Creation Overhead    | Lower overhead when creating new threads.                                                                          | Higher overhead when creating new processes.                                                                         |
| Context Switching    | Faster context switching between threads.                                                                          | Slower context switching between processes.                                                                          |
| Fault Tolerance      | If one thread crashes, it can potentially affect other threads in the same process.                                | If one process crashes, it usually does not affect other processes.                                                  |
| Scalability          | Well-suited for tasks that can be parallelized within a single process, taking advantage of multi-core processors. | Used for running separate, independent tasks or programs.                                                            |

### Thread Scheduling

Thread scheduling is the process by which an operating system or a thread management system decides which threads should run, when they should run, and for how long they should run on a CPU core. Thread scheduling is crucial for efficient and fair resource utilization in multi-threaded and multi-core systems.

