SlideShare a Scribd company logo
1 of 31
Threads
Instructor: MOHAMMAD NAZIR
Agenda for today
• Processes vs. Threads
• User vs. Kernel Threads
• Multithreading Models
• Threading Issues
• Pthreads
• Linux Threads
• Windows XP Threads
Process Overheads
• A full process includes numerous things:
– an address space (defining all the code and
data pages)
– OS resources and accounting information
– a “thread of control”,
• defines where the process is currently
executing
• PC, registers, and stack
Creating a new process is costly
– all of the structures (e.g., page tables) that
must be allocated
Communicating between processes is costly
– most communication goes through the OS
Threads and Processes
• Most operating systems therefore support two entities:
– the process,
• which defines the address space and general process attributes
– the thread,
• which defines a sequential execution stream within a process
• A thread is bound to a single process.
– For each process, however, there may be many threads.
• Threads are the unit of scheduling
• Processes are containers in which threads execute
Single and Multithreaded Processes
Distinguishing Threads and Processes
• Makes it easier to support multithreaded applications
– Different from multiprocessing, multiprogramming, multitasking
• Concurrency (multithreading) is useful for:
– improving program structure
– Improving responsiveness
– handling concurrent events (e.g., web requests)
– building parallel programs
– sharing resources economically
– multiprocessor utilization
• Is multithreading useful even on a single processor?
Threads vs. Processes
• A thread has no data segment or heap
• A thread cannot live on its own, it
must live within a process
• There can be more than one thread in
a process, the first thread calls main &
has the process’s stack
• Inexpensive creation
• Inexpensive context switching
• If a thread dies, its stack is reclaimed
• A process has code/data/heap &
other segments
• A process has at least one thread
• Threads within a process share
code/data/heap, share I/O, but
each has its own stack & registers
• Expensive creation
• Expensive context switching
• If a process dies, its resources are
reclaimed & all threads die
User and Kernel Threads
• User threads - Thread management done
by user-level threads library, with or
without knowledge of the kernel
• Kernel threads - Threads directly
supported by the kernel:
– Windows XP/2000
– Solaris
– Linux
– Tru64 UNIX
– Mac OS X
(a) A user-level threads package. (b) A threads
package managed by the kernel.
User vs. Kernel Threads
Example from Tanenbaum, Modern Operating Systems 3 e,
(c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Kernel Threads
• Also called Lightweight Processes (LWP)
• Kernel threads still suffer from performance
problems
• Operations on kernel threads are slow because:
– a thread operation still requires a system call
– kernel threads may be overly general
• to support needs of different users, languages, etc.
– the kernel doesn’t trust the user
• there must be lots of checking on kernel calls
User-Level Threads
• For speed, implement threads at the user level
• A user-level thread is managed by the run-time system
– user-level code that is linked with your program
• Each thread is represented simply by:
– PC
– Registers
– Stack
– Small control block
• All thread operations are at the user-level:
– Creating a new thread
– switching between threads
– synchronizing between threads
User-Level Threads
• User-level threads
– the thread scheduler is part of a library, outside the kernel
– thread context switching and scheduling is done by the library
– Can either use cooperative or pre-emptive threads
• cooperative threads are implemented by:
– CreateThread(), DestroyThread(), Yield(), Suspend(), etc.
• pre-emptive threads are implemented with a timer (signal)
– where the timer handler decides which thread to run next
User-Level vs. Kernel Threads
User-Level
• Managed by application
• Kernel not aware of thread
• Context switching cheap
• Can create many more
• Must be used with care
Kernel-Level
• Managed by kernel
• Consumes kernel resources
• Context switching expensive
• Number limited by kernel resources
• Simpler to use
Key issue: kernel threads provide virtual processors to user-level threads,
but if all of kernel threads block, then all user-level threads will
block even if the program logic allows them to proceed
Multiplexing User-Level Threads
• The user-level thread package sees a “virtual” processor(s)
– it schedules user-level threads on these virtual processors
– each “virtual” processor is implemented by a kernel thread
• The big picture:
– Create as many kernel threads as there are processors
– Create as many user-level threads as the application needs
– Multiplex user-level threads on top of the kernel-level threads
• Why not just create as many kernel-level threads as app needs?
– Context switching
– Resources
Multithreading Models
Mapping user threads to kernel
threads:
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One
• Many user-level threads
mapped to single kernel thread
– Fast - no system calls
required
– Few system dependencies;
portable
– No parallel execution of
threads - can’t exploit
multiple CPUs
– All threads block when one
uses synchronous I/O
• Examples:
– Solaris Green Threads
– GNU Portable Threads
One-to-One
• Each user-level thread maps to kernel thread
– More concurrency
– Better multiprocessor performance
– Each user thread requires creation of kernel thread
– Each thread requires kernel resources; limits number of total
threads
• Examples
– Windows NT/XP/2000
– Linux
– Solaris 9 and later
Many-to-Many Model
 Allows many user level
threads to be mapped to
many kernel threads
 Allows the operating
system to create a
sufficient number of kernel
threads
 If U < L, no benefits of
multithreading
 If U > L, some threads
may have to wait for an
LWP to run
 Examples:
 Solaris prior to version 9
 Windows NT/2000 with
the ThreadFiber package
Two-level Model
 Similar to M:M,
except that a user
thread may be
bound to kernel
thread
 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and
earlier
Multithreading Issues
• Semantics of fork() and exec() system calls
– Does fork() duplicate only the calling thread or all threads?
• Thread cancellation
– Asynchronous vs. Deferred Cancellation
• Signal handling
– Which thread to deliver it to?
• Thread pools
– Creating new threads, unlimited number of threads
• Scheduler activations
– Maintaining the correct number of scheduler threads
Thread Cancellation
• Terminating a thread before it has
finished
• Two general approaches:
– Asynchronous cancellation terminates
the target thread immediately
– Deferred cancellation allows the target
thread to periodically check if it should
be cancelled
Signal 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
• 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 threa to receive all signals for the
process
Thread Pools
• Create a number of threads in a pool
where they await work
• Advantages:
– 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
Scheduler Activations
• Both M:M and Two-level models require communication to
maintain the appropriate number of kernel threads
allocated to the application
• Scheduler activations provide upcalls - a communication
mechanism from the kernel to the thread library
• This communication allows an application to maintain the
correct number kernel threads
Pthreads (POSIX Threads)
• A POSIX standard (IEEE 1003.1c) API
for thread creation and
synchronization
• API specifies behavior of the thread
library, implementation is up to
development of the library
• Common in UNIX operating systems
(Solaris, Linux, Mac OS X)
Some of the Pthreads function calls.
Example: POSIX Threads (1)
Example from Tanenbaum, Modern Operating Systems 3 e,
(c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
An example program using threads.
POSIX Threads (2)
. . .
Example from Tanenbaum, Modern Operating Systems 3 e,
(c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Figure 2-15. An example program using threads.
POSIX Threads (3)
. . .
Example from Tanenbaum, Modern Operating Systems 3 e,
(c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Linux Threads
• Linux refers to them as tasks
rather than threads
• Thread creation is done through
clone() system call
• clone() allows a child task to
share the address space of the
parent task (process)
Windows XP Threads
• Implements the one-to-one mapping
• Each thread contains
– A thread id
– Register set
– Separate user and kernel stacks
– Private data storage area
• The register set, stacks, and private
storage area are known as the
context of the threads
ThanX…

More Related Content

What's hot

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Thread scheduling in Operating Systems
Thread scheduling in Operating SystemsThread scheduling in Operating Systems
Thread scheduling in Operating SystemsNitish Gulati
 
Operating system support in distributed system
Operating system support in distributed systemOperating system support in distributed system
Operating system support in distributed systemishapadhy
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)Prakhar Maurya
 
Thread management
Thread management Thread management
Thread management Ayaan Adeel
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threadingAntonio Cesarano
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingguesta40f80
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading modelsanoopkrishna2
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiectureHaris456
 
What is simultaneous multithreading
What is simultaneous multithreadingWhat is simultaneous multithreading
What is simultaneous multithreadingFraboni Ec
 
Parallel Computing - Lec 3
Parallel Computing - Lec 3Parallel Computing - Lec 3
Parallel Computing - Lec 3Shah Zaib
 
Multithreading
Multithreading Multithreading
Multithreading WafaQKhan
 
Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture Haris456
 
Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Akhil Nadh PC
 
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Kasun Gajasinghe
 

What's hot (20)

OS_Ch5
OS_Ch5OS_Ch5
OS_Ch5
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Thread scheduling in Operating Systems
Thread scheduling in Operating SystemsThread scheduling in Operating Systems
Thread scheduling in Operating Systems
 
Operating system support in distributed system
Operating system support in distributed systemOperating system support in distributed system
Operating system support in distributed system
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Thread management
Thread management Thread management
Thread management
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Thread
ThreadThread
Thread
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
 
Graphics processing uni computer archiecture
Graphics processing uni computer archiectureGraphics processing uni computer archiecture
Graphics processing uni computer archiecture
 
Os
OsOs
Os
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Pthread
PthreadPthread
Pthread
 
What is simultaneous multithreading
What is simultaneous multithreadingWhat is simultaneous multithreading
What is simultaneous multithreading
 
Parallel Computing - Lec 3
Parallel Computing - Lec 3Parallel Computing - Lec 3
Parallel Computing - Lec 3
 
Multithreading
Multithreading Multithreading
Multithreading
 
Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
 
Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]Chorus - Distributed Operating System [ case study ]
Chorus - Distributed Operating System [ case study ]
 
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
 

Similar to Ch04 threads

Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxAmanuelmergia
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
Operating system 21 multithreading models
Operating system 21 multithreading modelsOperating system 21 multithreading models
Operating system 21 multithreading modelsVaibhav Khanna
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.pptshreesha16
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdfAmanuelmergia
 
Operating system 22 threading issues
Operating system 22 threading issuesOperating system 22 threading issues
Operating system 22 threading issuesVaibhav Khanna
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threadsVaibhav Khanna
 
UNIT II DIS.pptx
UNIT II DIS.pptxUNIT II DIS.pptx
UNIT II DIS.pptxSamPrem3
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
chapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptchapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptaakarshsiwani1
 
Operating system: threads(mulithreading,benefits of threads, types of thread)
Operating system: threads(mulithreading,benefits of threads, types of thread)Operating system: threads(mulithreading,benefits of threads, types of thread)
Operating system: threads(mulithreading,benefits of threads, types of thread)sonuu__
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.pptLuis Goldster
 

Similar to Ch04 threads (20)

Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Operating system 21 multithreading models
Operating system 21 multithreading modelsOperating system 21 multithreading models
Operating system 21 multithreading models
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.ppt
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdf
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
 
Operating system 22 threading issues
Operating system 22 threading issuesOperating system 22 threading issues
Operating system 22 threading issues
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
CH04.pdf
CH04.pdfCH04.pdf
CH04.pdf
 
UNIT II DIS.pptx
UNIT II DIS.pptxUNIT II DIS.pptx
UNIT II DIS.pptx
 
OS Thr schd.ppt
OS Thr schd.pptOS Thr schd.ppt
OS Thr schd.ppt
 
Unit 2(oss) (1)
Unit 2(oss) (1)Unit 2(oss) (1)
Unit 2(oss) (1)
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
chapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.pptchapter4-processes nd processors in DS.ppt
chapter4-processes nd processors in DS.ppt
 
Operating system: threads(mulithreading,benefits of threads, types of thread)
Operating system: threads(mulithreading,benefits of threads, types of thread)Operating system: threads(mulithreading,benefits of threads, types of thread)
Operating system: threads(mulithreading,benefits of threads, types of thread)
 
Multithreading models.ppt
Multithreading models.pptMultithreading models.ppt
Multithreading models.ppt
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 

More from Nazir Ahmed

Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
 
Data structures lecture 04
Data structures  lecture 04Data structures  lecture 04
Data structures lecture 04Nazir Ahmed
 
Control unit design(1)
Control unit design(1)Control unit design(1)
Control unit design(1)Nazir Ahmed
 
Computer architecture mcq (2)
Computer architecture mcq (2)Computer architecture mcq (2)
Computer architecture mcq (2)Nazir Ahmed
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heapNazir Ahmed
 
Clamper clipper(10)
Clamper clipper(10)Clamper clipper(10)
Clamper clipper(10)Nazir Ahmed
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sqlNazir Ahmed
 
Chapter # 12 er modeling
Chapter # 12 er modelingChapter # 12 er modeling
Chapter # 12 er modelingNazir Ahmed
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-schedulingNazir Ahmed
 

More from Nazir Ahmed (20)

Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
 
Data structures lecture 04
Data structures  lecture 04Data structures  lecture 04
Data structures lecture 04
 
Data structure
Data structureData structure
Data structure
 
Cover letter
Cover letterCover letter
Cover letter
 
Control unit design(1)
Control unit design(1)Control unit design(1)
Control unit design(1)
 
Computer architecture mcq (2)
Computer architecture mcq (2)Computer architecture mcq (2)
Computer architecture mcq (2)
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Clamper clipper(10)
Clamper clipper(10)Clamper clipper(10)
Clamper clipper(10)
 
Cisc mc68000
Cisc mc68000Cisc mc68000
Cisc mc68000
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter # 12 er modeling
Chapter # 12 er modelingChapter # 12 er modeling
Chapter # 12 er modeling
 
Chapeter 2
Chapeter 2Chapeter 2
Chapeter 2
 
Ch7 1 v1
Ch7 1 v1Ch7 1 v1
Ch7 1 v1
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-scheduling
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
 
Ch1 v1
Ch1 v1Ch1 v1
Ch1 v1
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
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
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

Ch04 threads

  • 2. Agenda for today • Processes vs. Threads • User vs. Kernel Threads • Multithreading Models • Threading Issues • Pthreads • Linux Threads • Windows XP Threads
  • 3. Process Overheads • A full process includes numerous things: – an address space (defining all the code and data pages) – OS resources and accounting information – a “thread of control”, • defines where the process is currently executing • PC, registers, and stack Creating a new process is costly – all of the structures (e.g., page tables) that must be allocated Communicating between processes is costly – most communication goes through the OS
  • 4. Threads and Processes • Most operating systems therefore support two entities: – the process, • which defines the address space and general process attributes – the thread, • which defines a sequential execution stream within a process • A thread is bound to a single process. – For each process, however, there may be many threads. • Threads are the unit of scheduling • Processes are containers in which threads execute
  • 6. Distinguishing Threads and Processes • Makes it easier to support multithreaded applications – Different from multiprocessing, multiprogramming, multitasking • Concurrency (multithreading) is useful for: – improving program structure – Improving responsiveness – handling concurrent events (e.g., web requests) – building parallel programs – sharing resources economically – multiprocessor utilization • Is multithreading useful even on a single processor?
  • 7. Threads vs. Processes • A thread has no data segment or heap • A thread cannot live on its own, it must live within a process • There can be more than one thread in a process, the first thread calls main & has the process’s stack • Inexpensive creation • Inexpensive context switching • If a thread dies, its stack is reclaimed • A process has code/data/heap & other segments • A process has at least one thread • Threads within a process share code/data/heap, share I/O, but each has its own stack & registers • Expensive creation • Expensive context switching • If a process dies, its resources are reclaimed & all threads die
  • 8. User and Kernel Threads • User threads - Thread management done by user-level threads library, with or without knowledge of the kernel • Kernel threads - Threads directly supported by the kernel: – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X
  • 9. (a) A user-level threads package. (b) A threads package managed by the kernel. User vs. Kernel Threads Example from Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
  • 10. Kernel Threads • Also called Lightweight Processes (LWP) • Kernel threads still suffer from performance problems • Operations on kernel threads are slow because: – a thread operation still requires a system call – kernel threads may be overly general • to support needs of different users, languages, etc. – the kernel doesn’t trust the user • there must be lots of checking on kernel calls
  • 11. User-Level Threads • For speed, implement threads at the user level • A user-level thread is managed by the run-time system – user-level code that is linked with your program • Each thread is represented simply by: – PC – Registers – Stack – Small control block • All thread operations are at the user-level: – Creating a new thread – switching between threads – synchronizing between threads
  • 12. User-Level Threads • User-level threads – the thread scheduler is part of a library, outside the kernel – thread context switching and scheduling is done by the library – Can either use cooperative or pre-emptive threads • cooperative threads are implemented by: – CreateThread(), DestroyThread(), Yield(), Suspend(), etc. • pre-emptive threads are implemented with a timer (signal) – where the timer handler decides which thread to run next
  • 13. User-Level vs. Kernel Threads User-Level • Managed by application • Kernel not aware of thread • Context switching cheap • Can create many more • Must be used with care Kernel-Level • Managed by kernel • Consumes kernel resources • Context switching expensive • Number limited by kernel resources • Simpler to use Key issue: kernel threads provide virtual processors to user-level threads, but if all of kernel threads block, then all user-level threads will block even if the program logic allows them to proceed
  • 14. Multiplexing User-Level Threads • The user-level thread package sees a “virtual” processor(s) – it schedules user-level threads on these virtual processors – each “virtual” processor is implemented by a kernel thread • The big picture: – Create as many kernel threads as there are processors – Create as many user-level threads as the application needs – Multiplex user-level threads on top of the kernel-level threads • Why not just create as many kernel-level threads as app needs? – Context switching – Resources
  • 15. Multithreading Models Mapping user threads to kernel threads: • Many-to-One • One-to-One • Many-to-Many
  • 16. Many-to-One • Many user-level threads mapped to single kernel thread – Fast - no system calls required – Few system dependencies; portable – No parallel execution of threads - can’t exploit multiple CPUs – All threads block when one uses synchronous I/O • Examples: – Solaris Green Threads – GNU Portable Threads
  • 17. One-to-One • Each user-level thread maps to kernel thread – More concurrency – Better multiprocessor performance – Each user thread requires creation of kernel thread – Each thread requires kernel resources; limits number of total threads • Examples – Windows NT/XP/2000 – Linux – Solaris 9 and later
  • 18. Many-to-Many Model  Allows many user level threads to be mapped to many kernel threads  Allows the operating system to create a sufficient number of kernel threads  If U < L, no benefits of multithreading  If U > L, some threads may have to wait for an LWP to run  Examples:  Solaris prior to version 9  Windows NT/2000 with the ThreadFiber package
  • 19. Two-level Model  Similar to M:M, except that a user thread may be bound to kernel thread  Examples  IRIX  HP-UX  Tru64 UNIX  Solaris 8 and earlier
  • 20. Multithreading Issues • Semantics of fork() and exec() system calls – Does fork() duplicate only the calling thread or all threads? • Thread cancellation – Asynchronous vs. Deferred Cancellation • Signal handling – Which thread to deliver it to? • Thread pools – Creating new threads, unlimited number of threads • Scheduler activations – Maintaining the correct number of scheduler threads
  • 21. Thread Cancellation • Terminating a thread before it has finished • Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled
  • 22. Signal 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 • 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 threa to receive all signals for the process
  • 23. Thread Pools • Create a number of threads in a pool where they await work • Advantages: – 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
  • 24. Scheduler Activations • Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application • Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library • This communication allows an application to maintain the correct number kernel threads
  • 25. Pthreads (POSIX Threads) • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • API specifies behavior of the thread library, implementation is up to development of the library • Common in UNIX operating systems (Solaris, Linux, Mac OS X)
  • 26. Some of the Pthreads function calls. Example: POSIX Threads (1) Example from Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
  • 27. An example program using threads. POSIX Threads (2) . . . Example from Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
  • 28. Figure 2-15. An example program using threads. POSIX Threads (3) . . . Example from Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
  • 29. Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • clone() allows a child task to share the address space of the parent task (process)
  • 30. Windows XP Threads • Implements the one-to-one mapping • Each thread contains – A thread id – Register set – Separate user and kernel stacks – Private data storage area • The register set, stacks, and private storage area are known as the context of the threads