0% found this document useful (0 votes)
23 views

Lecture5 (Threads)

This document discusses threads and concurrency in operating systems. It introduces the concept of using multiple threads to implement different tasks within an application concurrently. Thread creation has lower overhead than process creation. The document also discusses benefits of multithreading like improved responsiveness. It covers concepts like concurrency vs parallelism, different types of parallelism, Amdahl's law, and threading models used in operating systems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture5 (Threads)

This document discusses threads and concurrency in operating systems. It introduces the concept of using multiple threads to implement different tasks within an application concurrently. Thread creation has lower overhead than process creation. The document also discusses benefits of multithreading like improved responsiveness. It covers concepts like concurrency vs parallelism, different types of parallelism, Amdahl's law, and threading models used in operating systems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Motivation

Most modern applications are multithreaded


Threads run within application
Lecture 5: Threads & Multiple tasks with the application can be implemented by
separate threads
Concurrency
Update display
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is
light-weight
Can simplify code, increase efficiency
Kernels are generally multithreaded
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018

Single and Multithreaded Processes Multithreaded Server Architecture

Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Benefits Multicore Programming
Multicore or multiprocessor systems putting pressure on
Responsiveness – may allow continued execution if part of programmers, challenges include:
process is blocked, especially important for user interfaces
Dividing activities
Resource Sharing – threads share resources of process,
easier than shared memory or message passing Balance
Economy – cheaper than process creation, thread switching Data splitting
lower overhead than context switching Data dependency
Scalability – process can take advantage of multicore Testing and debugging
architectures Parallelism implies a system can perform more than one task
simultaneously
Concurrency supports more than one task making progress
Single processor / core, scheduler providing concurrency

Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018

Concurrency vs. Parallelism Multicore Programming

Concurrent execution on single-core system: Types of parallelism


Data parallelism – distributes subsets of the same
data across multiple cores, same operation on each
Task parallelism – distributing threads across cores,
each thread performing unique operation
Parallelism on a multi-core system:

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
Data and Task Parallelism Amdahl’s Law
Identifies performance gains from adding additional cores to an application
that has both serial and parallel components
S is serial portion
N processing cores

That is, if application is 75% parallel / 25% serial, moving from 1 to 2 cores
results in speedup of 1.6 times
As N approaches infinity, speedup approaches 1 / S

Serial portion of an application has disproportionate effect on


performance gained by adding additional cores

But does the law take into account contemporary multicore systems?
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018

Amdahl’s Law User Threads and Kernel Threads

User threads - management done by user-level threads library


Three primary thread libraries:
POSIX Pthreads
Windows threads
Java threads
Kernel threads - Supported by the Kernel
Examples – virtually all general purpose operating systems, including:
Windows
Linux
Mac OS X
iOS
Android

Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
User and Kernel Threads Multithreading Models

Many-to-One

One-to-One

Many-to-Many

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018

Many-to-One One-to-One
Many user-level threads mapped to single kernel thread Each user-level thread maps to kernel thread
One thread blocking causes all to block Creating a user-level thread creates a kernel thread
Multiple threads may not run in parallel on multicore system More concurrency than many-to-one
because only one may be in kernel at a time Number of threads per process sometimes restricted due to
Few systems currently use this model overhead
Examples: Examples
Solaris Green Threads Windows
GNU Portable Threads Linux

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
Many-to-Many Model Two-level Model

Allows many user level threads to be mapped to many kernel threads Similar to M:M, except that it allows a user thread to be
Allows the operating system to create a sufficient number of kernel bound to kernel thread
threads
Windows with the ThreadFiber package
Otherwise not very common

Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018

Thread Libraries Pthreads

Thread library provides programmer with API for creating May be provided either as user-level or kernel-level
and managing threads A POSIX standard (IEEE 1003.1c) API for thread creation
Two primary ways of implementing and synchronization
Library entirely in user space Specification, not implementation
Kernel-level library supported by the OS API specifies behavior of the thread library, implementation
is up to development of the library
Common in UNIX operating systems (Linux & Mac OS X)

Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
Implicit Threading Thread Pools
Create a number of threads in a pool where they await work
Growing in popularity as numbers of threads increase, Advantages:
program correctness more difficult with explicit threads Usually slightly faster to service a request with an existing thread than
create a new thread
Creation and management of threads done by compilers
and run-time libraries rather than programmers Allows the number of threads in the application(s) to be bound to the
size of the pool
Five methods explored Separating task to be performed from mechanics of creating task
Thread Pools allows different strategies for running task
 i.e.Tasks could be scheduled to run periodically
Fork-Join
Windows API supports thread pools:
OpenMP
Grand Central Dispatch
Intel Threading Building Blocks

Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018

Java Thread Pools Java Thread Pools (cont)


Three factory methods for creating thread pools in Executors class:

Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018 Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018

You might also like