Instance of executing portion of a program within a process
Purpose — Threads let us avoid incurring the overhead of creating and managing separate PCBs.
Structure
A thread is much lighter than a PCB and contains:
Thread ID
program counter
Register set
Stack
It shares the following with other threads in the same process.
Code section
Data section
Other OS resources (e.g., open files)
Example — A word processor may have a thread for getting keystrokes from the user, another for spellcheck, and another for graphics.
Example: Single v. Multithreaded Process
Key Difference: In the multithreaded process, the Code, Data, and Heap are shared, while each thread gets its own private Stack, Registers, and Program Counter (PC).
Motivation and Benefits
Motivation
Increase efficiency of system.
Process creation is heavy, thread creation is light.
Can simplify code.
Aside — Today, kernels and modern applications are generally multithreaded.
Benefits
Responsiveness: Especially important for UI.
Resource Sharing: Cheaper than shared memory or message passing.
Economy: Cheaper than process creation and context-switching.
Scalability: Can use multiprocessor architectures.
Example: Multithreaded Server Architecture
A server listens for requests.
When it gets a request, it makes a thread to service it.
The server can quickly resume listening for requests.
Concurrency and Parallelism
Concurrency: Supports more than one task making progress.
Parallelism: System can do more than one task simultaneously.
Types of Parallelism:
Data Parallelism: Splits data across multiple cores, doing same operation on each.
Example: Data Parallelism
You want to sum an array on a dual-core system.
You could have one core sum-up the first half, and the other core do the latter half.
Task Parallelism: Distribute threads across cores. Each thread can do unique operations on same or different data.
Challenges of Multicore Programming
Multicore or multiprocessor systems put pressure on programmers, such as:
Dividing activities: Finding ways to divide things into separate, concurrent tasks.
Balance: Ensuring tasks perform equal work of equal value.
Data splitting: Data being accessed and manipulated must be divided to run on separate cores.
Data dependency: If one task depends on another, their execution must be synchronized. (And, how do you know if you have such a dependency)
Testing and debugging: Testing and debugging concurrent programs is inherently more difficult.
Amdal’s Law
Identifies performance gains from adding additional cores to an application that has serial and parallel components.
\text{speedup} \le \frac{1}{
S + \frac{1-S}{N}
} \\~\\
\small
\textit{where $S$ is serial portion; $N$ is processing cores}
The serial portion has a disproportionate effect on performance.
Thread Models
User Threads v. Kernel Threads
User: Supported above the kernel, managed without kernel support.
Kernel: Managed directly by the kernel.
Aside — All modern operating systems support multithreading.
Multithreading Models
1. Many-to-One Model
What: Many user threads are mapped to one kernel thread
Issue: Few systems currently use this model, as one thread blocks the rest.
Examples — Solaris Green Threads Library
2. One-to-One Model
What: User threads are mapped directly kernel threads.
Benefit: More concurrency.
Issue: Number of threads per process sometimes restricted due to overhead.
Examples — Windows, Linux, and many programming languages.
3. Many-to-Many Model
What: Multiplexes many user-level threads to smaller or equal number of kernel threads.
Examples — Windows with ThreadFiber package
4. Two-Level Model
What: Like many-to-many except a user thread can also be bound to a kernel thread.
Use Case: e.g., I want one, dedicated and guaranteed thread
Examples — Tru64 UNIX
Thread Library
Provides programmer with API for creating and managing threads
Two Primary Implementation:
Entirely in userspace
Supported by kernel
Examples:
Windows: kernel-level
Java: typically kernel-level
Pthread: any
Java Threads
Managed by the JVM
You can create a thread by implementing the Runnable interface.
class Mytask extends SomeOtherClass implementsRunnable{// ...}