Operating Systems Level 4
PART 1 • EXECUTION FOUNDATIONS

Many Execution Paths inside One Process

See exactly what threads share, what each thread must keep independently, how user threads map to kernel schedulable entities, and why shared memory creates both speed and correctness risk.

Level 04 of 15 Intermediate 90–120 minutes Requires Process Model
BY THE END, YOU CAN

Reason about concurrent execution

  • Describe a thread without calling it a process.
  • Separate shared process resources from thread-private state.
  • Compare many-to-one, one-to-one and many-to-many.
  • Distinguish concurrency from parallelism.
  • Trace and explain a lost-update race.
01 • OPEN THE PROCESS

A Thread Is an Execution Unit inside a Process

Threads in one process share the resources that define the process, while each preserves the CPU state needed to execute independently.

PROCESS 4217 • SHARED ADDRESS SPACE
Code
Global data
Heap
Open files
Thread A PC + registers
private stack
thread ID
Thread B PC + registers
private stack
thread ID
Thread C PC + registers
private stack
thread ID
THE ESSENTIAL DIVISION

Share the process; preserve each execution path

If threads did not keep separate program counters, registers and stacks, one function call or local variable could overwrite another thread’s active execution context.

02 • CHOOSE THREADS FOR A REASON

Why Applications Use Multiple Threads

Responsiveness

A user interface can remain active while another thread waits for storage or network work.

Resource sharing

Threads naturally cooperate through one process address space and open-resource collection.

Economy

Creating and switching threads can require less work than separate processes because more context is shared.

Scalability

Independent work can execute simultaneously on multiple processor cores when the mapping permits it.

03 • INTERACTIVE ARCHITECTURE

Map User Threads to Kernel Scheduling Entities

Select a model to see its mapping, blocking behaviour and parallelism trade-off.

MANY USER THREADS → ONE KERNEL ENTITY

04 • INTERACTIVE SCHEDULING VIEW

Observe Concurrency and Parallelism on a Timeline

Run the same two threads on one logical CPU or two cores. The work is concurrent in both cases, but simultaneous only with two cores.

Ready to schedule

Advance the timeline to reveal one time slice at a time.

05 • USE PRECISE LANGUAGE

Concurrency Is Overlap; Parallelism Is Simultaneity

Concurrency

Multiple tasks make progress during overlapping periods. On one core, the OS can interleave them by switching between execution contexts.

A works B works A works

Parallelism

Multiple tasks execute at the same instant on distinct processing resources. Parallel work is concurrent, but concurrent work need not be parallel.

Core 1: A + Core 2: B
06 • INTERACTIVE RACE LAB

Make a Lost Update Visible

Two threads execute counter = counter + 1. That source statement contains a read, calculation and write; an unsafe interleaving can lose one update.

SHARED COUNTER 0

Thread A

read counter → localA
localA = localA + 1
write localA → counter

Thread B

read counter → localB
localB = localB + 1
write localB → counter
Initial state

Both increments are pending. Step through the selected schedule.

07 • DESIGN FOR CORRECTNESS

Common Multithreading Hazards

Hazard What goes wrong Direction of solution
Race condition The result depends on an uncontrolled interleaving of shared-data operations. Protect critical sections or remove shared mutable state.
Deadlock Threads wait forever for resources held in a cycle. Control acquisition order, timeout or avoid unsafe allocation.
Starvation A thread remains eligible but repeatedly fails to receive required service. Use fairer scheduling/locking policies or aging.
Livelock Threads keep reacting to each other but make no useful progress. Break symmetry using backoff or coordination.
Thread leak Threads are created or retained without proper completion and cleanup. Bound pools, define ownership and join/cancel safely.
08 • CHECK YOUR UNDERSTANDING

Ten Misconception-Specific Checks

Feedback explains why each selected option fits or fails.

Answered correctly: 0 of 10
09 • EXPLAIN & PREPARE

University and Placement Questions

2-MARK QUESTIONS
  1. Define a thread.
  2. What does a thread share?
  3. Define concurrency.
  4. What is a race condition?
  5. State one benefit of multithreading.
5-MARK QUESTIONS
  1. Compare process and thread.
  2. Explain multithreading models.
  3. Differentiate concurrency and parallelism.
  4. Trace a lost-update race.
  5. Discuss benefits and costs of threads.
INTERVIEW QUESTIONS
  1. Can threads run in parallel on one core?
  2. Why does each thread need a private stack?
  3. What happens if one user thread blocks in many-to-one?
  4. Why is a race difficult to reproduce?
  5. Thread versus asynchronous task?
Show a strong answer: “Process versus thread”
  1. Define the process as a protected resource container and execution environment.
  2. Define the thread as an execution path scheduled within that process.
  3. State shared items: address space, code, heap, globals and process resources.
  4. State private items: thread ID, program counter, registers and stack.
  5. Compare creation/switching cost, isolation, communication and failure impact.
  6. Conclude with when separate processes are preferable: stronger isolation and independent lifetime.
Show a strong answer: “Explain the lost update”

Both threads read the original value before either writes. Each computes the same incremented local value, then both write that value. The second write overwrites rather than adds to the first result, so two logical increments produce only one stored increment. Protect the entire read–modify–write sequence as one critical section or use a suitable atomic operation.

LEVEL 4 SUMMARY

You Can Now Explain Both the Power and Risk of Threads

  • Threads share their process’s address space and resources but keep independent execution contexts.
  • User-to-kernel mapping determines blocking and parallelism behaviour.
  • Concurrency means overlapping progress; parallelism means simultaneous execution.
  • A race emerges when a result depends on an uncontrolled ordering of shared-memory operations.
  • Correct synchronization protects an invariant without unnecessarily serializing independent work.
COURSE CHECKPOINT

Mark Level 4 after you can reproduce the lost update and compare all three main threading models.

Saved only in this browser.