Processes and threads — exam-style exercises with states to trace, fork() calls to count and threads that share almost everything (and the “Hello” that came out eight times)

My for loop with three fork() calls printed eight “Hello”s instead of four, and the lesson helped me more in the exam than in the lab: the IC2 test has questions about counting processes, about which state comes after which and about what a thread owns, and the trap is always the same as mine. You answer them well if you have the right picture in your head, not if you remember loose sentences.

This article is the practical side of the processes and threads theory: an interactive test with every exam format (single choice, multiple choice with penalty, true or false, gaps, matching, ordering, short answer and two calculation tables), four open questions with their model answer, the whole topic in one picture and the cheat sheet for the last review. The questions are original: they cover the same concepts as the exam, with other data and other wording.

How to use this article

  1. Do the test without looking at the theory. Each question has its own “Check” button: answer, check it and read the explanation, even when you get it right.
  2. Careful with the multiple-choice ones: they penalise like the exam. Each wrong option you tick subtracts, so only tick what you are sure about.
  3. For the two tables, grab some paper. In the states one, draw the five states and move A and B event by event; in the fork() one, draw the tree. Type the states with the exact word (new, ready, running, blocked or terminated; capitals do not matter).
  4. Write down what you get wrong and review it with the cheat sheet at the end. If you miss more than three in a row from the same block, go back to that section of the theory.

Test: processes, states, API and threads

There are 31 questions in the order of the topic: what a process is and its PCB (1-5), states (6-11), queues, schedulers and context switch (12-17), the process API (18-26) and threads (27-31).

Question 1Single choice

Which definition of process does the exam expect?

  1. A program in execution, with the resources the OS assigns to it
  2. An executable file stored on disk
  3. An interrupt service routine
  4. A kernel thread with no memory of its own
Solution

Answer: A program in execution, with the resources the OS assigns to it

A process is a program in execution: the code plus everything needed to run it (memory, registers, PC, open files and CPU time). The same program can give rise to several processes at once.

Question 2True or false

Mark each statement about processes as true or false.

  1. To run, a process needs main memory, CPU time and I/O devices.
  2. A new process is created by an interrupt service routine.
  3. Two processes can be running the same program at the same time.
  4. A program stored on disk is an active entity.
  5. Processes compete for CPU time and for memory areas.
Solution
  • T — To run, a process needs main memory, CPU time and I/O devices.
  • F — A new process is created by an interrupt service routine.
  • T — Two processes can be running the same program at the same time.
  • F — A program stored on disk is an active entity.
  • T — Processes compete for CPU time and for memory areas.

It needs main memory (RAM), not secondary memory: the disk stores the program, but to run it has to be loaded into RAM.

A process is created with a system call (fork or CreateProcess), not with an interrupt routine. And opening two terminals with bash gives two processes of the same program.

Question 3Match

Match each area of a process’s memory with what it holds.

text
data
heap
stack

Definitions:

  • The machine code of the program
  • Global and static variables
  • Memory requested with malloc
  • Local variables, parameters and return addresses
  • The CPU registers of each thread
Solution
  • text → The machine code of the program
  • data → Global and static variables
  • heap → Memory requested with malloc
  • stack → Local variables, parameters and return addresses

From bottom to top: text (code), data (globals), heap (dynamic memory, grows up) and stack (grows down). Registers are not in any memory area: they live in the CPU and, when the process leaves it, in its PCB.

Question 4Single choice

Which areas of a process’s memory are copied from the executable file when it is loaded?

  1. text and data
  2. heap and stack
  3. Only the stack
  4. All four
Solution

Answer: text and data

text and data come from the executable; heap and stack are created and grow while the process runs.

Question 5Multiple · wrong ticks subtract

What information does the PCB (process control block) hold? Tick all the correct ones.

  1. The state of the process
  2. The program counter
  3. A copy of the CPU registers
  4. Open files and I/O status
  5. The machine code of the program
  6. The interrupt vector table
  7. The local variables of its functions
Solution

Answer: The state of the process · The program counter · A copy of the CPU registers · Open files and I/O status

