Skip to the page

Operating systems · core

Process, thread, states and fork

A process is a running program with its own memory. A thread is one path of execution inside a process. A process moves between states, and fork makes a new process.

The notes

What you must know

  • A process has its own memory (code, data, heap, stack), open files and a process ID. The OS keeps its details in a process control block (PCB).
  • Threads of one process share code, data, heap and open files. Each thread has its own stack, registers and program counter.
  • Processes are isolated: one crash does not touch another. A crash in one thread, such as a segmentation fault, usually ends the whole process.
  • States: New, Ready, Running, Waiting (Blocked), Terminated. Ready to Running when the scheduler picks it. Running to Ready when the time slice ends. Running to Waiting for I/O. Waiting goes to Ready, never straight to Running.
  • The PCB holds the process ID, state, program counter, CPU registers, priority, page table and open files.
  • Zombie: a child that has finished while the parent has not yet collected its exit status with wait(). It keeps only a process table entry. Orphan: a child whose parent has ended.
  • fork() returns 0 in the child, the child process ID in the parent, and -1 on failure. The child gets a copy of the parent memory, shared by copy-on-write.
  • n forks in a row give 2^n processes. exec replaces the current program with a new one; a shell forks and the child calls exec.

The red pen

Common traps: mistake → fix

  • Mistake: A thread is a small process

    Fix: threads share memory; processes do not.

  • Mistake: Waiting goes straight to Running

    Fix: it goes to Ready first and waits for the scheduler.

  • Mistake: A zombie uses CPU and memory

    Fix: it only keeps a process table entry.

  • Mistake: Three forks print 3 or 4 times

    Fix: they give 8 processes.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between a process and a thread? Use a browser as an example.
  2. Draw the states of a process.
  3. What is a zombie process?
  4. How many times does hello print: fork(); fork(); fork(); printf("hello");?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview