SlideShare a Scribd company logo
Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Chapter 4: MultithreadedChapter 4: Multithreaded
ProgrammingProgramming
Operating Systems
Dr. Ra’Fat Ahmad AL-msie’Deen
Chapter 4: Threads
Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
Text BookText Book::
This material is based on chapter 4 of “(Operating System
Concepts, [9th Edition])” by Silberschatz et al.,
4.3
Chapter 4: MultithreadedChapter 4: Multithreaded
ProgrammingProgramming
Overview
Multicore Programming
Multithreading Models
Thread Libraries
Implicit Threading
Threading Issues
Operating System Examples
4.4
ObjectivesObjectives
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
To explore several strategies that provide implicit threading
To examine issues related to multithreaded programming
To cover operating system support for threads in Windows and Linux
4.5
MotivationMotivation
Most modern applications are multithreaded
Threads run within application
Multiple tasks with the application can be implemented by separate
threads
Update display / mouse on screen
Fetch data
Spell checking
Answer a network request
Process creation is heavy-weight while thread creation is light-
weight (less resources)
Can simplify code, increase efficiency
Kernels are generally multithreaded
4.6
Chapter 4: ThreadsChapter 4: Threads
A thread is a basic unit of CPU utilization (use) ,(Lightweight
process: LWP), not stand alone process but a part of it (process).
Each thread has its own ID , stack, program counter, and registers.
Threads share common code, data, and open files.
Each single window under word process is a single thread.
Figure x. Lightweight process (LWP).
4.7
Single and Multithreaded ProcessesSingle and Multithreaded Processes
4.8
Multithreaded Server ArchitectureMultithreaded Server Architecture
Figure x. Multithreaded server architecture.
4.9
BenefitsBenefits
Responsiveness: Faster execution.
Resource Sharing: By default threads share common code,
data, and other resources, which allows multiple tasks to be
performed simultaneously in a single address space.
threads share resources of process, easier than shared
memory or message passing
Economy :Creating and managing threads ( and context switches
between them ) is much faster than performing the same tasks for
processes.
cheaper than process creation, thread switching lower
overhead than context switching
Utilization of MP Architectures: A single threaded process
can only run on one CPU, whereas the execution of a multi-
threaded application may be split between available processors.
Scalability – process can take advantage of multiprocessor
architectures
4.10
Multicore ProgrammingMulticore 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
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
4.11
Concurrency vs. ParallelismConcurrency vs. Parallelism
Concurrent execution on single-core system:
Parallelism on a multi-core system:
4.12
Types of threadsTypes of threads
User threads
Threads that created and managed in the user space , without
kernel support.
 User threads - management done by user-level threads library
These are the threads that application programmers put into
their programs.
 user-direct to- CPU (execution)
Kernel acts with processes not with threads.
Kernel threads
Kernel threads - Supported by the Kernel
threads are supported (managed, created, killed) by the OS.
Kernel acts with threads.
Examples – virtually all general purpose operating systems,
including: Windows, Solaris, Linux, Tru64 UNIX and Mac OS X.
4.13
Multithreading ModelsMultithreading Models
Many-to-One
One-to-One
Many-to-Many
4.14
Many-to-OneMany-to-One
Many user-level threads mapped to single kernel thread
One thread blocking causes all to block
If a blocking system call is made, then the entire process blocks.
Multiple threads may not run in parallel on Multicore system because only one
may be in kernel at a time
Single kernel thread can operate only on a single CPU, so the many-to-one
model does not allow individual processes to be split across multiple CPUs.
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
Figure x. Many-to-one model.
4.15
One-to-OneOne-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
The overhead of managing the one-to-one model is more
significant, involving more overhead and slowing down the
system.
Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
Figure x. One-to-one model.
4.16
Many-to-Many ModelMany-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
Block a specific thread ,unlike many-one
Less overhead, unlike one-one
Solaris prior to version 9
Windows NT/2000 with the ThreadFiber
package
Figure x. Many-to-many model.
4.17
Two-level ModelTwo-level Model
Similar to M:M, except that it allows a user thread to be bound to
kernel thread
A popular variation of the many-to-many model is the two-tier model,
which allows either many-to-many or one-to-one operation.
Examples
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
Figure x. Two-level model.
4.18
Thread LibrariesThread Libraries
Thread library provides programmer with API for creating and
managing threads
Two primary ways of implementing
Library entirely in user space
Kernel-level library supported by the OS
4.19
PthreadsPthreads
May be provided either as user-level or kernel-level
A POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization
POSIX stands for Portable Operating System Interface for Unix
Specification, not implementation
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)
4.20
Windows ThreadsWindows Threads
The technique for creating threads using the Windows thread
library is similar to the Pthreads technique in several ways.
4.21
Java ThreadsJava Threads
Threads are the fundamental model of program execution in a Java
program, and the Java language and its API provide a rich set of features for
the creation and management of threads.
Java threads are managed by the JVM
Typically implemented using the threads model provided by underlying OS
Java threads may be created by:
Extending Thread class
Implementing the Runnable interface
4.22
Implicit ThreadingImplicit Threading
Growing in popularity as numbers of threads increase, program correctness more
difficult with explicit threads.
Creation and management of threads done by compilers and run-time libraries
rather than programmers.
Three methods explored
Thread Pools –
 Create a number of threads in a pool where they await work
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.
Grand Central Dispatch –
 Apple technology for Mac OS X and iOS operating systems
 Extensions to C, C++ languages, API, and run-time library
 Allows identification of parallel sections