The PCB holds: state, PC, registers, scheduling information (priority, waiting time), memory, I/O and files, and accounting (CPU time used, PID). Careful: it is multiple choice with penalty, so ticking the code or the locals subtracts.

Question 6Match

Match each state with its description.

New
Ready
Running
Blocked
Terminated

Definitions:

  • It will not run any more and the OS takes its resources back
  • The OS has just created it and has not admitted it yet
  • It is on the CPU executing instructions
  • It has everything and only waits to be given the CPU
  • Stored on disk with its PCB deleted
  • It waits for an event to happen, such as the end of an I/O
Solution
  • New → The OS has just created it and has not admitted it yet
  • Ready → It has everything and only waits to be given the CPU
  • Running → It is on the CPU executing instructions
  • Blocked → It waits for an event to happen, such as the end of an I/O
  • Terminated → It will not run any more and the OS takes its resources back

The pair people mix up most is ready versus blocked: ready waits for the CPU; blocked waits for something else (the disk, the network, a child). Even if you gave the CPU to a blocked process, it could not move forward.

Question 7Single choice

gcc has asked the disk for the contents of a .c file and it has not arrived yet. What state is gcc in?

  1. Blocked
  2. Ready
  3. Running
  4. New
Solution

Answer: Blocked

Asking for I/O takes a process from running to blocked. When the disk finishes, an interrupt will move it to ready.

Question 8Single choice

The music player has its data in RAM and nothing to wait for, but another process is using the CPU. What state is it in?

  1. Ready
  2. Blocked
  3. Running
  4. Terminated
Solution

Answer: Ready

Ready = waiting to be assigned a processor. It is in the ready queue, and the dispatcher will pick it when its turn comes.

Question 9True or false

Transitions between states: mark whether each one exists in the five-state diagram.

  1. A blocked process goes straight to running when its I/O finishes.
  2. A running process goes back to ready when the timer fires.
  3. A ready process can become blocked without having run.
  4. A new process becomes ready when the OS admits it.
  5. A terminated process can go back to ready if the user asks for it.
Solution
  • F — A blocked process goes straight to running when its I/O finishes.
  • T — A running process goes back to ready when the timer fires.
  • F — A ready process can become blocked without having run.
  • T — A new process becomes ready when the OS admits it.
  • F — A terminated process can go back to ready if the user asks for it.

The six arrows are: new → ready, ready → running, running → ready, running → blocked, blocked → ready and running → terminated.

To block you have to ask for something (an I/O, a wait), and you can only do that by executing instructions. That is why blocked can only be reached from running, and blocked can only be left towards ready: after the I/O it goes back to the queue, not to the CPU.

Question 10Order

Put in order the life of a gcc process that is created, reads a file from disk and ends (with a single CPU that others use at times).

  1. The disk interrupt moves it back to ready
  2. bash calls fork(): the child is born in the new state
  3. The dispatcher gives it the CPU: it becomes running
  4. The OS admits it and puts it in the ready queue
  5. It gets back on the CPU and ends with exit(0)
  6. It asks the disk for the file: it becomes blocked
Solution

Correct order:

  1. bash calls fork(): the child is born in the new state
  2. The OS admits it and puts it in the ready queue
  3. The dispatcher gives it the CPU: it becomes running
  4. It asks the disk for the file: it becomes blocked
  5. The disk interrupt moves it back to ready
  6. It gets back on the CPU and ends with exit(0)

new → ready → running → blocked → ready → running → terminated. Look at the fifth step: after the I/O it does not go straight back to the CPU, but to the ready queue.

Question 11Calculation

Two processes, A and B, share one CPU. At the start, A is running and B is ready. Write the state of each one after each event (new, ready, running, blocked or terminated).

StepEventState of AState of B
1A asks the disk for a block
2The dispatcher picks B
3A’s disk interrupt arrives
4The timer throws B out
5The dispatcher picks A
6A calls exit(0)
Solution
StepEventState of AState of B
1A asks the disk for a blockblockedready
2The dispatcher picks Bblockedrunning
3A’s disk interrupt arrivesreadyrunning
4The timer throws B outreadyready
5The dispatcher picks Arunningready
6A calls exit(0)terminatedready

Step 1: asking for I/O takes A from running to blocked; B stays ready because nobody has picked it yet (that is step 2).

