Processes and Threads

Program

A program is an executable file, containing instructions and some data.
Usually it is a binary executable file, or even text code in case of interpreted languages.

Processes

process_components

A process is an running instance of a program

Each process has its own virtual memory address space.
A process memory consists of:

  1. Code memory
  2. Data memory (global and static variables)
  3. Heap (for dynamic memory allocations)
  4. Process Stack
  5. Process Control Block

In Linux, a running process can spawn a new process using system calls fork(2) and execve(2)

The Linux OS keeps track of all the running processes using a Kernel Process Table

The new process has saperate virtual memory than the process that spawned it initially.

Threads

Thread is a lightweight process.

Thread is the smallest unit of a process that a can run on a CPU core.

Each process has at least 1 thread.

All the threads spawned from a process shares the virtual memory address space of the parent process.

Each thread has its own:

  1. Thread Stack
  2. Thread Control Block

Each thread shares:

  1. Code memory
  2. Data memory
  3. Heap
  4. PCB

Threads are supported by nearly all operating systems and can be created with system calls.

In Linux, a process can spawn a new thread by using system calls like clone(2) and specifying flags like CLONE_THREAD or CLONE_VM.