>_
EngineeringNotes
← Back to OS Internals
Chapter 08

Coordination & Synchronization

Managing concurrent execution and maintaining data consistency across shared resources.

01

Concurrency Definitions

"It is the execution of the multiple instruction sequences at the same time. It happens in the operating system when there are several process threads running in parallel."

Thread Characteristics
  • Single sequence stream within a process.
  • An independent path of execution in a process.
  • Light weight process.
  • Used to achieve parallelism by dividing a process's tasks which are independent path of execution.

"(All are the part of one processor memory & all share same resources for their execution)."

Thread Scheduling

"Thread are scheduled for execution based on their priority."

"Even though threads are executing during runtime, all threads are assigned processor time slices by operating system."

TCB
Every thread has its TCB (Thread Control Block) similar to PCB.

Thread Context Switching

  • "OS saves current state of thread & switches to another thread of same process."

  • "Doesn't includes switching of memory address space. (But PC, register & stacks are include)."

Speed
Fast switching as compared to process switching
CPU Cache state preservation
CPU Access Mechanism
  • Each thread has its own Program Counter (PC).
  • OS schedules threads based on internal algorithms.
  • Instructions are fetched from thread-specific PC.
Switching Logic

"I/O or TQ based context switching is done here as well → for this we have TCB (to store state management)."

02

Single CPU Execution

Q

Will Single CPU system gain by multi-threading technique?

NEVER

"As two threads have to context switch for the single CPU. This won't give any gain."

Efficiency Result: 0% gain due to switching overhead

Process vs Thread Memory Layout

Process Memory
Stack (User)
Heap
Data
Source Code (Text)
Threaded Memory
Stack 1
Stack 2
Shared Heap
Shared Data
Shared Text

"* One stack is created for each active thread."

Benefits of Multi-Threading
Responsiveness

Performance boost for interactive applications by running non-blocking tasks.

Resource sharing

Efficient allocation as threads share the same address space.

Economy

Cheaper and faster context switching compared to separate processes.

Scaling

Optimal utilization of modern multiprocessor architectures.