Computing Foundations › Inside the Machine › Day 7

Day 7: Processes, Threads, and Scheduling

Day 7 of 365 — Processes, Threads, and Scheduling

After this lesson you will be able to explain what a process really is, how the scheduler makes hundreds of programs share a few cores, and spawn, observe, and signal processes yourself from the shell.

Course
Computing Foundations
Category
Inside the Machine
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-007-processes-threads-and-scheduling

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-007-processes-threads-and-scheduling
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: the pieces the kernel creates for a new process


Historical background

Diagram: the progression from batch processing to threads


What it is — and what it is not

Diagram: a process compared with a thread

A process is:

A process is not:


Why it was created and what problems it solves


How it works

The anatomy and lifecycle of a process

Diagram: the states a process moves through, and what causes each transition

State diagram: the process lifecycle from spawn through ready, running, and waiting to exit

Threads: cheap and dangerous

Diagram: two threads incrementing a shared counter and losing one update

The scheduler: time slices, context switches, preemption

Timeline: three processes sharing one CPU core through time slices and context switches

Jobs and signals: taking the controls

SignalNo.Sent byMeaningRefusable?
SIGTERM15kill PID”Please shut down.”Yes
SIGINT2Ctrl+C”Stop what you’re doing.”Yes
SIGKILL9kill -9 PID”Cease to exist, now.”No
SIGSTOP—kill -STOP”Freeze, resumably.”No
SIGCONT—bg, fg”Resume.”—
SIGHUP1Terminal closes”Your terminal went away.”Yes

Concurrency versus parallelism, precisely


An everyday analogy

Diagram: the steps of a context switch and the overhead each adds

A single barista with four half-made drinks:


Examples in practice


Implications: security, privacy, performance, scalability, and cost


Alternatives: free, open source, and commercial

ToolTypeWhat it gives you
ps, top, killFree, built inEverything in this lesson, on any Unix
htopFree, open sourceThe same, readable, with a tree view
nohup, setsidFree, built inDetach a job so SIGHUP cannot reach it
tmux, screenFree, open sourceA terminal that survives disconnection
systemdFree, open sourceSupervising long-running services properly
Activity Monitor / Task ManagerBundledThe same data, with a mouse

Diagram: concurrency compared with parallelism

TermWhat it actually means
ProcessA running program with its own address space
ThreadAn instruction stream sharing a process’s memory
JobA shell’s name for a process it started
DaemonA process deliberately detached from any terminal
CoroutineConcurrency scheduled by your program, not the kernel

When to use it — and when not to

Diagram: SIGTERM compared with SIGKILL


The AI connection

Diagram: when to choose threads and when to choose processes


Knowledge check

quiz:
  question: "A containerised training job disappears from the logs with exit code 137 and no traceback. What happened?"
  options:
    - "The kernel sent SIGKILL, most likely because the job exceeded its memory limit"
    - "The Python process raised an unhandled exception that was not logged"
    - "The container image was corrupted and the process never fully started"
    - "The job completed successfully but the logging buffer was not flushed"
  answer_index: 0
  explanation: "128 + 9 = 137, and signal 9 is SIGKILL, which a process cannot catch. No cleanup runs and no traceback is written, which is exactly why the log ends without explanation."

Hands-on exercise

Watch processes being born, scheduled, signalled and reaped. Worked through fully in the Day 7 lab directory.

What you are observingCommand
The family treeps -o pid,ppid,stat,command
Live state and per-core loadhtop (or top)
Start a background jobsleep 300 & echo $!
List your shell’s childrenjobs
Ask politely, then insistkill $! then kill -9 $!
Read the exit codeecho $?

Expected output

  PID  PPID STAT COMMAND
 4812  4801 S    sleep 300
[1]+  Terminated              sleep 300
143

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. What is the difference between a program and a process?

  1. A program is a file on disk; a process is a running instance of it
  2. They are two words for the same thing
  3. A program runs in RAM while a process runs on the disk
  4. A process is the source code and a program is the compiled version
Show answer

Answer: A. A program is a file on disk; a process is a running instance of it

A program is just a file until the operating system loads it into memory, assigns it a PID, gives it private memory and file handles, and starts executing it — that running instance is a process, and one program can run as many processes at once.

Q2. A process asks to read a block from disk. Which lifecycle state does it enter while the disk works, and what does that cost in CPU time?

  1. Running — it keeps its core until the read completes
  2. Waiting — it is blocked until the event arrives and consumes no CPU at all
  3. Ready — it goes back into the scheduler queue and keeps consuming its slice
  4. Zombie — it is dead until the disk revives it
Show answer

Answer: B. Waiting — it is blocked until the event arrives and consumes no CPU at all

A process blocked on input or output moves to the waiting state, where the scheduler ignores it entirely; when the event arrives it returns to ready. Blocked processes cost no CPU, which is why one core can host hundreds of mostly-idle processes.

Q3. What exactly is a zombie process?

  1. A process whose parent died before it did
  2. A process consuming 100 percent CPU that cannot be killed
  3. A process that has exited but whose exit code is uncollected
  4. A background process detached from any terminal
Show answer

Answer: C. A process that has exited but whose exit code is uncollected

