Chapter 4: Threads

CS330 - Operating Systems

Multithreading Models, Benefits, and Implementation

4.1 Thread Overview

A thread is a basic unit of CPU utilization. It is also called a lightweight process. A thread comprises a thread ID, program counter, register set, and stack.

Thread Components

Each thread has its own:

  • Thread ID
  • Program counter
  • Register set
  • Stack space

Threads share with other threads:

  • Code section
  • Data section
  • Operating system resources (open files, signals)

Process Types

Type Description Characteristics
Heavyweight Process Traditional process with single thread One thread of control
Lightweight Process Another name for thread Basic unit of CPU utilization
Multithreaded Process Process with multiple threads Multiple threads share process resources

📊 Single vs Multithreaded Process

[Insert diagram showing single-threaded vs multithreaded process structure]

4.2 Single-threaded vs Multithreaded Processes

Single-threaded Process
Shared Resources:
  • Code
  • Data
  • Files
Per-Process:
  • Registers
  • Stack
  • Program Counter
Multithreaded Process
Shared (among threads):
  • Code
  • Data
  • Files
Per-Thread:
  • Registers
  • Stack
  • Program Counter

⚠️ Important:

Threads within the same process share memory and resources, which makes communication between threads efficient but requires careful synchronization to avoid race conditions.

4.3 Benefits of Multithreading

📱 Responsiveness

Allows a program to continue running even if part is blocked. Example: Web browser can allow user interaction in one thread while loading images in another.

🔄 Resource Sharing

Threads share memory and resources of the process they belong to. Easier than shared memory or message passing between processes.

💰 Economy

Thread creation is faster than process creation. Context switching between threads has lower overhead. In Solaris, creating process is 30 times slower than thread.

⚡ Scalability

Can utilize multiprocessor architectures. Multiple threads can run in parallel on different CPUs. Single-threaded process can run only on one CPU.

📝 Exam Question (CS330):

Q: List the four major benefits of multithreaded programming.

Answer:

  1. Responsiveness: May allow continued execution if part of process is blocked
  2. Resource Sharing: Threads share resources of process, easier than shared memory
  3. Economy: Cheaper than process creation, lower overhead for context switching
  4. Scalability: Process can take advantage of multiprocessor architectures

4.4 Multicore Programming

Concurrency vs Parallelism

Single-Core System (Concurrency)

Single Core
T₁
T₂
T₃
T₄
T₁
T₂
T₃
T₄

Threads are interleaved over time

Multi-Core System (Parallelism)

Core 1
T₁
T₃
T₁
T₃
Core 2
T₂
T₄
T₂
T₄

Threads run simultaneously on different cores

Aspect Concurrency Parallelism
Definition Multiple tasks make progress Multiple tasks execute simultaneously
Hardware Can work on single core Requires multiple cores
Execution Interleaved execution Simultaneous execution

Types of Parallelism

Data Parallelism

Distributes subsets of the same data across multiple cores, same operation on each.

Example: Summing array elements - divide array into chunks, sum each chunk in parallel.

Task Parallelism

Distributing threads across cores, each thread performing unique operation.

Example: Web server - one thread handles requests, another processes data, another updates logs.

4.5 User Threads and Kernel Threads

User Threads

Threads managed by user-level threads library, not in the kernel. Scheduled by the thread library.

Advantages Disadvantages
  • Fast to create and manage
  • No kernel intervention needed
  • Thread switching doesn't require kernel mode
  • Blocking system call blocks entire process
  • Cannot take advantage of multiprocessing
  • No true parallelism

Examples: POSIX Pthreads, Java threads, Win32 threads

Kernel Threads

All thread management done by kernel itself. Kernel does creation, scheduling and management.

Advantages Disadvantages
  • If one thread blocks, kernel can schedule another
  • Can run threads on different processors
  • True parallelism on multiprocessor systems
  • Slower to create and manage
  • Requires kernel intervention
  • More overhead for thread operations

Examples: Windows, Linux, Mac OS X, iOS, Android

📝 Practice Question:

Q: One disadvantage of User-Level Threads (ULTs) compared to Kernel-Level Threads (KLTs) is:

  • a) Scheduling is application specific
  • b) When a ULT executes a system call, all threads in the process are blocked
  • c) Thread switching does not require kernel mode privileges
  • d) All of the above

4.6 Multithreading Models

1. Many-to-One Model

User Space
Kernel Space

Characteristics:

  • Many user threads mapped to single kernel thread
  • Thread management done in user space - efficient
  • Entire process blocks if thread makes blocking system call
  • Cannot run in parallel on multiprocessors
  • Few systems currently use this model

Examples: Solaris Green Threads, GNU Portable Threads

2. One-to-One Model

User Space
↓ ↓ ↓
Kernel Space

Characteristics:

  • Each user thread maps to kernel thread
  • More concurrency - if one blocks, others can run
  • Can run on different CPUs in multiprocessor system
  • Thread management done by kernel - slower
  • Overhead of creating kernel threads

Examples: Windows, Linux

3. Many-to-Many Model

User Space
↘ ↓ ↙
Kernel Space

Characteristics:

  • Many user threads mapped to smaller or equal kernel threads
  • Allows more user threads than kernel threads
  • Kernel threads run in parallel on multiprocessor
  • If thread blocks, kernel can schedule another
  • Best features of both models

Examples: Solaris prior to version 9, Windows with ThreadFiber

4. Two-level Model

Similar to M:M, except it allows a user thread to be bound to kernel thread.

Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier

4.7 Thread Libraries

A thread library provides programmer with API for creating and managing threads.