Step 3: the interrupt only wakes A (to ready); B stays on the CPU. Step 4: the timer moves B to ready, and at that moment both are in the queue. In each row there is at most one running process: there is only one CPU.

Question 12Fill in the gaps

Complete the text about process queues.

The OS organises the ____ into queues waiting for the ____ or for the I/O devices. Queues are implemented with linked ____, tables or trees, and the ____ inserts and removes processes from each queue according to its policy.

Words:

  • lists
  • processor
  • stacks
  • scheduler
  • registers
  • PCBs
  • compiler
Solution

The OS organises the PCBs into queues waiting for the processor or for the I/O devices. Queues are implemented with linked lists, tables or trees, and the scheduler inserts and removes processes from each queue according to its policy.

What moves from queue to queue is not the whole process but its PCB. Each resource (the CPU, each disk) has its queue, and the scheduling policy decides the order.

Question 13Match

Match each scheduler with its job.

Short-term (STS)
Long-term (LTS)
Medium-term (MTS)

Definitions:

  • Shares the disk blocks out among the files
  • If RAM is saturated, moves blocked processes to disk
  • Decides which jobs enter the ready queue
  • Gives and takes the CPU to and from ready processes
Solution
  • Short-term (STS) → Gives and takes the CPU to and from ready processes
  • Long-term (LTS) → Decides which jobs enter the ready queue
  • Medium-term (MTS) → If RAM is saturated, moves blocked processes to disk

The STS works all the time; the LTS, now and then (in batch systems or server farms) to get a good mix of jobs; the MTS does swapping when memory runs short.

Question 14Single choice

Which scheduler runs most often?

  1. The short-term one
  2. The long-term one
  3. The medium-term one
  4. All three equally often
Solution

Answer: The short-term one

Short, medium and long refer to how often they act: the short-term one, constantly.

Question 15Short answer

What is the technique of moving blocked processes to disk to free main memory called? (one word)

Solution

Answer: swapping

Swapping, and it is done by the medium-term scheduler.

Question 16True or false

About the context switch, mark true or false.

  1. It consists of taking a process off the CPU and resuming another.
  2. The state of the outgoing process is saved in its PCB.
  3. It is done in user mode so that it is faster.
  4. While it lasts, no user process moves forward.
  5. The context of the evicted process is saved in secondary memory.
Solution
  • T — It consists of taking a process off the CPU and resuming another.
  • T — The state of the outgoing process is saved in its PCB.
  • F — It is done in user mode so that it is faster.
  • T — While it lasts, no user process moves forward.
  • F — The context of the evicted process is saved in secondary memory.

It is OS work, so it is done in kernel mode; the context goes into the PCB, which is in main memory. It is wasted time, which is why the hardware offers instructions that save and load registers in one go (PUSHA/POPA on x86, LDM/STM on ARM).

Question 17Short answer

What is the OS module that performs the context switch called?

Solution

Answer: dispatcher

The dispatcher. Do not confuse it with the scheduler: the scheduler decides who goes on the CPU; the dispatcher performs the switch.

Question 18Single choice

On Unix, what uniquely identifies each process?

  1. Its PID
  2. The name of its executable
  3. The address of its stack
  4. The name of the user who starts it
Solution

Answer: Its PID

Each process has a unique PID, and since each one is created by another (its parent), they form a process tree with systemd (PID 1) at the root.

Question 19True or false

Right after a fork(), mark true or false.

  1. Parent and child run the same code.
  2. Parent and child use the same memory area: if the child changes a variable, the parent sees the change.
  3. The files the parent had open are shared with the child.
  4. fork() returns 0 in the parent.
  5. Every process is created by typing a command in the terminal.
Solution
  • T — Parent and child run the same code.
  • F — Parent and child use the same memory area: if the child changes a variable, the parent sees the change.
  • T — The files the parent had open are shared with the child.
  • F — fork() returns 0 in the parent.
  • F — Every process is created by typing a command in the terminal.

The child is a duplicate of the parent: same code, but its own copy of memory. If the child does counter++, the parent keeps its value. Open files are shared.

