T TestsWorld
← All topic guides

Processes and Threads: formulas, shortcuts & solved examples

What it is

A process is a program in execution with its own address space, open files and resources. A thread is a lightweight unit of execution inside a process; threads of the same process share code, data and heap but each has its own stack, registers and program counter. Operating systems questions in placement tests focus on states, fork(), synchronisation and the process-versus-thread comparison.

Key rules

Process states

New → Ready → Running → (Waiting/Blocked) → Ready → ... → Terminated.

  • Running → Ready happens on a timer interrupt (preemption).
  • Running → Waiting happens on an I/O request or a wait on a semaphore.
  • Waiting → Ready happens when the I/O completes.

The PCB (Process Control Block) stores the PID, state, program counter, registers, memory limits and open files. A context switch saves one PCB and loads another; it is pure overhead.

Process versus thread

Aspect Process Thread
Memory Separate address space Shared with sibling threads
Creation cost Higher Lower
Communication IPC (pipes, sockets, shared memory) Shared variables
Crash impact Isolated Can bring down the whole process

fork() rules

  • fork() returns 0 in the child, the child's PID (positive) in the parent, and −1 on failure.
  • n unconditional fork() calls in sequence produce 2ⁿ processes, that is 2ⁿ − 1 new children.

Synchronisation

  • Critical section requirements: mutual exclusion, progress, bounded waiting.
  • Semaphore S: wait(S) decrements; if the value becomes negative the caller blocks. signal(S) increments and wakes one waiting process. A negative value −k means k processes are waiting.
  • Deadlock needs all four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait.

Shortcuts

  1. Counting prints after forks. Every print statement executes once per process alive at that point. A print after k forks runs 2ᵏ times.
  2. Conditional fork. In if (fork() == 0) fork(); only the child forks again, so you get 3 processes, not 4.
  3. Semaphore arithmetic. Final value = initial − (number of waits) + (number of signals). Read off blocked processes from how negative it is.

Solved examples

Example 1: Three forks

fork();
fork();
fork();
printf("x\n");

Solution. Each fork doubles the number of processes: 1 → 2 → 4 → 8. The print runs in all 8 processes, so "x" appears 8 times, and 7 child processes were created.

Example 2: Print between forks

fork();
printf("A\n");
fork();
printf("B\n");

Solution. After the first fork there are 2 processes, so "A" prints twice. After the second fork there are 4, so "B" prints 4 times. Total lines printed: 6 (2 + 4), using the "print after k forks runs 2ᵏ times" rule.

Example 3: Race condition on a shared counter

Two threads each execute count = count + 1 once on a shared variable starting at 0, with no lock. The statement compiles to load, add, store.

Solution. If one thread completes all three steps before the other starts, count = 2. But if both load 0 before either stores, both store 1, and count = 1. So the possible final values are 1 and 2. A mutex around the increment, or an atomic increment instruction, guarantees 2.

Example 4: Semaphore value

A counting semaphore guarding 3 printers in a college lab starts at S = 3. Five students call wait(S), then two call signal(S).

Solution. After 5 waits: 3 − 5 = −2, so 3 students got printers and 2 are blocked. After 2 signals: −2 + 2 = 0. Both blocked students have now been woken, and no one is waiting.

Common mistakes

  1. Thinking threads share stacks. Threads share heap and globals, but each has its own stack; local variables are private.
  2. Writing 2ⁿ as the number of children. 2ⁿ is the total number of processes including the original; children are 2ⁿ − 1.
  3. Assuming fork() copies threads. On POSIX systems only the calling thread is duplicated in the child.
  4. Confusing a binary semaphore with a mutex in every respect. A mutex has ownership: only the thread that locked it should unlock it.
  5. Believing a deadlock needs only a cycle. Circular wait is one of four conditions; breaking any one of them, such as imposing a global lock order, prevents deadlock.

Practice now: https://www.testsworld.com/mocks

Ready to practise Processes and Threads?
Take a free mock test

Related guides