POSIX Pthreads

  • POSIX standard (IEEE 1003.1c)
  • Specification, not implementation
  • Common in UNIX systems
  • Can be user or kernel level

Java Threads

  • Managed by JVM
  • Platform independent
  • Typically mapped to OS threads
  • Built into language

Windows Threads

  • Win32/64 API
  • Kernel-level library
  • One-to-one model
  • CreateThread() function

Pthreads Example

// Simple Pthread example in C #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *runner(void *param) { int *value = (int *)param; printf("Thread: value = %d\n", *value); *value = (*value) * 2; pthread_exit(0); } int main() { pthread_t tid; // Thread identifier pthread_attr_t attr; // Thread attributes int value = 5; // Initialize default attributes pthread_attr_init(&attr); // Create thread pthread_create(&tid, &attr, runner, &value); // Wait for thread to exit pthread_join(tid, NULL); printf("Main: value = %d\n", value); return 0; }

Common Pthread Functions

Function Description
pthread_create() Create a new thread
pthread_join() Wait for thread to terminate
pthread_exit() Terminate calling thread
pthread_attr_init() Initialize thread attributes
pthread_self() Get calling thread's ID
pthread_equal() Compare two thread IDs

4.8 Threading Issues

1. fork() and exec() Semantics

When a multithreaded process calls fork():

  • Should new process duplicate all threads?
  • Or only the calling thread?

Different systems handle this differently. exec() typically replaces entire process.

2. Signal Handling

Where should signal be delivered?

  • To the thread to which signal applies
  • To every thread in process
  • To certain threads in process
  • To specific thread assigned to receive all signals

3. Thread Cancellation

Type Description Issues
Asynchronous Terminate target thread immediately Resources may not be freed, data may be inconsistent
Deferred Target thread checks flag periodically Safer but requires cooperation

4. Thread-Local Storage (TLS)

Allows each thread to have its own copy of data. Useful when you don't have control over thread creation process.

5. Thread Pools

Concept:

Create number of threads at startup and place them in pool where they await work.

Advantages:

  • Usually slightly faster to service request with existing thread
  • Allows bound on number of threads in application
  • Separates task to be performed from thread management

4.9 Multithreaded Server Architecture

Client
Server
(Main Thread)
Worker Thread

How it works:

  1. Server creates separate thread that listens for client requests
  2. When request arrives, server creates new thread to service request
  3. Server resumes listening for additional requests
  4. More efficient than creating new process for each request

Example: Web Server

  • Main thread listens on port 80
  • For each HTTP request, spawn worker thread
  • Worker thread handles request and sends response
  • Main thread continues accepting new connections

4.10 Exam-Style Practice Questions

Multiple Choice Questions

Q1. Which is NOT shared by threads?

  • a) Program counter
  • b) Stack
  • c) Both (a) and (b)
  • d) None of the mentioned

Q2. A process that has multiple threads of control can do more than one task at a time. This is called:

  • a) Multiprogramming
  • b) Multithreading
  • c) Multiprocessing
  • d) Multitasking

Q3. In the Many-to-One threading model, if one thread performs a blocking system call:

  • a) Only that thread is blocked
  • b) The entire process blocks
  • c) Another thread is scheduled
  • d) The kernel switches to another process

Q4. Thread creation is:

  • a) More expensive than process creation
  • b) Less expensive than process creation
  • c) Equal in cost to process creation
  • d) Not possible in modern OS

Short Answer Questions

Q5. Consider this code segment. How many unique processes and threads are created?

pid = fork(); if (pid == 0) { /* child process */ fork(); thread_create(...); } fork();

Answer:

  • Initial fork() creates 1 child (2 processes total)
  • Child executes second fork() (3 processes total)
  • 2 processes execute thread_create() (2 threads created)
  • All 3 processes execute final fork() (6 processes total)
  • Total: 6 processes, 8 threads (6 main + 2 created)

Q6. In a multiprocessor system with many-to-many threading model, discuss performance when:

a) Number of kernel threads < number of processors

b) Number of kernel threads = number of processors

Answer:

a) Some processors remain idle since scheduler maps only kernel threads to processors, not user threads.

b) All processors can be utilized simultaneously. However, if kernel thread blocks (page fault, system call), corresponding processor becomes idle.

Q7. What is the difference between concurrency and parallelism?

Answer:

  • Concurrency: Multiple tasks making progress, possibly interleaved. Can occur on single processor through time-slicing.
  • Parallelism: Multiple tasks executing simultaneously. Requires multiple processors/cores.

4.11 Key Terms and Definitions

Term Definition
Thread Basic unit of CPU utilization; lightweight process
Multithreading Ability of OS to support multiple threads in single process
User Thread Thread managed by user-level library without kernel support
Kernel Thread Thread managed directly by operating system kernel
Thread Pool Collection of pre-created threads awaiting work
TLS Thread-Local Storage - per-thread data storage
pthread POSIX thread library specification
Concurrency Multiple tasks making progress (possibly interleaved)
Parallelism Multiple tasks executing simultaneously
Race Condition When outcome depends on timing of uncontrolled events

4.12 Diagrams from Slides

📊 Single vs Multithreaded Process

[Insert diagram from Chapter_4.pdf page 6]

📊 User and Kernel Threads

[Insert diagram showing user/kernel space separation]

📊 Multithreaded Server Architecture

[Insert server architecture diagram from slides]

📊 Threading Models Comparison

[Insert many-to-one, one-to-one, many-to-many models]

Note: Please provide the actual diagrams from your Chapter_4.pdf slides to replace these placeholders.