SlideShare une entreprise Scribd logo
1  sur  34
Rushdi Shams, Dept of CSE, KUET 1
The Cake Baking Analogy
 The computer scientist baking the cake
has the ingredients (data) and recipe
(algorithm).
 Now, he needs to blend the egg
 Then that blended egg will be mixed up
with the dough made by flour
 The dough mixed with blended egg will
then be placed to syrup.
Rushdi Shams, Dept of CSE, KUET 2
The Cake Baking Analogy
 Are they dependent or independent of each
other?
- Dependent
 Are they verbs? I mean are they executing
something?
- Of course, making something is always a verb
 Are they sharing some common place?
- mmm, that can be tricky! I can say they are
sharing common place if the mixing up of
blended egg with the dough takes place in the
same bowl! 
Rushdi Shams, Dept of CSE, KUET 3
The Cake Baking Analogy
 So, blending egg is a process, mixing it
up with dough is a process and mixing
the dough with yummy syrup is a
process.
 But they are dependent on each other.
 We will thus call them Threads
 And threads share some common
resources that processes don’t!
Rushdi Shams, Dept of CSE, KUET 4
Threads
 Because threads have some of the
properties of processes, they are
sometimes called lightweight
processes
 A thread comprises of
1. Thread ID
2. Program Counter (PC)
3. Register Set
4. Stack
Rushdi Shams, Dept of CSE, KUET 5
Threads
 Threads share its code section and
data section and other operating
systems resources with other threads
belonging to the same process
Rushdi Shams, Dept of CSE, KUET 6
Thread Property
Rushdi Shams, Dept of CSE, KUET 7
Rushdi Shams, Dept of CSE, KUET 8
Threads vs Processes
Similarities
Rushdi Shams, Dept of CSE, KUET 9
Threads vs Processes
Dissimilarities
Rushdi Shams, Dept of CSE, KUET 10
Why Threads?
 A web browser has a thread to display
contents and another thread that serves
you as you click Download button
 MS Word has a thread that displays the
content area, another thread that reads
keystrokes from the keyboard and the
third checks the grammar and spelling in
background
Rushdi Shams, Dept of CSE, KUET 11
Why Threads?
 And of course process does the same
thing thread does! 
 A client poses 5 different requests (but
similar) to the web browser.
 The browser can create 5 different
processes to serve him
 But, what’s the wrong with this
architecture?
Rushdi Shams, Dept of CSE, KUET 12
Benefits of Threads
 Responsiveness
A multithreaded web browser allows user to
interact on several buttons with less overhead
 Resource Sharing
Have you ever heard any time anyone saying I
have plenty of resources, use as much as you
like?? 
Rushdi Shams, Dept of CSE, KUET 13
Benefits of Threads
 Economy
Allocating memory and resources for process
creation is costly. It is more economical to
create and switch between threads than
processes.
-Creating process is 30% slower than creating
threads
-Switching between processes is 5% slower
than switching between threads
Rushdi Shams, Dept of CSE, KUET 14
User Thread
 User threads are supported above the
kernel and implemented by a thread
library at user level
 This library provides support for thread
creation, scheduling, and management
 Does not need support from the kernel
 Kernel is unaware of user level threads
 If kernel is single threaded, any user
level thread performing system block
call will cause the entire process
blocked
Rushdi Shams, Dept of CSE, KUET 15
Kernel Thread
 Supported directly by the operating
system
 Slower to create and manage than user
threads
Rushdi Shams, Dept of CSE, KUET 16
Multithread Model
 Many systems support for both user and
kernel threads.
1. Many to one Model
 Many user level threads to one kernel
thread
 Thread management is done in user
space. So, efficient but the entire process
will block if any of the user threads call a
system block.
 Multiple threads are unable to run in
parallel
Rushdi Shams, Dept of CSE, KUET 17
Rushdi Shams, Dept of CSE, KUET 18
Multithread Model
2. One to one Model
 More concurrency than many to many