Other methods include Microsoft Threading Building Blocks (TBB),
java.util.concurrent package
4.23
Thread PoolsThread Pools
Create a number of threads in a pool where they await work.
Advantages: (Thread pools offer these benefits:)
Usually slightly faster to service a request with an existing thread than
create a new thread.
Allows the number of threads in the application(s) to be bound to the
size of the pool.
A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks
(yellow)
4.24
Threading IssuesThreading Issues
Semantics of fork() and exec() system calls
Thread cancellation
4.25
Semantics of fork() and exec()Semantics of fork() and exec()
Does fork() duplicate only the calling thread or all threads?
o Some UNIXes have two versions of fork
There are two options:
 Create a child process that contains identical copy of the whole
threads in the parent process.
OR
 Create a child process that contains a copy of the thread that
executed the fork call only.
Exec() usually works as normal – replace the running process
including all threads
4.26
Thread CancellationThread Cancellation
Terminating a thread before it has finished
Thread to be canceled is target thread
Two general approaches:
 Asynchronous cancellation terminates the target thread
immediately.
 Deferred (synchronous) cancellation allows the target
thread to periodically check if it should be cancelled.
 Why :
 Data consistent , …… updating.
 On Linux systems, thread cancellation is handled through signals
 Signals are used in UNIX systems to notify a process that a
particular event has occurred.
 A signal handler is used to process signals. (See next slide)
4.27
Signal HandlingSignal Handling
Signals are used in UNIX systems to notify a process that a particular event has
occurred
A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
 Signal is handled by one of two signal handlers:
1. Default signal handler
2. User-defined signal handler
Options:
Deliver the signal to the thread to which the signal applies
Deliver the signal to every thread in the process
Deliver the signal to certain threads in the process
Assign a specific thread to receive all signals for the process
4.28
Thread-Local StorageThread-Local Storage
Thread-local storage (TLS) allows each thread to have its own copy of
data.
Useful when you do not have control over the thread creation process (i.e.,
when using a thread pool).
Different from local variables:
 Local variables visible only during single function invocation.
 TLS visible across function invocations.
Similar to static data.
TLS is unique to each thread.
4.29
Scheduler ActivationsScheduler Activations
Both M:M and Two-level models require communication to maintain the
appropriate number of kernel threads allocated to the application.
Typically use an intermediate data structure between user and kernel threads –
lightweight process (LWP).
 Appears to be a virtual processor on which process can schedule user thread
to run.
 Each LWP attached to kernel thread.
 How many LWPs to create?
Scheduler activations provide upcalls - a communication mechanism from the
kernel to the upcall handler in the thread library.
This communication allows an application to maintain the correct number kernel
threads
Figure x. Lightweight process (LWP).
4.30
Operating System ExamplesOperating System Examples
Windows XP Threads:
 Windows implements the Windows API – primary API for Win 98,
Win NT, Win 2000, Win XP, and Win 7.
 Implements the one-to-one mapping, kernel-level
 The register set, stacks, and private storage area are known as