fork() returns 0 to the child and the child’s PID to the parent. And not everything is born in the terminal: any process can create others with a system call (systemd creates the services at boot).

Question 20Match

Match each call with what it does.

fork()
exec()
wait()
exit()
kill()
CreateProcess()

Definitions:

  • Aborts another process
  • Ends the process that calls it
  • Replaces the code of the process with another executable
  • Creates a child that duplicates the parent
  • Waits for a child to finish
  • Changes the priority of a process
  • Creates a process on Windows naming its executable
Solution
  • fork() → Creates a child that duplicates the parent
  • exec() → Replaces the code of the process with another executable
  • wait() → Waits for a child to finish
  • exit() → Ends the process that calls it
  • kill() → Aborts another process
  • CreateProcess() → Creates a process on Windows naming its executable

On Unix, creating a process that runs another program takes two steps: fork() (duplicate) and exec() (change the code). On Windows, CreateProcess() does it in one, naming the executable.

Question 21Single choice

The child of bash (PID 3151) calls exec() to run gcc. How many new processes are created?

  1. None: process 3151 is still the same one, with the code of gcc
  2. One, with a new PID
  3. Two: one for bash and one for gcc
  4. One, but with the same PID as bash (3150)
Solution

Answer: None: process 3151 is still the same one, with the code of gcc

exec() keeps the PID, the PCB and the open files, and replaces the code and data with those of the new program. If exec() succeeds, it never returns: the code that called it no longer exists.

Question 22Calculation

For each C fragment, run by a single process, write how many processes there are at the end (counting the original) and how many times “x” is printed. Assume fork() never fails.

FragmentProcesses in totalTimes x is printed
fork(); fork(); printf("x");
for (i = 0; i < 4; i++) fork(); printf("x");
if (fork() == 0) fork(); printf("x");
fork(); if (fork() == 0) exit(0); printf("x");
Solution
FragmentProcesses in totalTimes x is printed
fork(); fork(); printf("x");44
for (i = 0; i < 4; i++) fork(); printf("x");1616
if (fork() == 0) fork(); printf("x");33
fork(); if (fork() == 0) exit(0); printf("x");42

Row 1: the first fork() leaves 2 processes and both make the second one: 2 × 2 = 4. Row 2: each round doubles everyone, 2⁴ = 16, and all 16 print.

Row 3: only the child (to which fork() returns 0) makes the second fork(): original + child + grandchild = 3. Row 4: after the first fork() there are 2 processes; each one creates a child with the second fork() (that makes 4), but those two children call exit(0) before the printf: only the first 2 print.

Question 23Single choice

What does fork() return in the parent process when everything goes well?

  1. The PID of the child it has just created
  2. 0
  3. -1
  4. Its own PID
Solution

Answer: The PID of the child it has just created

One fork(), two returns: the parent gets the child’s PID and the child gets 0. That is why the usual if (pid == 0) separates what each one does.

Question 24True or false

About how a process ends, mark true or false.

  1. A process ends when it calls exit().
  2. A process can only end if another one sends it kill().
  3. If it causes an exception, the OS may decide to abort it.
  4. A child ends the moment its parent calls wait().
  5. A zombie process keeps executing instructions forever.
Solution
  • T — A process ends when it calls exit().
  • F — A process can only end if another one sends it kill().
  • T — If it causes an exception, the OS may decide to abort it.
  • F — A child ends the moment its parent calls wait().
  • F — A zombie process keeps executing instructions forever.

It ends with exit(), because another process aborts it with kill(), or because the OS aborts it after an exception. wait() kills nobody: the parent waits for the child to finish.

A zombie has already ended: it executes nothing, it only keeps its entry (PID and exit status) until the parent collects it with wait().

Question 25Multiple · wrong ticks subtract

What are the two models of inter-process communication (IPC)?

  1. Shared memory
  2. Message passing
  3. Interrupts
  4. Context switch
  5. Long-term scheduling
Solution

Answer: Shared memory · Message passing

Two models: shared memory and message passing. The specific mechanisms (pipes, sockets, RPC…) are ways of implementing them.

Question 26Fill in the gaps

Complete the classic IPC mechanisms.

