SlideShare a Scribd company logo
1 of 21
Threads
in operating systems
By
Syeda Farheen Naz
Haji Gul
Sajid
Mudassir
What are Threads?
• Thread is an execution unit which consists of its own program
counter, a stack, and a set of registers. Threads are also known
as Lightweight processes.
• Threads are popular way to improve application through
parallelism.
• As each thread has its own independent resource for process
execution, multiple processes can be executed parallel by
increasing number of threads.
Why use Threads?
• Large multiprocessors need many computing entities (one per
CPU)
• Switching between processes incurs high overhead
• With threads, an application can avoid per-process overheads
– Thread creation, deletion, switching cheaper than processes
• Threads have full access to address space (easy sharing)
• Threads can execute in parallel on multiprocessors
Single Threaded Approaches
• A single execution path per process, in which the concept of a
thread is not recognized, is referred to as a single-threaded
approach
• MS-DOS is an example of an OS that supports a single user
process and a single thread.
• some versions of UNIX also supported this type of process.
Multithreaded Approaches
• A Java run-time environment is a system of one process with
multiple threads; Windows also support multiple multithreaded
processes
Concurrent Execution on a Single-core System
Parallel Execution on a Multi core System
Types of Thread
There are two types of threads :
1. User level Thread management done by user-level threads
library(Pthreads,Win32 threads, Java threads)
2. Kernel level Supported by the Kernel/OS(Linux ,Windows
XP/2000)
8
Advantages and disadvantages of ULT
Advantages
• Thread switching does not
involve the kernel: no
mode switching, therefore
fast
• Scheduling can be
application specific:
choose the best algorithm
for the situation.
• Can run on any OS. We
only need a thread library
Disadvantages
• Most system calls are
blocking for processes. So
all threads within a process
will be implicitly blocked
• The kernel can only assign
processors to processes.
Two threads within the
same process cannot run
simultaneously on two
processors
9
Advantages and disadvantages of KLT
Advantages
• The kernel can schedule
multiple threads of the same
process on multiple
processors
• Blocking at thread level, not
process level
– If a thread blocks, the
CPU can be assigned to
another thread in the
same process
• Even the kernel routines can
be multithreaded
Disadvantages
• Thread switching always
involves the kernel. This
means 2 mode switches
per thread switch
• So it is slower compared to
User Level Threads
– (But faster than a full
process switch)
10
Few Implementation
Libraries
PThreads
• 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
Windows Threads
• Implements the one-to-one mapping
• Each thread contains
– A thread id
– Register set
– Separate user and kernel stacks
– Private data storage area
11
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)
Java Threads
• Java threads may be created by:
– Extending Thread class
– Implementing the Run able interface
• Java threads are managed by the JVM.
Cooperative Threads
• Cooperative threads use non pre-emptive scheduling
• Advantages:
– Simple
• Scientific apps
• Disadvantages:
– For badly written code
• Scheduler gets invoked only when Yield is called
• A thread could yield the processor when it blocks for I/O
Non-Cooperative Threads
• No explicit control passing among threads
• Rely on a scheduler to decide which thread to run
• A thread can be pre-empted at any point
• Often called pre-emptive threads
• Most modern thread packages use this approach.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the
following strategies.
• Many-To-One Model
• One-To-One Model
• Many-To-Many Model
Many-To-One Model
• In the many-to-one model, many user-level threads are all
mapped onto a single kernel thread.
• Thread management is handled by the thread library in user space,
which is efficient in nature.
One-To-One Model
• The one-to-one model creates a separate kernel thread to
handle each and every user thread.
• Most implementations of this model place a limit on how
many threads can be created.
• Linux and Windows from 95 to XP implement the one-to-one
model for threads.
Many-To-Many Model
• The many-to-many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads,
combining the best features of the one-to-one and many-to-one
models.
• Users can create any number of the threads.
• Blocking the kernel system calls does not block the entire
process.
• Processes can be split across multiple processors.
Thread Life Cycle in JAVA
The Life cycle of thread in Java is controlled by JVM. The Java thread states are as
follows.
 New
The thread is in new state if you create an instance of Thread class but before
the invocation of start() method.
 Runnable
