Professional Documents
Culture Documents
Lecture 11
1/2/2022
Agenda
• Threads
– Introduction
– Motivation
– Single and Multithreaded Processes
– Multicore Programming
– Parallelism: Data Parallelism Vs. Task Parallelism
– Concurrency Vs. Parallelism
– User Threads and Kernel Threads
– Multithreading Models
– Thread Libraries
Motivation
• What is a Thread?
– A thread is a basic unit of CPU Utilization
– It comprises of a thread ID, a PC, a register set, and a stack
– It shares with other threads code section, data section and other OS
resources like open files
– Each thread represents a separate flow of control.
• A traditional process has a single thread of control. Also, called
Heavyweight process
• In modern systems, processes have multiple threads of control. As
a result, it can perform multiple tasks a time provided hardware
supports.
• A thread is also called a lightweight process.
• Threads provide a way to improve application performance
through parallelism.
Single and Multithreaded Processes
Benefits
• Responsiveness: Multi threading an interactive application may allow a program
to continue running even if part of it is blocked or is performing lengthy operation,
there by increasing responsiveness to the user.
• Resource Sharing: threads share memory and resources of the process to which
they belong by default. It allows an application to have several different threads of
activity within the same address space.
• Economy: Allocating memory and resources for process creation is costly. Process
context switch overhead is huge. But since threads share address space and
resources of the process to which it belongs, it is more economical to create and
context switch thread.
Process Thread
Process is heavy weight or resource Thread is light weight, takes less resources
intensive. than a process.
Process switching needs interaction Thread switching does not need to interact
with operating system with operating system.
In multiple processing environments, All threads can share same set of open files,
each process executes the same code child processes
but has its own memory and file
resources
If one process is blocked, then no other While one thread is blocked and waiting, a
process can execute until the first second thread in the same task can run
process is unblocked.
Concurrent Execution on a
Single-core System
Parallel Execution on a
Multicore System
Types of Thread
• User Level Threads − These threads are managed by the user thread
libraries like POSIX. JAVA etc.
• Kernel Level Threads − Operating System manages threads acting on
kernel
User Level Threads
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
• Many user-level threads mapped to single kernel thread
• Entire process will be blocked if any of the threads make a blocking
system call since all the threads are mapped to a single kernel thread
• Can not run in parallel on multiple cores/processors.
• Examples:
– Solaris Green Threads
– GNU Portable Threads
Many-to-One Model
One-to-One
• Each user-level thread maps to a kernel thread.
• It provides more concurrency than many to one mapping by
allowing another thread to run when one thread makes a
blocking system call.
• But drawback is creating a user thread requires creating the
corresponding kernel thread.
• Examples
– Windows NT/XP/2000
– Linux
– Solaris 9 and later
One-to-one Model
Many-to-Many Model
• Thread library provides programmer with API for creating and managing
threads
• Two primary ways of implementing
– Library entirely in user space
• This means invoking a library function results in a local function
call in user space and not the system call
– Kernel-level library supported by the OS
• This means invoking a library function results in a system call to the
kernel
Pthreads