Two processes can communicate through ____, ____, shared memory areas, remote procedure calls (RPC) or, the least efficient way, ordinary ____.

Words:

  • PCBs
  • files
  • interrupts
  • pipes
  • registers
  • sockets
Solution

Two processes can communicate through pipes, sockets, shared memory areas, remote procedure calls (RPC) or, the least efficient way, ordinary files.

Files work, but every exchange goes through the disk: that is why they are the slowest mechanism. Pipes and sockets are message passing.

Question 27True or false

What does each thread own? Mark T if it belongs to the thread and F if it is shared with the other threads of its process.

  1. The program counter
  2. The stack (and its stack pointer)
  3. The CPU registers
  4. The global variables
  5. The open files
  6. The code of the program
Solution
  • T — The program counter
  • T — The stack (and its stack pointer)
  • T — The CPU registers
  • F — The global variables
  • F — The open files
  • F — The code of the program

What a thread owns is what it needs to execute on its own: PC, registers, stack and its CPU time. Code, data, files and I/O resources belong to the process and are shared.

Question 28Multiple · wrong ticks subtract

What advantages do threads have over creating several processes? Tick all the correct ones.

  1. Creating a thread costs less than creating a process
  2. They communicate more easily, because they share memory
  3. They achieve more concurrency with fewer resources
  4. Switching between threads of the same process can be faster
  5. Each thread has its own copy of the global variables
  6. If a thread fails, the other threads of the process are never affected
Solution

Answer: Creating a thread costs less than creating a process · They communicate more easily, because they share memory · They achieve more concurrency with fewer resources · Switching between threads of the same process can be faster

The first four are the benefits in the notes. Precisely because they share memory, creating threads is cheaper than creating processes, not the other way round.

Question 29Match

Match each multithreading model with its description.

Many-to-one
One-to-one
Many-to-many
Two-level

Definitions:

  • Many user threads spread over several kernel threads
  • Each user thread has its own kernel thread; the one used by Linux and Windows
  • Like many-to-many, but allows binding a user thread to a kernel thread
  • Each kernel thread runs several heavyweight processes at once
  • Many user threads on a single kernel thread; if one blocks, they all block
Solution
  • Many-to-one → Many user threads on a single kernel thread; if one blocks, they all block
  • One-to-one → Each user thread has its own kernel thread; the one used by Linux and Windows
  • Many-to-many → Many user threads spread over several kernel threads
  • Two-level → Like many-to-many, but allows binding a user thread to a kernel thread

Many-to-one is the oldest (Solaris Green Threads, GNU Portable Threads) and is hardly used: it cannot take advantage of several cores. One-to-one gives more concurrency, at the cost of sometimes limiting the number of threads.

Question 30Single choice

On a system with user threads (managed by a library, unknown to the kernel), one of the threads does a disk read and blocks. What happens?

  1. The whole heavyweight process blocks
  2. Only that thread blocks; the others carry on
  3. The OS creates another kernel thread for the rest
  4. The process ends with an error
Solution

Answer: The whole heavyweight process blocks

With user threads, the kernel does not know the process has internal concurrency: if one thread blocks, everything blocks. That is why kernel threads are the norm today.

Question 31Short answer

Because a thread shares the memory and resources of its process, it is also called a … process (one word)

Solution

Answer: lightweight

Lightweight process, as opposed to the heavyweight process, which is the whole application with one or more threads.

In the questions about counting processes, do not try to guess with formulas. Draw the tree: each fork() that runs adds one process, and the trick is to see who reaches each fork(). If there is an if (fork() == 0), only the child goes in; if there is an exit(), that process never reaches the printf.

Open questions

The written part of the IC2 exam has questions you explain in your own words. Write your answer in the box before opening the model answer and compare it with the list of “what the marker looks for”.

Question 1Open answer

Explain the difference between a program and a process. What areas does a process’s memory have and what information does its PCB hold?

Solution

Model answer:

A program is an executable file on disk: a passive entity. A process is a program in execution: an active entity that the OS creates with a system call and to which it assigns resources (main memory, CPU, I/O). The same program can give rise to several processes.

A process’s memory has four areas: text (code), data (global variables), heap (dynamic memory, growing up) and stack (locals, parameters and return addresses, growing down). text and data are copied from the executable.