model by allowing another thread to
run when a thread makes a system
call.
 Creating user thread requires creating
corresponding kernel thread. And
creating kernel thread is expensive!
Rushdi Shams, Dept of CSE, KUET 19
Rushdi Shams, Dept of CSE, KUET 20
Multithread Model
3. Many to Many Model
 Multiplexes many user level threads to
a smaller or equal number of kernel
threads
Rushdi Shams, Dept of CSE, KUET 21
Rushdi Shams, Dept of CSE, KUET 22
Thread Cancellation
 Thread cancellation is a task of
terminating threads before their
completion
 OS creates 5 threads for a database
operation. 1 executes the query, 1
fetches the record and 1 displays. The 2
more threads are cancelled by the OS
Rushdi Shams, Dept of CSE, KUET 23
Thread Cancellation
 You are browsing through IE. Suppose,
3 threads are running. Suddenly you
press STOP button. All of them are
cancelled.
Rushdi Shams, Dept of CSE, KUET 24
Asynchronous Cancellation
 The thread that is to be cancelled is
called Target Thread
 In asynchronous cancellation, one
thread immediately cancel the target
thread
Rushdi Shams, Dept of CSE, KUET 25
Deferred Cancellation
 The target thread checks periodically
check if it should terminate, allowing the
target thread an opportunity to terminate
itself in an orderly fashion
Rushdi Shams, Dept of CSE, KUET 26
Problem in Cancellation
 Well, cancellation of thread means
simply withdrawing OS resources from
the thread. Sometimes, resources are
GIVEN to a thread and sometimes
resources are ALLOCATED to a thread.
When GIVEN, it is really a problem to
retrieve that.
 If a thread is in a mid of update, and
cancelled, lost of data may occur.
Rushdi Shams, Dept of CSE, KUET 27
Problem in Cancellation
 The problem is severe in case of
asynchronous cancellation. OS often
cannot retrieve all of its resources from
the cancelled thread.
 Most OS, however, do the
asynchronous thread cancellation! 
Rushdi Shams, Dept of CSE, KUET 28
Signal Handling
 A signal is used to notify a process that
a particular event has occurred. All
signals follow the pattern-
1. A signal is generated by the
occurrence of a particular event
2. A generated signal is delivered to a
process
3. Once delivered, the signal MUST be
handled.
Rushdi Shams, Dept of CSE, KUET 29
Types of Signals
 Synchronous Signal
A signal is generated and delivered to a
particular process if the occurrence is
caused by that process only
 Asynchronous Signal
A signal is generated and delivered to all
of the process created by the process
responsible for the event.
Rushdi Shams, Dept of CSE, KUET 30
Signal Handler
 Every signal has a default signal handler
run by kernel.
 User defined signal handler can
overtrump it if required.
 Some signals can be ignored
(maximizing the window), others can be
handled by terminating the program
(illegal memory access)
Rushdi Shams, Dept of CSE, KUET 31
Signal Delivery
 Delivery of a signal is simple in single
threaded programs (hand to hand delivery)
 In multithreaded environment, any one of the
following can be accomplished-
1. Deliver the signal to the thread to which the
signal applies
2. Deliver the signal to every thread of the
process
3. Deliver the signal to certain threads of the
process
4. Assign a specific thread to receive all signals
for processes.
Rushdi Shams, Dept of CSE, KUET 32
Thread Pool
 We said earlier that in web browser we can
serve the client faster by creating 5 threads
for 1 process (browsing) than by creating 5
processes (saving, downloading, clicking,
maximizing, minimizing)
 Okay, but still there is a problem- if you
create too many threads, that would again
embarrass our POOR OS!! 
 Too many threads can be just a little bit
reduction in servicing time than creating
independent processes
Rushdi Shams, Dept of CSE, KUET 33
Thread Pool
 That’s why OS creates a pool of threads
at start up
 While starting up, the OS puts, say, 50