The thread is in runnable state after invocation of start() method, but the
thread scheduler has not selected it to be the running thread.
 Running
The thread is in running state if the thread scheduler has selected it.
 Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to
run.
 Terminated
A thread is terminated when run() method terminates.
Non
Runnable
(Bocked)
New
Runnable
Running
Terminated
Start()
Sleep, block on
I/O, suspend,
wait
Sleep done, I/O
complete,
resume, notify
Thread Control Block
• Thread Control Block (TCB) is a data structure in the operating system kernel
which contains thread-specific information needed to manage it. The TCB is "the
manifestation of a thread in an operating system".
• An example of information contained within a TCB is:
• Stack pointer: Points to thread's stack in the process
• Program counter
• State of the thread (running, ready, waiting, start, done)
• Thread's register values
• Pointer to the Process control block (PCB) of the process that the thread lives on
The Thread Control Block acts as a library of information about the threads in a
system. Specific information is stored in the thread control block highlighting
important information about each thread.
Improve Parallelism with Threads in Operating Systems

More Related Content

What's hot

Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPTQUONTRASOLUTIONS
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9myrajendra
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Round robin scheduling
Round robin schedulingRound robin scheduling
Round robin schedulingRaghav S
 
Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesVasavi College of Engg
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationShivam Mitra
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper classSimoniShah6
 
Multithreading
Multithreading Multithreading
Multithreading WafaQKhan
 

What's hot (20)

Data types
Data typesData types
Data types
 
Process and Threads in Linux - PPT
Process and Threads in Linux - PPTProcess and Threads in Linux - PPT
Process and Threads in Linux - PPT
 
Operating system services 9
Operating system services 9Operating system services 9
Operating system services 9
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Round robin scheduling
Round robin schedulingRound robin scheduling
Round robin scheduling
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
 
Threads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess CommunicationThreads in Operating System | Multithreading | Interprocess Communication
Threads in Operating System | Multithreading | Interprocess Communication
 
Threads
ThreadsThreads
Threads
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
Multithreading
Multithreading Multithreading
Multithreading
 
Pthread
PthreadPthread
Pthread
 

Similar to Improve Parallelism with Threads in Operating Systems

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
 
1 Multithreading basics.pptx
1 Multithreading basics.pptx1 Multithreading basics.pptx
1 Multithreading basics.pptxYojanaFegade
 
THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid noman zahid
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxpiyushlohia1857
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programmingAnyapuPranav
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threadsVaibhav Khanna
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptxsandhyakiran10
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptxmadan r
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading modelsanoopkrishna2
 

Similar to Improve Parallelism with Threads in Operating Systems (20)

Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
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
 
1 Multithreading basics.pptx
1 Multithreading basics.pptx1 Multithreading basics.pptx
1 Multithreading basics.pptx
 
THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptx
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
thread os.pptx
thread os.pptxthread os.pptx
thread os.pptx
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
Ch4 threads
Ch4   threadsCh4   threads
Ch4 threads
 
4 threads
4 threads4 threads
4 threads
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Threads
ThreadsThreads
Threads
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
 
OS Thr schd.ppt
OS Thr schd.pptOS Thr schd.ppt
OS Thr schd.ppt
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Java threading
Java threadingJava threading
Java threading
 