The PCB is the OS structure with its state, program counter, registers, scheduling information, memory, I/O and files, and accounting (PID, CPU time). Thanks to it the OS can take the process off the CPU and resume it later.

What the marker looks for:

  • Passive program versus active process (program in execution)
  • The four memory areas with their contents and what is copied from the executable
  • At least five fields of the PCB
  • What the PCB is for (context switch)

If you are short of time, prioritise the definition and the PCB fields: they are what gets asked most.

Question 2Open answer

Draw or describe the five-state diagram of a process with its transitions and explain why there is no transition from blocked to running.

Solution

Model answer:

States: new, ready, running, blocked and terminated. Transitions: new → ready (the OS admits it); ready → running (the dispatcher picks it); running → ready (an interrupt, for example the timer); running → blocked (it asks for I/O or waits for an event); blocked → ready (the I/O ends or the event happens); running → terminated (exit or abort).

There is no blocked → running because when the event arrives another process may be using the CPU, and whoever decides who uses it is the short-term scheduler, which chooses among the ready processes. The woken process enters the ready queue and waits for its turn like the others.

What the marker looks for:

  • The five states with their meaning
  • The six transitions with their cause
  • Difference between ready (waits for the CPU) and blocked (waits for an event)
  • Justification: on waking it goes back to the ready queue and the scheduler decides

A clean drawing with labelled arrows is often worth more than a long paragraph.

Question 3Open answer

You type gcc lab.c -o lab in a terminal running bash. Explain which processes take part and what fork(), exec(), exit() and wait() do until the prompt comes back.

Solution

Model answer:

bash calls fork(): the OS creates a child, a duplicate of bash, with a new PID. fork() returns the child’s PID to bash and 0 to the child. The child has the same code and its own copy of memory, and shares the open files (the terminal).

The child calls exec() with gcc: its code and data are replaced with those of gcc, but it is still the same process, with the same PID. Meanwhile, bash calls wait() and becomes blocked.

When gcc finishes compiling, it calls exit(0). It stays a zombie (it no longer runs, it only keeps its exit status) until bash’s wait() collects it. Then bash becomes ready, gets back on the CPU and shows the prompt.

What the marker looks for:

  • fork() duplicates and returns different values to parent and child
  • exec() changes the code without creating a process or changing the PID
  • wait() blocks the parent until the child ends
  • exit() ends the child and the parent collects its status
  • What is copied (memory) and what is shared (open files)

It is the sequence behind every command you type in the terminal; telling it with a concrete example makes it much clearer.

Question 4Open answer

Compare processes and threads: what a thread shares with the other threads of its process, what it owns, what advantages it brings and what problem user threads have in the many-to-one model.

Solution

Model answer:

A thread (lightweight process) is the basic unit of execution inside a process. Each thread has its own program counter, registers, stack and CPU time. It shares with the other threads the code, the data (global variables, heap) and the OS resources (open files, I/O).

Advantages: they are created faster than processes, they communicate easily because they share memory, they give more concurrency with fewer resources and switching between threads can be cheaper (no need to flush caches or change page tables).

User threads are managed by a library and the kernel does not know about them. In the many-to-one model, they all run on a single kernel thread: if one makes a blocking call the whole process blocks, and they cannot run in parallel on several cores. That is why the one-to-one model (Linux, Windows) is the most used today.

What the marker looks for:

  • What the thread owns (PC, registers, stack) and what is shared (code, data, files)
  • At least three advantages over processes
  • User threads versus kernel threads
  • The blocking problem in many-to-one and parallelism

A sketch of a process with three threads (like the spotify one in the theory article) sums up half the answer.

In the fork() and exec() question, a concrete example is gold. “bash (PID 3150) calls fork(), child 3151 calls exec() on gcc…” shows you understand it better than three definitions in a row.

The whole topic in one picture

Before the cheat sheet, the whole map. If you can explain every line of the six boxes out loud, the test for this topic is done:

