Process Management: Threads

Thread

Thread: Basic unit of CPU utilization.

Purpose: Threads let us avoid incurring the overhead of creating and managing separate PCBs.

Structure:

Example: A word processor may have a thread for getting keystrokes from the user, another for spellcheck, and another for graphics.

TODO Diagram of single and multithreaded process.

Motivation

Today: Jernels and moden applications are generall multithreaded.

Benefits

Multithreaded Server Architecture

  1. A server listens for requests.
  2. When it gets a request, it makes a thread to service it.
  3. The server then resumes listening for requests.

Multiprogramming

Multicore systems put pressure on programmars.

Challenges include:

Concurrency v. Parallelism

Parallelism: System can do more than one task simultaneously.

Concurrency: Supports more than one task making progress.

Types of Parallelism:

Data Parallelism: Split data across multiple cores, 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.

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.

User and Kernel Threads

User: Supported above the kernel, managed without kernel support

Kernel: Managed directly by the Kernel

All modern operating systems support multithreading.

Multithreading Models

  1. Many-to-One: Many user threads and mapped to one kernel thread
  2. One-to-One: User threads are mapped directly kernel threads.
  3. Many-to-Many:

Many to One

Few systems currently use this model, as one thread blocks the rest.

Examples: Solaris Green Threads Library

One to One

More concurrency. Number of threads per process sometimes restricted due to overhead.

Examples: Windows, Linux

Many to Many

Multiplexes many user-level threads to smaller or equal number of kernel threads.

Examples: Windows with ThreadFiber package

Two-Level

Like many-to-many except a user thread can also be bound to a kernel thread.

Examples: Tru64 UNIX

Thread Library

Provides programmer with API for creating and managing threads

Two Primary Implementation:

  1. Entirely in userspace
  2. Supported by kernel

Examples:

Java Threads

Managed by the JVM

You can create a thread by implementing the Runnable interface.

class Mytask extends SomeOtherClass implements Runnable {
    // ...
}

Note: Don’t extend the Thread class, you’ll lock-out extending anything else!