OS Week 6 Threads
OS Week 6 Threads
Operating
systems
Threads vs Process
Threads
•Unlike a process, threads within the same process share resources like code,
data, and files but have their own registers and stack.
1.Program Counter
2.Register Set
3.Stack space
Benefits of a thread
•Only one thread executes at a given moment, but the switching creates the
illusion of simultaneous execution.
•Task Parallelism: Different tasks run in parallel (e.g., web browser rendering a
page while downloading files).
Data Parallelism
•In data parallelism, the same operation is performed on different chunks of
data simultaneously.
•The workload is divided across multiple processors, but each processor performs
the same task on different subsets of the data.
• Each processor applies the filter to its assigned portion at the same
time.
Task Parallelism
•In task parallelism, different tasks (or functions) are executed simultaneously
on the same or different data.
•This is useful when a problem can be broken down into multiple subtasks
that can be performed independently.
Task Parallelism Example
Consider a web browser where different tasks are handled in parallel:
•Many-to-One: Multiple user threads map to one kernel thread (not scalable).
•One-to-One: Each user thread maps to a kernel thread (better but more
resource-intensive).
(best balance).
Many to one
Many user-level threads mapped to single kernel thread
Examples:
•One-to-One does not use multiplexing since each thread has its own kernel
•thread.
Two-level model
Formula:
here:
w
• P = Fraction of the program that can be parallelized
• (1 - P) = Fraction that remains sequential
• N = Number of processors
Amdahl’s law Example
•At 4 lanes, traffic moves faster, but the toll booth still slows everyone down.
•At 64 lanes, most of the highway is ultra-fast, but cars still queue up at the bottleneck.
•With infinite lanes, the maximum speedup is only 10×, because 10% of the road (serial part) still limits the overall speed.