The whole topic in one pictureMap of the topic in six boxes: process, states, scheduling and context, process API, threads and threading models, each with its three key ideas.Processa program in execution (active)text · data · heap · stackPCB: its full recordStatesnew → ready ⇄ runningrunning → blocked → readyrunning → terminatedScheduling and contextqueues of PCBs (lists, tables, trees)STS · LTS · MTS (swapping)dispatcher: saves and loads PCBsProcess APIfork() · exec() · wait()exit() · kill() · CreateProcess()IPC: shared memory or messagesThreadsown: PC, registers, stack, CPUshared: code, data, filescheaper to create and to talkThreading modelsmany-to-one · one-to-onemany-to-many · two-leveluser versus kernel threads
Six boxes, eighteen ideas: if you can explain every line, the test for this topic is done.

Cheat sheet: processes and threads

The essentials in 30 seconds

  • Process = program in execution (active). Program = file (passive). It is created with a system call.
  • Memory: text (code) and data (globals) come from the executable; heap (malloc) and stack (locals) are born at run time.
  • PCB: state, PC, registers, scheduling, memory, I/O and files, accounting. Never the locals or the code.
  • States: new → ready ⇄ running → terminated; running → blocked → ready. Never blocked → running.
  • Context switch: the dispatcher, in kernel mode, saves into the PCB; it is wasted time.
  • fork duplicates (0 to the child, the child’s PID to the parent); exec changes the code without creating a process; wait waits; exit ends.
  • Thread: owns its PC, registers and stack; shares code, data and files.

I want to know… / Answer

I want to know…Answer
What a ready process waits forThe CPU
What a blocked process waits forAn event: the end of an I/O, a child…
Where a process goes when its I/O endsTo ready (not to running)
Who decides which process uses the CPUThe short-term scheduler (STS)
Who performs the context switchThe dispatcher, in kernel mode
Which scheduler does swappingThe medium-term one (MTS)
What fork() returns to the parent / the childThe child’s PID / 0 (and -1 if it fails)
How many processes exec() createsNone
What fork() copiesMemory (each one gets its own)
What fork() sharesThe code and the open files
What a zombie isA finished child whose parent has not called wait() yet
The two IPC modelsShared memory and message passing
The threading model of Linux and WindowsOne-to-one

Templates for counting processes

FragmentProcesses in totalWhy
fork(); repeated n times in a row2ⁿEvery fork() is run by everyone alive
for (i = 0; i < n; i++) fork();2ⁿ (2ⁿ − 1 children)Same as n fork() calls in a row
if (fork() == 0) fork();3Only the child makes the second one
if (fork() > 0) fork();3Only the parent makes the second one
fork(); if (fork() == 0) exit(0);4, but only 2 stay aliveThe two grandchildren end as soon as they are born

States: from where to where

StateReached from…Left towards…
New(created by fork())ready
Readynew, running, blockedrunning
Runningreadyready, blocked, terminated
Blockedrunningready
Terminatedrunning(the OS takes its resources back)

If the question says… / Think of…

If the question says…Think of…
“secondary memory” to runFalse: main memory
“created by an interrupt service routine”False: by a system call
“the context switch is done in user mode”False: kernel mode
“the module is called the switcher”False: the dispatcher
“creating threads costs more than creating processes”False: the other way round
“the child ends when the parent calls wait()”False: wait() only waits
“a thread has its own global variables”False: it shares them
“with user threads, if a thread blocks…”The whole heavyweight process blocks

Before you hand it in, ask yourself

  1. Have I told ready (waits for the CPU) and blocked (waits for an event) apart correctly?
  2. In the counting questions, have I drawn the tree and checked who reaches each fork()?
  3. In the multiple-choice questions on the PCB and threads, have I ticked only what is really owned?
  4. Have I read carefully whether the sentence compares processes with threads or threads with processes?

If you want to review something, go back to the processes and threads theory, and if you fancy seeing it work in your terminal, the Processes in C exercises have real fork(), exec() and wait(). The next topic is CPU scheduling: what criterion the scheduler uses to decide whose turn it is and how to draw it in a Gantt chart.

Keep goingAll Operating systems articles

Did this article help you? Here is how you can support SergioLearns →

Ready for the exam?IC2 theory mock exam — first midterm, second midterm or the full exam, with your real mark and a review of every mistake (as if you were in the exam room)