You are on page 1of 26

Threads

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

• Modern software applications that run on modern computers (multicore/multiprocessor)


are multithreaded.
• An application is implemented as a separate process with several threads of control.
• Example 1, A web brouser can have multiple threads such as
(i) Display image/text
(ii) Thread to retrieve data from the network
• Examle 2, A word processor can have multiple tasks which can be mapped to multiple
threads
(i) Displaying the graphics
(ii) Thread to respond to a keystrokes from the user
(iii) Thread to perform spell and grammar check
• An application can perform several CPU-intensive tasks in parallel across the multiple
cores.

Introduction

• 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.

• Scalability: multiple threads can run in parallel on multiple processors in


multiprocessor architecture. It increases parallelism.
Multicore Programming

• Multicore systems putting pressure on programmers, challenges


include:
– Dividing activities : This involves examining application to find areas that
can be divided into separate, independent tasks and thus can run in
parallel on individual cores.
– Balance: While identifying tasks that can run in parallel, programmers
must also ensure that the tasks perform equal work on each core.
– Data splitting: Just as application is divided into multiple tasks, the data
used by the tasks must be divided to rune on separate cores.
– Data dependency: tasks should be synchronized in order to ensure data
dependency
– Testing and debugging: Since threads are running in parallel on multiple
cores, there are many different execution paths, so testing and debugging
is more difficult than single threaded applications.
Multithreaded Server Architecture
Difference between Process and 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

• The thread management system


/service is not aware of the existence
of user threads.
• The thread library contains code for
– creating and destroying threads,
– for passing message and data
between threads,
– for scheduling thread execution
– and for saving and restoring thread
contexts
Kernel Level Threads

• Thread management is done by the Kernel.


• There is no thread management code in the application area.
• Kernel threads are supported directly by the operating system.
• The Kernel performs thread creation, scheduling and management in
Kernel space.
• Kernel threads are generally slower to create and manage than the user
threads
Multithreading Models

• 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

• Allows many user level threads to be mapped to many kernel


threads
• Allows the operating system to create a sufficient number of kernel
threads
• It does not suffer from neither of the shortcoming s of previous
models.
• Here developer can create as many user threads as needed and the
corresponding kernel threads can run in parallel on multiprocessor.
• No problem of blocking system call.

• Solaris prior to version 9

• Windows NT/2000 with the ThreadFiber package


Many-to-Many Model
Thread Libraries

• 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

• May be provided either as user-level or kernel-level

• Pthreads refers to POSIX standard (IEEE 1003.1c) defining an API for


thread creation and synchronization

• API specifies behavior of the thread library, implementation is up to


development of the library

• Common in UNIX operating systems (Solaris, Linux, Mac OS X)


Pthreads Example
Pthreads Example (Cont.)
• Questions ?

You might also like