SlideShare ist ein Scribd-Unternehmen logo
1 von 31
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

Weitere ähnliche Inhalte

Was ist angesagt?

Kernel mode vs user mode in linux
Kernel mode vs user mode in linuxKernel mode vs user mode in linux
Kernel mode vs user mode in linuxSiddique Ibrahim
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemShafaan Khaliq Bhatti
 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introductioncscarcas
 
Chapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzChapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzGiulianoRanauro
 
Unit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - ThreadsUnit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - Threadscscarcas
 
Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 
Memory management
Memory managementMemory management
Memory managementcpjcollege
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process SchedulingDamian T. Gordon
 
Operating system components
Operating system componentsOperating system components
Operating system componentsSyed Zaid Irshad
 

Was ist angesagt? (20)

Kernel mode vs user mode in linux
Kernel mode vs user mode in linuxKernel mode vs user mode in linux
Kernel mode vs user mode in linux
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Memory management
Memory managementMemory management
Memory management
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introduction
 
Chapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatzChapter 10 Operating Systems silberschatz
Chapter 10 Operating Systems silberschatz
 
Unit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - ThreadsUnit II - 2 - Operating System - Threads
Unit II - 2 - Operating System - Threads
 
Ch1-Operating System Concept
Ch1-Operating System ConceptCh1-Operating System Concept
Ch1-Operating System Concept
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Memory management
Memory managementMemory management
Memory management
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process Scheduling
 
CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2CS6401 OPERATING SYSTEMS Unit 2
CS6401 OPERATING SYSTEMS Unit 2
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
Chapter 8 - Main Memory
Chapter 8 - Main MemoryChapter 8 - Main Memory
Chapter 8 - Main Memory
 
Operating system components
Operating system componentsOperating system components
Operating system components
 
Cs8493 unit 2
Cs8493 unit 2Cs8493 unit 2
Cs8493 unit 2
 

Ähnlich wie Operating Systems - "Chapter 4: Multithreaded Programming"

Ähnlich wie 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
 
Threads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.pptThreads semaphoremutexlocksdeadlockp.ppt
Threads semaphoremutexlocksdeadlockp.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
 
ch4.ppt
ch4.pptch4.ppt
ch4.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
 

Mehr von 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.pdfRa'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.pdfRa'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 ReportsRa'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 ApproachRa'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 variantsRa'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 CodeRa'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 DesignRa'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 documentationRa'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 managementRa'Fat Al-Msie'deen
 
Requirements change - requirements engineering
Requirements change - requirements engineeringRequirements change - requirements engineering
Requirements change - requirements engineeringRa'Fat Al-Msie'deen
 
Requirements validation - requirements engineering
Requirements validation - requirements engineeringRequirements validation - requirements engineering
Requirements validation - requirements engineeringRa'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 - referencesRa'Fat Al-Msie'deen
 

Mehr von 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"
 

Kürzlich hochgeladen

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Kürzlich hochgeladen (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

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