When a process exits, the kernel keeps a small process-table entry holding its exit code until the parent reads it with a wait call; until then the dead process is a zombie. A few momentary zombies are normal — accumulating zombies indicate a parent that never reaps its children.

Q4. Why are threads described as cheap compared with processes — and what makes them dangerous?

  1. Threads run on special low-power cores, but overheat easily
  2. Threads copy the whole process memory at creation, which is fast but wastes RAM
  3. Threads are cheaper because the scheduler never preempts them, which risks freezing the machine
  4. Threads share the process's memory, which is why races are possible
Show answer

Answer: D. Threads share the process's memory, which is why races are possible

Creating a thread only adds a stack and registers inside an existing address space, so it is far lighter than creating a process. But because all threads of a process share one memory, simultaneous unsynchronized updates can silently corrupt data — the race condition.

Q5. Your laptop has 14 cores and 480 processes. How do they all appear to run at once?

  1. Most are waiting; the scheduler time-slices too fast to notice
  2. The operating system compresses processes so 480 fit onto 14 cores simultaneously
  3. Each core secretly contains about 34 smaller cores
  4. Only 14 processes ever exist at a time; the rest are swapped to disk
Show answer

Answer: A. Most are waiting; the scheduler time-slices too fast to notice

At any instant at most 14 threads truly execute. Nearly all processes are blocked in the waiting state, and the few runnable ones take rapid preempted turns — time slices separated by context switches — creating the illusion of simultaneity.

Q6. What is the practical difference between kill (SIGTERM) and kill -9 (SIGKILL)?

  1. SIGTERM works only on background jobs; SIGKILL works on any process
  2. SIGTERM can be caught and handled; SIGKILL cannot be refused
  3. SIGKILL politely asks the process to stop; SIGTERM forces it
  4. They are identical; -9 just makes it faster
Show answer

Answer: B. SIGTERM can be caught and handled; SIGKILL cannot be refused

SIGTERM (signal 15, the default) lets a process save files and shut down gracefully — a process killed by it exits with code 143. SIGKILL (signal 9) is enforced by the kernel, runs no cleanup, and yields exit code 137; use it only after SIGTERM fails.

Q7. Which statement correctly distinguishes concurrency from parallelism?

  1. Parallelism is possible on a single core; concurrency requires at least two cores
  2. Concurrency is the hardware feature; parallelism is the software feature
  3. Concurrency overlaps tasks on one core; parallelism needs several
  4. They are synonyms used by different operating systems
Show answer

Answer: C. Concurrency overlaps tasks on one core; parallelism needs several

A single core running three processes in time slices is concurrent but never parallel; simultaneous execution needs multiple cores (or a GPU's thousands). All parallelism on a shared machine also involves concurrency, but not vice versa.

Q8. Why do machine-learning frameworks load training data with several worker processes rather than threads in Python?

  1. Threads cannot read files on most operating systems
  2. Worker processes can share the GPU while threads cannot
  3. Processes are always faster than threads for every workload
  4. The GIL lets only one thread run Python, so parallelism needs processes
Show answer

Answer: D. The GIL lets only one thread run Python, so parallelism needs processes

Under the GIL, Python threads provide concurrency (overlapping waits) but not parallel execution of Python code. Spawning worker processes gives each its own interpreter and memory, so many cores can genuinely preprocess batches simultaneously to keep the GPU fed.

Glossary

process
A running instance of a program: the program's instructions loaded into private memory, plus the PID, file handles, owner, parent, and state the operating system tracks to run and clean up after it.
PID
Process identifier — the unique number the operating system assigns to each process, used to observe it (ps -p) and signal it (kill).
thread
An independent stream of instructions inside a process, with its own program counter and stack but sharing the process's memory and file handles with its sibling threads.
scheduler
The part of the operating system that decides which runnable thread gets which CPU core, and for how long, balancing responsiveness, throughput, and fairness.
context switch
The act of saving one thread's complete CPU state and loading another's so a core can change what it is running — pure overhead that pays for the illusion of simultaneity.
time slice
The brief interval, typically a few milliseconds, that the scheduler lets a thread run before preempting it and giving the core to another.
preemption
The scheduler forcibly interrupting a running thread when its time slice expires or something more urgent arrives — the thread is not asked, it is stopped.
signal
A small numbered notification the kernel delivers to a process, such as SIGTERM ("please shut down"), SIGINT (Ctrl+C), or SIGKILL (unrefusable termination).
exit code
The number a process leaves behind when it terminates: 0 means success, other values signal failure, and 128 plus a signal number marks death by that signal (143 for SIGTERM, 137 for SIGKILL).
zombie process
A process that has exited but whose exit code has not yet been collected by its parent, leaving a dead entry in the process table until it is reaped.
concurrency
Structuring many tasks so they make progress over overlapping time periods — achievable on a single core through time slicing, with no true simultaneity required.
parallelism
Executing more than one task at the same physical instant, which requires multiple execution units such as several CPU cores or a GPU's thousands of cores.
background job
A command started with & so the shell does not wait for it: the process runs while the prompt returns immediately, its PID captured in $! and listed by jobs.
race condition
A bug in which the result depends on the timing of threads touching shared data — for example two threads both incrementing a counter and silently losing one update.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.