the context of the thread
Linux Thread:
 Linux refers to them as tasks rather than threads.
 Thread creation is done through clone() system call.
Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th
Edition
End of Chapter 4End of Chapter 4
Operating Systems
Dr. Ra’Fat Ahmad AL-msie’Deen
Chapter 4: Threads

More Related Content

What's hot

Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
Mukesh Chinta
 
Process threads operating system.
Process threads operating system.Process threads operating system.
Process threads operating system.
Reham Maher El-Safarini
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
Prakhar Maurya
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
Shafaan Khaliq Bhatti
 
Chapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzChapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatz
GiulianoRanauro
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
usmankiyani1
 
Operating System 3
Operating System 3Operating System 3
Operating System 3tech2click
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
MsAnita2
 
CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2
Kathirvel Ayyaswamy
 
Multithreading
MultithreadingMultithreading
Multithreading
A B Shinde
 
cpu scheduling
cpu schedulingcpu scheduling
cpu scheduling
hashim102
 
Demand paging
Demand pagingDemand paging
Demand paging
Trinity Dwarka
 
Linux architecture
Linux architectureLinux architecture
Linux architecturemcganesh
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
Shubhashish Punj
 
Process management in os
Process management in osProcess management in os
Process management in os
Miong Lazaro
 
Os Threads
Os ThreadsOs Threads
Os Threads
Salman Memon
 
Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
sangrampatil81
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 

What's hot (20)

Operating systems system structures
Operating systems   system structuresOperating systems   system structures
Operating systems system structures
 
Process threads operating system.
Process threads operating system.Process threads operating system.
Process threads operating system.
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
 
Chapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzChapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatz
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2
 
Multithreading
MultithreadingMultithreading
Multithreading
 
cpu scheduling
cpu schedulingcpu scheduling
cpu scheduling
 
Demand paging
Demand pagingDemand paging
Demand paging
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
operating system structure
operating system structureoperating system structure
operating system structure
 
Process management in os
Process management in osProcess management in os
Process management in os
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Deadlock
DeadlockDeadlock
Deadlock
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 

Similar to Operating Systems - "Chapter 4: Multithreaded Programming"

CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
ImranKhan880955
 
My DIaries
My DIariesMy DIaries
My DIaries
Mehak Raza
 
ch4.ppt
ch4.pptch4.ppt
ch4.ppt
ssuser9f517f1
 
ch4.ppt
ch4.pptch4.ppt
ch4.ppt operation system. Dats structure and alogor
ch4.ppt operation system. Dats structure and alogorch4.ppt operation system. Dats structure and alogor
ch4.ppt operation system. Dats structure and alogor
AjithPArun1
 
Threads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.pptThreads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.ppt
StephyBless
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
AnyapuPranav
 
Threads are using in operating systems.pptx
Threads are using in operating systems.pptxThreads are using in operating systems.pptx
Threads are using in operating systems.pptx
MAmir53
 
Os
OsOs
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
DanishMahmood23
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
bleh23
 
Treads
TreadsTreads
Threads & Concurrency
Threads & Concurrency Threads & Concurrency
Threads & Concurrency
NomanMHasan
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
BalasundaramSr
 
4 threads
4 threads4 threads
4 threads
BaliThorat1
 

Similar to Operating Systems - "Chapter 4: Multithreaded Programming" (20)

CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
 
Threads
ThreadsThreads
Threads
 
My DIaries
My DIariesMy DIaries
My DIaries
 
ch4.ppt
ch4.pptch4.ppt
ch4.ppt
 
ch4.ppt
ch4.pptch4.ppt
ch4.ppt
 
ch4.ppt
ch4.pptch4.ppt
ch4.ppt
 
ch4.ppt operation system. Dats structure and alogor
ch4.ppt operation system. Dats structure and alogorch4.ppt operation system. Dats structure and alogor
ch4.ppt operation system. Dats structure and alogor
 
Threads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.pptThreads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.ppt
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Threads are using in operating systems.pptx
Threads are using in operating systems.pptxThreads are using in operating systems.pptx
Threads are using in operating systems.pptx
 
Os
OsOs
Os
 
Chapter04 new
Chapter04 newChapter04 new
Chapter04 new
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Section04 threads
Section04 threadsSection04 threads
Section04 threads
 
Treads
TreadsTreads
Treads
 
Ch4 Threads
Ch4 ThreadsCh4 Threads
Ch4 Threads
 