threads in its pool (10 system threads,
10 browsing threads, 10 nitty bitty, 20
bla bla)
 Then it will invite those threads from the
pool when required.
Rushdi Shams, Dept of CSE, KUET 34

Contenu connexe

Similaire à Lecture 3 and 4 threads

Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java multithreading
Java multithreadingJava multithreading
Java multithreadingMohammed625
 
Networking threads
Networking threadsNetworking threads
Networking threadsNilesh Pawar
 
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating SystemsLecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating SystemsRushdi Shams
 
Parallel Processing Presentation2
Parallel Processing Presentation2Parallel Processing Presentation2
Parallel Processing Presentation2daniyalqureshi712
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSKathirvel Ayyaswamy
 
Distributed Computing & MapReduce
Distributed Computing & MapReduceDistributed Computing & MapReduce
Distributed Computing & MapReducecoolmirza143
 
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationLM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationMani Deepak Choudhry
 
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded SoftwareBeyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded SoftwareQuantum Leaps, LLC
 
Explain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfExplain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfezzi97
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESsuthi
 
Process Management Operating Systems .pptx
Process Management        Operating Systems .pptxProcess Management        Operating Systems .pptx
Process Management Operating Systems .pptxSAIKRISHNADURVASULA2
 
Types or evolution of operating system
Types or evolution of operating systemTypes or evolution of operating system
Types or evolution of operating systemEkta Bafna
 
Scalable Apache for Beginners
Scalable Apache for BeginnersScalable Apache for Beginners
Scalable Apache for Beginnerswebhostingguy
 
Operating system (OS) itself is a process, what approaches are there.pdf
Operating system (OS) itself is a process, what approaches are there.pdfOperating system (OS) itself is a process, what approaches are there.pdf
Operating system (OS) itself is a process, what approaches are there.pdfJUSTSTYLISH3B2MOHALI
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
 

Similaire à Lecture 3 and 4 threads (20)

Java
JavaJava
Java
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
multithreading
multithreadingmultithreading
multithreading
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating SystemsLecture 7, 8, 9 and 10  Inter Process Communication (IPC) in Operating Systems
Lecture 7, 8, 9 and 10 Inter Process Communication (IPC) in Operating Systems
 
Parallel Processing Presentation2
Parallel Processing Presentation2Parallel Processing Presentation2
Parallel Processing Presentation2
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Distributed Computing & MapReduce
Distributed Computing & MapReduceDistributed Computing & MapReduce
Distributed Computing & MapReduce
 
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncationLM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
LM9 - OPERATIONS, SCHEDULING, Inter process xommuncation
 
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded SoftwareBeyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Explain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdfExplain why multiple processes cannot share data easilySolution.pdf
Explain why multiple processes cannot share data easilySolution.pdf
 
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTESPARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
PARALLEL ARCHITECTURE AND COMPUTING - SHORT NOTES
 
Process Management Operating Systems .pptx
Process Management        Operating Systems .pptxProcess Management        Operating Systems .pptx
Process Management Operating Systems .pptx
 
Types or evolution of operating system
Types or evolution of operating systemTypes or evolution of operating system
Types or evolution of operating system
 
Scalable Apache for Beginners
Scalable Apache for BeginnersScalable Apache for Beginners
Scalable Apache for Beginners
 
Operating system (OS) itself is a process, what approaches are there.pdf
Operating system (OS) itself is a process, what approaches are there.pdfOperating system (OS) itself is a process, what approaches are there.pdf
Operating system (OS) itself is a process, what approaches are there.pdf
 
Assignment-01.pptx
Assignment-01.pptxAssignment-01.pptx
Assignment-01.pptx
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 

Plus de Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

