Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Notes › Operating Systems

Process vs Thread

Process

A process is a program in execution. The program is a file on disk; when you run it, the OS creates a process with its own memory and resources. Opening Chrome three times creates three processes from one program.

Memory layout of a process

High address ┌───────────────┐
             │ Stack         │ ← function calls, local variables (grows down)
             │      ↓        │
             │      ↑        │
             │ Heap          │ ← dynamic memory: new / malloc (grows up)
             │ Data          │ ← global and static variables
             │ Text (code)   │ ← the program's instructions
Low address  └───────────────┘

Process states

new → ready ⇄ running → terminated
          ↑      ↓
          └─ waiting (blocked on I/O)

Process Control Block (PCB)

The OS keeps a PCB per process: process id, state, program counter, CPU registers, scheduling info (priority), memory info (page tables), open files, parent/child. The PCB is what gets saved and restored during a context switch.

Thread

A thread is the smallest unit of execution inside a process — a "lightweight process". One process can have many threads that run at the same time.

Threads of a process share: code, data (globals), heap, open files. Each thread has its own: stack, registers, program counter, thread id.

Process
├── code | data | heap | open files      (shared)
├── Thread 1: stack + registers + PC
├── Thread 2: stack + registers + PC
└── Thread 3: stack + registers + PC

Process vs thread

Process Thread
Definition program in execution unit of execution inside a process
Memory own address space shares the process's memory
Creation slow (copy/allocate memory) fast
Context switch expensive (change memory map, flush TLB) cheap
Communication needs IPC (pipes, sockets, shared memory) directly through shared variables
Isolation one crash doesn't affect others one thread crashing can kill the whole process
Example each browser tab in Chrome UI thread + network thread + rendering thread in one app

Context switch

Switching the CPU from one process/thread to another:

  1. Save the state (registers, PC) of the current one into its PCB/TCB.
  2. Load the state of the next one.
  3. For processes, also switch the address space (page tables) — this is why process switches cost more. No useful work happens during a switch — it's overhead.

Creating processes and threads

// a new process in Unix: fork() copies the current process
pid_t pid = fork();
if (pid == 0) printf("child\n");
else printf("parent of %d\n", pid);
// a new thread in Java
Thread t = new Thread(() -> System.out.println("hello from " + Thread.currentThread().getName()));
t.start();
t.join();   // wait for it to finish

fork() returns 0 in the child and the child's PID in the parent. A process with n fork() calls in a row creates 2ⁿ processes in total.

Why multithreading?

Problems with threads

User-level vs kernel-level threads

User-level Kernel-level
Managed by a library in user space the OS
Switching very fast (no system call) slower
Blocking I/O blocks the whole process blocks only that thread
True parallelism no (OS sees one thread) yes

Mapping models: many-to-one, one-to-one (Linux, Windows), many-to-many.

Concurrency vs parallelism

Inter-process communication (IPC)

Pipes, named pipes (FIFOs), message queues, shared memory (fastest), sockets, signals.

Special processes

Interview one-liners

More Operating Systems notes