Threads & Concurrency
Threads & Concurrency Threads & Concurrency
Threads & Concurrency
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
4 threads
4 threads4 threads
4 threads
 

More from Ra'Fat Al-Msie'deen

SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdfSoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
Ra'Fat Al-Msie'deen
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdfBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
Ra'Fat Al-Msie'deen
 
Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
Ra'Fat Al-Msie'deen
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
Ra'Fat Al-Msie'deen
 
Source Code Summarization
Source Code SummarizationSource Code Summarization
Source Code Summarization
Ra'Fat Al-Msie'deen
 
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
Ra'Fat Al-Msie'deen
 
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
Ra'Fat Al-Msie'deen
 
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Ra'Fat Al-Msie'deen
 
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus ApproachAutomatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
Ra'Fat Al-Msie'deen
 
Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...
Ra'Fat Al-Msie'deen
 
Detecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variantsDetecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variants
Ra'Fat Al-Msie'deen
 
Naming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source CodeNaming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source Code
Ra'Fat Al-Msie'deen
 
Application architectures - Software Architecture and Design
Application architectures - Software Architecture and DesignApplication architectures - Software Architecture and Design
Application architectures - Software Architecture and Design
Ra'Fat Al-Msie'deen
 
Planning and writing your documents - Software documentation
Planning and writing your documents - Software documentationPlanning and writing your documents - Software documentation
Planning and writing your documents - Software documentation
Ra'Fat Al-Msie'deen
 
Requirements management planning & Requirements change management
Requirements management planning & Requirements change managementRequirements management planning & Requirements change management
Requirements management planning & Requirements change management
Ra'Fat Al-Msie'deen
 
Requirements change - requirements engineering
Requirements change - requirements engineeringRequirements change - requirements engineering
Requirements change - requirements engineering
Ra'Fat Al-Msie'deen
 
Requirements validation - requirements engineering
Requirements validation - requirements engineeringRequirements validation - requirements engineering
Requirements validation - requirements engineering
Ra'Fat Al-Msie'deen
 
Software Documentation - writing to support - references
Software Documentation - writing to support - referencesSoftware Documentation - writing to support - references
Software Documentation - writing to support - references
Ra'Fat Al-Msie'deen
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
Ra'Fat Al-Msie'deen
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
Ra'Fat Al-Msie'deen
 

More from Ra'Fat Al-Msie'deen (20)

SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdfSoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
SoftCloud: A Tool for Visualizing Software Artifacts as Tag Clouds.pdf
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdfBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports.pdf
 
Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...Software evolution understanding: Automatic extraction of software identifier...
Software evolution understanding: Automatic extraction of software identifier...
 
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
 
Source Code Summarization
Source Code SummarizationSource Code Summarization
Source Code Summarization
 
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
FeatureClouds: Naming the Identified Feature Implementation Blocks from Softw...
 
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
YamenTrace: Requirements Traceability - Recovering and Visualizing Traceabili...
 
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
Requirements Traceability: Recovering and Visualizing Traceability Links Betw...
 
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus ApproachAutomatic Labeling of the Object-oriented Source Code: The Lotus Approach
Automatic Labeling of the Object-oriented Source Code: The Lotus Approach
 
Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...Constructing a software requirements specification and design for electronic ...
Constructing a software requirements specification and design for electronic ...
 
Detecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variantsDetecting commonality and variability in use-case diagram variants
Detecting commonality and variability in use-case diagram variants
 
Naming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source CodeNaming the Identified Feature Implementation Blocks from Software Source Code
Naming the Identified Feature Implementation Blocks from Software Source Code
 
Application architectures - Software Architecture and Design
Application architectures - Software Architecture and DesignApplication architectures - Software Architecture and Design
Application architectures - Software Architecture and Design
 
Planning and writing your documents - Software documentation
Planning and writing your documents - Software documentationPlanning and writing your documents - Software documentation
Planning and writing your documents - Software documentation
 
Requirements management planning & Requirements change management
Requirements management planning & Requirements change managementRequirements management planning & Requirements change management
Requirements management planning & Requirements change management
 
Requirements change - requirements engineering
Requirements change - requirements engineeringRequirements change - requirements engineering
Requirements change - requirements engineering
 