Multithreading models
Multithreading modelsMultithreading models
Multithreading models
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Improve Parallelism with Threads in Operating Systems

  • 1. Threads in operating systems By Syeda Farheen Naz Haji Gul Sajid Mudassir
  • 2. What are Threads? • Thread is an execution unit which consists of its own program counter, a stack, and a set of registers. Threads are also known as Lightweight processes. • Threads are popular way to improve application through parallelism. • As each thread has its own independent resource for process execution, multiple processes can be executed parallel by increasing number of threads.
  • 3. Why use Threads? • Large multiprocessors need many computing entities (one per CPU) • Switching between processes incurs high overhead • With threads, an application can avoid per-process overheads – Thread creation, deletion, switching cheaper than processes • Threads have full access to address space (easy sharing) • Threads can execute in parallel on multiprocessors
  • 4. Single Threaded Approaches • A single execution path per process, in which the concept of a thread is not recognized, is referred to as a single-threaded approach • MS-DOS is an example of an OS that supports a single user process and a single thread. • some versions of UNIX also supported this type of process.
  • 5. Multithreaded Approaches • A Java run-time environment is a system of one process with multiple threads; Windows also support multiple multithreaded processes
  • 6. Concurrent Execution on a Single-core System Parallel Execution on a Multi core System
  • 7. Types of Thread There are two types of threads : 1. User level Thread management done by user-level threads library(Pthreads,Win32 threads, Java threads) 2. Kernel level Supported by the Kernel/OS(Linux ,Windows XP/2000)
  • 8. 8 Advantages and disadvantages of ULT Advantages • Thread switching does not involve the kernel: no mode switching, therefore fast • Scheduling can be application specific: choose the best algorithm for the situation. • Can run on any OS. We only need a thread library Disadvantages • Most system calls are blocking for processes. So all threads within a process will be implicitly blocked • The kernel can only assign processors to processes. Two threads within the same process cannot run simultaneously on two processors
  • 9. 9 Advantages and disadvantages of KLT Advantages • The kernel can schedule multiple threads of the same process on multiple processors • Blocking at thread level, not process level – If a thread blocks, the CPU can be assigned to another thread in the same process • Even the kernel routines can be multithreaded Disadvantages • Thread switching always involves the kernel. This means 2 mode switches per thread switch • So it is slower compared to User Level Threads – (But faster than a full process switch)
  • 10. 10 Few Implementation Libraries PThreads • 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 Windows Threads • Implements the one-to-one mapping • Each thread contains – A thread id – Register set – Separate user and kernel stacks – Private data storage area
  • 11. 11 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) Java Threads • Java threads may be created by: – Extending Thread class – Implementing the Run able interface • Java threads are managed by the JVM.
  • 12. Cooperative Threads • Cooperative threads use non pre-emptive scheduling • Advantages: – Simple • Scientific apps • Disadvantages: – For badly written code • Scheduler gets invoked only when Yield is called • A thread could yield the processor when it blocks for I/O
  • 13. Non-Cooperative Threads • No explicit control passing among threads • Rely on a scheduler to decide which thread to run • A thread can be pre-empted at any point • Often called pre-emptive threads • Most modern thread packages use this approach.
  • 14. Multithreading Models The user threads must be mapped to kernel threads, by one of the following strategies. • Many-To-One Model • One-To-One Model • Many-To-Many Model
  • 15. Many-To-One Model • In the many-to-one model, many user-level threads are all mapped onto a single kernel thread. • Thread management is handled by the thread library in user space, which is efficient in nature.
  • 16. One-To-One Model • The one-to-one model creates a separate kernel thread to handle each and every user thread. • Most implementations of this model place a limit on how many threads can be created. • Linux and Windows from 95 to XP implement the one-to-one model for threads.
  • 17. Many-To-Many Model • The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads, combining the best features of the one-to-one and many-to-one models. • Users can create any number of the threads. • Blocking the kernel system calls does not block the entire process. • Processes can be split across multiple processors.
  • 18. Thread Life Cycle in JAVA The Life cycle of thread in Java is controlled by JVM. The Java thread states are as follows.  New The thread is in new state if you create an instance of Thread class but before the invocation of start() method.  Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.  Running The thread is in running state if the thread scheduler has selected it.  Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run.  Terminated A thread is terminated when run() method terminates.
  • 19. Non Runnable (Bocked) New Runnable Running Terminated Start() Sleep, block on I/O, suspend, wait Sleep done, I/O complete, resume, notify
  • 20. Thread Control Block • Thread Control Block (TCB) is a data structure in the operating system kernel which contains thread-specific information needed to manage it. The TCB is "the manifestation of a thread in an operating system". • An example of information contained within a TCB is: • Stack pointer: Points to thread's stack in the process • Program counter • State of the thread (running, ready, waiting, start, done) • Thread's register values • Pointer to the Process control block (PCB) of the process that the thread lives on The Thread Control Block acts as a library of information about the threads in a system. Specific information is stored in the thread control block highlighting important information about each thread.