OS Chap 4 Slides
OS Chap 4 Slides
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Objectives
To introduce the notion of a thread—a fundamental unit of CPU utilization that forms the basis of
multithreaded computer systems
To discuss the APIs for the Pthreads, Windows, and Java thread libraries
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes
A thread is a basic unit of CPU utilization
It shares with other threads belonging to the same process its code
section, data section, and other operating-system resources, such as
open files and signals.
Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes
Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Motivation
A web server accepts client requests for web pages, images, sound, and
so forth. A busy web server may have several (perhaps thousands of)
clients concurrently accessing it.
Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture
Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Benefits
Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces
Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
A system is parallel if it can perform more than one task
simultaneously.
Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
Concurrency vs. Parallelism
Concurrent execution on single-core system:
Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Multicore Programming Challenges
For designers of operating systems must write scheduling algorithms
that use multiple processing cores to allow the parallel execution.
Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Multicore Programming Challenges
In general, five areas present challenges
Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Types of Parallelism
Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
Types of Parallelism: Data parallelism
Data parallelism focuses on distributing subsets of the same data
across multiple computing cores and performing the same operation on
each core. For example, summing the contents of an array of size N.
Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
Types of Parallelism: Task parallelism
Task parallelism involves distributing not data but tasks (threads)
across multiple computing cores.
Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
User Threads and Kernel Threads
User threads - management done by user-level threads library
Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
User Threads and Kernel Threads
Kernel threads - Supported by the Kernel
Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
1. Many-to-One
2. One-to-One
3. Many-to-Many
Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Many-to-One
Many user-level threads mapped to single
kernel thread.
One thread blocking causes all to block
Multiple threads may not run in parallel on
muticore system because only one may be in
kernel at a time
Examples:
Solaris Green Threads
GNU Portable Threads
Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
One-to-One
Each user-level thread maps to kernel thread
Creating a user-level thread creates a kernel thread
More concurrency than many-to-one
Number of threads per process sometimes restricted due to overhead
Examples are Windows, Linux, Solaris 9 and later
Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel
threads
Solaris prior to version 9 and Windows with the ThreadFiber package
Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Two-level Model
Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Thread Libraries
Thread library provides programmer with API for creating and managing
threads
Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Java Threads
All Java programs comprise at least a single thread of control—even a
simple Java program consisting of only a main() method runs as a
single thread
Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
Java Threads
Java threads may be created by:
Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Java Threads
When a class implements Runnable, it must define a run() method.
Operating System Concepts – 9th Edition 4.26 Silberschatz, Galvin and Gagne ©2013
Java Multithreaded Program
Operating System Concepts – 9th Edition 4.27 Silberschatz, Galvin and Gagne ©2013
Java Multithreaded Program
Operating System Concepts – 9th Edition 4.28 Silberschatz, Galvin and Gagne ©2013
Implicit Threading
With the continued growth of multicore processing, applications
containing hundreds—or even thousands—of threads are looming on
the horizon.
Operating System Concepts – 9th Edition 4.29 Silberschatz, Galvin and Gagne ©2013
Implicit Threading
By Implicit Threading we means creation and management of threads
done by compilers and run-time libraries rather than programmers.
Operating System Concepts – 9th Edition 4.30 Silberschatz, Galvin and Gagne ©2013
Thread Pools
Thread pool is to create a number of threads at process startup and
place them into a pool, where they sit and wait for work.
Once the thread completes its service, it returns to the pool and awaits
more work
If the pool contains no available thread, the server waits until one
becomes free.
Operating System Concepts – 9th Edition 4.31 Silberschatz, Galvin and Gagne ©2013
Thread Pools
Advantages:
Operating System Concepts – 9th Edition 4.32 Silberschatz, Galvin and Gagne ©2013
OpenMP
OpenMP is a set of compiler directives as well as an API for programs
written in C, C++, or FORTRAN that provides support for parallel
programming in shared-memory environments.
Operating System Concepts – 9th Edition 4.33 Silberschatz, Galvin and Gagne ©2013
OpenMP
#pragma omp parallel
Create as many threads as there are
cores
Operating System Concepts – 9th Edition 4.34 Silberschatz, Galvin and Gagne ©2013
Other Approaches
Other commercial approaches include
Operating System Concepts – 9th Edition 4.35 Silberschatz, Galvin and Gagne ©2013
Homework
Write answers to the (Only) even numbered exercise
question from 4.1 to 4.16
Operating System Concepts – 9th Edition 4.36 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013