Requirements validation - requirements engineering
Requirements validation - requirements engineeringRequirements validation - requirements engineering
Requirements validation - requirements engineering
 
Software Documentation - writing to support - references
Software Documentation - writing to support - referencesSoftware Documentation - writing to support - references
Software Documentation - writing to support - references
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Algorithms - "quicksort"
Algorithms - "quicksort"Algorithms - "quicksort"
Algorithms - "quicksort"
 

Recently uploaded

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
datarid22
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
SriSurya50
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 

Recently uploaded (20)

Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 

Operating Systems - "Chapter 4: Multithreaded Programming"

  • 1. Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Chapter 4: MultithreadedChapter 4: Multithreaded ProgrammingProgramming Operating Systems Dr. Ra’Fat Ahmad AL-msie’Deen Chapter 4: Threads
  • 2. Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition Text BookText Book:: This material is based on chapter 4 of “(Operating System Concepts, [9th Edition])” by Silberschatz et al.,
  • 3. 4.3 Chapter 4: MultithreadedChapter 4: Multithreaded ProgrammingProgramming Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues Operating System Examples
  • 4. 4.4 ObjectivesObjectives 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 To explore several strategies that provide implicit threading To examine issues related to multithreaded programming To cover operating system support for threads in Windows and Linux
  • 5. 4.5 MotivationMotivation Most modern applications are multithreaded Threads run within application Multiple tasks with the application can be implemented by separate threads Update display / mouse on screen Fetch data Spell checking Answer a network request Process creation is heavy-weight while thread creation is light- weight (less resources) Can simplify code, increase efficiency Kernels are generally multithreaded
  • 6. 4.6 Chapter 4: ThreadsChapter 4: Threads A thread is a basic unit of CPU utilization (use) ,(Lightweight process: LWP), not stand alone process but a part of it (process). Each thread has its own ID , stack, program counter, and registers. Threads share common code, data, and open files. Each single window under word process is a single thread. Figure x. Lightweight process (LWP).
  • 7. 4.7 Single and Multithreaded ProcessesSingle and Multithreaded Processes
  • 8. 4.8 Multithreaded Server ArchitectureMultithreaded Server Architecture Figure x. Multithreaded server architecture.
  • 9. 4.9 BenefitsBenefits Responsiveness: Faster execution. Resource Sharing: By default threads share common code, data, and other resources, which allows multiple tasks to be performed simultaneously in a single address space. threads share resources of process, easier than shared memory or message passing Economy :Creating and managing threads ( and context switches between them ) is much faster than performing the same tasks for processes. cheaper than process creation, thread switching lower overhead than context switching Utilization of MP Architectures: A single threaded process can only run on one CPU, whereas the execution of a multi- threaded application may be split between available processors. Scalability – process can take advantage of multiprocessor architectures
  • 10. 4.10 Multicore ProgrammingMulticore 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 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
  • 11. 4.11 Concurrency vs. ParallelismConcurrency vs. Parallelism Concurrent execution on single-core system: Parallelism on a multi-core system:
  • 12. 4.12 Types of threadsTypes of threads User threads Threads that created and managed in the user space , without kernel support.  User threads - management done by user-level threads library These are the threads that application programmers put into their programs.  user-direct to- CPU (execution) Kernel acts with processes not with threads. Kernel threads Kernel threads - Supported by the Kernel threads are supported (managed, created, killed) by the OS. Kernel acts with threads. Examples – virtually all general purpose operating systems, including: Windows, Solaris, Linux, Tru64 UNIX and Mac OS X.
  • 14. 4.14 Many-to-OneMany-to-One Many user-level threads mapped to single kernel thread One thread blocking causes all to block If a blocking system call is made, then the entire process blocks. Multiple threads may not run in parallel on Multicore system because only one may be in kernel at a time Single kernel thread can operate only on a single CPU, so the many-to-one model does not allow individual processes to be split across multiple CPUs. Few systems currently use this model Examples: Solaris Green Threads GNU Portable Threads Figure x. Many-to-one model.
  • 15. 4.15 One-to-OneOne-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 The overhead of managing the one-to-one model is more significant, involving more overhead and slowing down the system. Examples Windows NT/XP/2000 Linux Solaris 9 and later Figure x. One-to-one model.
  • 16. 4.16 Many-to-Many ModelMany-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 Block a specific thread ,unlike many-one Less overhead, unlike one-one Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package Figure x. Many-to-many model.
  • 17. 4.17 Two-level ModelTwo-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread A popular variation of the many-to-many model is the two-tier model, which allows either many-to-many or one-to-one operation. Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier Figure x. Two-level model.
  • 18. 4.18 Thread LibrariesThread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space Kernel-level library supported by the OS
  • 19. 4.19 PthreadsPthreads May be provided either as user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization POSIX stands for Portable Operating System Interface for Unix Specification, not implementation 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)
  • 20. 4.20 Windows ThreadsWindows Threads The technique for creating threads using the Windows thread library is similar to the Pthreads technique in several ways.
  • 21. 4.21 Java ThreadsJava Threads Threads are the fundamental model of program execution in a Java program, and the Java language and its API provide a rich set of features for the creation and management of threads. Java threads are managed by the JVM Typically implemented using the threads model provided by underlying OS Java threads may be created by: Extending Thread class Implementing the Runnable interface
  • 22. 4.22 Implicit ThreadingImplicit Threading Growing in popularity as numbers of threads increase, program correctness more difficult with explicit threads. Creation and management of threads done by compilers and run-time libraries rather than programmers. Three methods explored Thread Pools –  Create a number of threads in a pool where they await work 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. Grand Central Dispatch –  Apple technology for Mac OS X and iOS operating systems  Extensions to C, C++ languages, API, and run-time library  Allows identification of parallel sections Other methods include Microsoft Threading Building Blocks (TBB), java.util.concurrent package
  • 23. 4.23 Thread PoolsThread Pools Create a number of threads in a pool where they await work. Advantages: (Thread pools offer these benefits:) Usually slightly faster to service a request with an existing thread than create a new thread. Allows the number of threads in the application(s) to be bound to the size of the pool. A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks (yellow)
  • 24. 4.24 Threading IssuesThreading Issues Semantics of fork() and exec() system calls Thread cancellation
  • 25. 4.25 Semantics of fork() and exec()Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? o Some UNIXes have two versions of fork There are two options:  Create a child process that contains identical copy of the whole threads in the parent process. OR  Create a child process that contains a copy of the thread that executed the fork call only. Exec() usually works as normal – replace the running process including all threads
  • 26. 4.26 Thread CancellationThread Cancellation Terminating a thread before it has finished Thread to be canceled is target thread Two general approaches:  Asynchronous cancellation terminates the target thread immediately.  Deferred (synchronous) cancellation allows the target thread to periodically check if it should be cancelled.  Why :  Data consistent , …… updating.  On Linux systems, thread cancellation is handled through signals  Signals are used in UNIX systems to notify a process that a particular event has occurred.  A signal handler is used to process signals. (See next slide)
  • 27. 4.27 Signal HandlingSignal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled  Signal is handled by one of two signal handlers: 1. Default signal handler 2. User-defined signal handler Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process
  • 28. 4.28 Thread-Local StorageThread-Local Storage Thread-local storage (TLS) allows each thread to have its own copy of data. Useful when you do not have control over the thread creation process (i.e., when using a thread pool). Different from local variables:  Local variables visible only during single function invocation.  TLS visible across function invocations. Similar to static data. TLS is unique to each thread.
  • 29. 4.29 Scheduler ActivationsScheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application. Typically use an intermediate data structure between user and kernel threads – lightweight process (LWP).  Appears to be a virtual processor on which process can schedule user thread to run.  Each LWP attached to kernel thread.  How many LWPs to create? Scheduler activations provide upcalls - a communication mechanism from the kernel to the upcall handler in the thread library. This communication allows an application to maintain the correct number kernel threads Figure x. Lightweight process (LWP).
  • 30. 4.30 Operating System ExamplesOperating System Examples Windows XP Threads:  Windows implements the Windows API – primary API for Win 98, Win NT, Win 2000, Win XP, and Win 7.  Implements the one-to-one mapping, kernel-level  The register set, stacks, and private storage area are known as the context of the thread Linux Thread:  Linux refers to them as tasks rather than threads.  Thread creation is done through clone() system call.
  • 31. Silberschatz, Galvin and Gagne ©2013Operating System Concepts – 9th Edition End of Chapter 4End of Chapter 4 Operating Systems Dr. Ra’Fat Ahmad AL-msie’Deen Chapter 4: Threads