Plus de Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Dernier

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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
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
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Dernier (20)

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"
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
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
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
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
 
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
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Lecture 3 and 4 threads

  • 1. Rushdi Shams, Dept of CSE, KUET 1
  • 2. The Cake Baking Analogy  The computer scientist baking the cake has the ingredients (data) and recipe (algorithm).  Now, he needs to blend the egg  Then that blended egg will be mixed up with the dough made by flour  The dough mixed with blended egg will then be placed to syrup. Rushdi Shams, Dept of CSE, KUET 2
  • 3. The Cake Baking Analogy  Are they dependent or independent of each other? - Dependent  Are they verbs? I mean are they executing something? - Of course, making something is always a verb  Are they sharing some common place? - mmm, that can be tricky! I can say they are sharing common place if the mixing up of blended egg with the dough takes place in the same bowl!  Rushdi Shams, Dept of CSE, KUET 3
  • 4. The Cake Baking Analogy  So, blending egg is a process, mixing it up with dough is a process and mixing the dough with yummy syrup is a process.  But they are dependent on each other.  We will thus call them Threads  And threads share some common resources that processes don’t! Rushdi Shams, Dept of CSE, KUET 4
  • 5. Threads  Because threads have some of the properties of processes, they are sometimes called lightweight processes  A thread comprises of 1. Thread ID 2. Program Counter (PC) 3. Register Set 4. Stack Rushdi Shams, Dept of CSE, KUET 5
  • 6. Threads  Threads share its code section and data section and other operating systems resources with other threads belonging to the same process Rushdi Shams, Dept of CSE, KUET 6
  • 7. Thread Property Rushdi Shams, Dept of CSE, KUET 7
  • 8. Rushdi Shams, Dept of CSE, KUET 8
  • 9. Threads vs Processes Similarities Rushdi Shams, Dept of CSE, KUET 9
  • 10. Threads vs Processes Dissimilarities Rushdi Shams, Dept of CSE, KUET 10
  • 11. Why Threads?  A web browser has a thread to display contents and another thread that serves you as you click Download button  MS Word has a thread that displays the content area, another thread that reads keystrokes from the keyboard and the third checks the grammar and spelling in background Rushdi Shams, Dept of CSE, KUET 11
  • 12. Why Threads?  And of course process does the same thing thread does!   A client poses 5 different requests (but similar) to the web browser.  The browser can create 5 different processes to serve him  But, what’s the wrong with this architecture? Rushdi Shams, Dept of CSE, KUET 12
  • 13. Benefits of Threads  Responsiveness A multithreaded web browser allows user to interact on several buttons with less overhead  Resource Sharing Have you ever heard any time anyone saying I have plenty of resources, use as much as you like??  Rushdi Shams, Dept of CSE, KUET 13
  • 14. Benefits of Threads  Economy Allocating memory and resources for process creation is costly. It is more economical to create and switch between threads than processes. -Creating process is 30% slower than creating threads -Switching between processes is 5% slower than switching between threads Rushdi Shams, Dept of CSE, KUET 14
  • 15. User Thread  User threads are supported above the kernel and implemented by a thread library at user level  This library provides support for thread creation, scheduling, and management  Does not need support from the kernel  Kernel is unaware of user level threads  If kernel is single threaded, any user level thread performing system block call will cause the entire process blocked Rushdi Shams, Dept of CSE, KUET 15
  • 16. Kernel Thread  Supported directly by the operating system  Slower to create and manage than user threads Rushdi Shams, Dept of CSE, KUET 16
  • 17. Multithread Model  Many systems support for both user and kernel threads. 1. Many to one Model  Many user level threads to one kernel thread  Thread management is done in user space. So, efficient but the entire process will block if any of the user threads call a system block.  Multiple threads are unable to run in parallel Rushdi Shams, Dept of CSE, KUET 17
  • 18. Rushdi Shams, Dept of CSE, KUET 18
  • 19. Multithread Model 2. One to one Model  More concurrency than many to many model by allowing another thread to run when a thread makes a system call.  Creating user thread requires creating corresponding kernel thread. And creating kernel thread is expensive! Rushdi Shams, Dept of CSE, KUET 19
  • 20. Rushdi Shams, Dept of CSE, KUET 20
  • 21. Multithread Model 3. Many to Many Model  Multiplexes many user level threads to a smaller or equal number of kernel threads Rushdi Shams, Dept of CSE, KUET 21
  • 22. Rushdi Shams, Dept of CSE, KUET 22
  • 23. Thread Cancellation  Thread cancellation is a task of terminating threads before their completion  OS creates 5 threads for a database operation. 1 executes the query, 1 fetches the record and 1 displays. The 2 more threads are cancelled by the OS Rushdi Shams, Dept of CSE, KUET 23
  • 24. Thread Cancellation  You are browsing through IE. Suppose, 3 threads are running. Suddenly you press STOP button. All of them are cancelled. Rushdi Shams, Dept of CSE, KUET 24
  • 25. Asynchronous Cancellation  The thread that is to be cancelled is called Target Thread  In asynchronous cancellation, one thread immediately cancel the target thread Rushdi Shams, Dept of CSE, KUET 25
  • 26. Deferred Cancellation  The target thread checks periodically check if it should terminate, allowing the target thread an opportunity to terminate itself in an orderly fashion Rushdi Shams, Dept of CSE, KUET 26
  • 27. Problem in Cancellation  Well, cancellation of thread means simply withdrawing OS resources from the thread. Sometimes, resources are GIVEN to a thread and sometimes resources are ALLOCATED to a thread. When GIVEN, it is really a problem to retrieve that.  If a thread is in a mid of update, and cancelled, lost of data may occur. Rushdi Shams, Dept of CSE, KUET 27
  • 28. Problem in Cancellation  The problem is severe in case of asynchronous cancellation. OS often cannot retrieve all of its resources from the cancelled thread.  Most OS, however, do the asynchronous thread cancellation!  Rushdi Shams, Dept of CSE, KUET 28
  • 29. Signal Handling  A signal is used to notify a process that a particular event has occurred. All signals follow the pattern- 1. A signal is generated by the occurrence of a particular event 2. A generated signal is delivered to a process 3. Once delivered, the signal MUST be handled. Rushdi Shams, Dept of CSE, KUET 29
  • 30. Types of Signals  Synchronous Signal A signal is generated and delivered to a particular process if the occurrence is caused by that process only  Asynchronous Signal A signal is generated and delivered to all of the process created by the process responsible for the event. Rushdi Shams, Dept of CSE, KUET 30
  • 31. Signal Handler  Every signal has a default signal handler run by kernel.  User defined signal handler can overtrump it if required.  Some signals can be ignored (maximizing the window), others can be handled by terminating the program (illegal memory access) Rushdi Shams, Dept of CSE, KUET 31
  • 32. Signal Delivery  Delivery of a signal is simple in single threaded programs (hand to hand delivery)  In multithreaded environment, any one of the following can be accomplished- 1. Deliver the signal to the thread to which the signal applies 2. Deliver the signal to every thread of the process 3. Deliver the signal to certain threads of the process 4. Assign a specific thread to receive all signals for processes. Rushdi Shams, Dept of CSE, KUET 32
  • 33. Thread Pool  We said earlier that in web browser we can serve the client faster by creating 5 threads for 1 process (browsing) than by creating 5 processes (saving, downloading, clicking, maximizing, minimizing)  Okay, but still there is a problem- if you create too many threads, that would again embarrass our POOR OS!!   Too many threads can be just a little bit reduction in servicing time than creating independent processes Rushdi Shams, Dept of CSE, KUET 33
  • 34. Thread Pool  That’s why OS creates a pool of threads at start up  While starting up, the OS puts, say, 50 threads in its pool (10 system threads, 10 browsing threads, 10 nitty bitty, 20 bla bla)  Then it will invite those threads from the pool when required. Rushdi Shams, Dept of CSE, KUET 34