Lecture 4
Lecture 4
1. Overview
2. Multicore Programming
3. Multithreading Models
4. Thread Libraries
5. Implicit Threading
6. Threading Issues
7. Operating System Examples
Single and Multithreaded Processes
Multithreaded Server Architecture
Benefits
❑Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces
❑Resource Sharing – threads share resources of process, easier than
shared memory or message passing
❑Economy – cheaper than process creation, thread switching lower
overhead than context switching
❑Scalability – process can take advantage of multicore architectures
Multicore Programming
❑Multicore or multiprocessor systems putting pressure on programmers,
challenges include:
❑Dividing activities
❑Balance
❑Data splitting
❑Data dependency
❑Testing and debugging
❑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
Concurrency vs. Parallelism
❑ Concurrent execution on single-core system:
❑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.
❑Task parallelism – Task parallelism involves distributing not data but tasks
(threads) across multiple computing cores.
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?
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
User and Kernel Threads
Multithreading Models
❑Many-to-One
❑One-to-One
❑Many-to-Many
Many-to-One
❑Pthreads refers to the POSIX standard (IEEE 1003.1c) defining an API for
thread creation and synchronization
May be provided either as user-level or kernel-level
❑A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
❑Specification, not implementation
❑API specifies behavior of the thread library, implementation is up to
development of the library
❑Common in UNIX operating systems (Linux & Mac OS X)
Pthreads Example
Pthreads Example (Cont’n) (cont’d)
Pthreads Code for Joining 10 Threads
Windows Multithreaded C Program
Windows Multithreaded C Program (cont’d)
Java Threads
Creating a thread:
Waiting on a thread:
Java Executor Framework
❑Rather than explicitly creating threads, Java also allows
thread creation around the Executor interface:
❑Windows Threads
❑Linux Threads
Windows Threads
❑Windows API – primary API for Windows applications
❑Implements the one-to-one mapping, kernel-level
❑Each thread contains
❑A thread id
❑Register set representing state of processor
❑Separate user and kernel stacks for when thread runs in user mode or kernel mode
❑Private data storage area used by run-time libraries and dynamic link libraries (DLLs)
❑The register set, stacks, and private storage area are known as the context of
the thread
Windows Threads (cont’d)