SlideShare a Scribd company logo
1 of 26
Threads
Introduction
• What is a Thread?
• A thread is a flow of execution through the process code, with its own
program counter that keeps track of which instruction to execute next, system
registers which hold its current working variables, and a stack which contains
the execution history.
• A thread is a single sequence stream within in a process.
• Because threads have some of the properties of processes, they are
sometimes called lightweight processes.
• A thread shares with its peer threads few information like code segment, data
segment and open files.
• When one thread alters a code segment memory item, all other threads see
that.
Introduction ctd’
• Threads provide a way to improve application performance through
parallelism.
• The CPU switches rapidly back and forth among the threads giving illusion
that the threads are running in parallel.
• Threads represent a software approach to improving performance of
operating system by reducing the overhead thread is equivalent to a
classical process.
• Each thread belongs to exactly one process and no thread can exist outside
a process.
• Each thread represents a separate flow of control.
• Threads have been successfully used in implementing network servers and
web server.
• They also provide a suitable foundation for parallel execution of
applications on shared memory multiprocessors.
Introduction ctd’
• Like a traditional process i.e., process with one thread, a thread can be in
any of several states (Running, Blocked, Ready or Terminated).
• Each thread has its own stack since thread will generally call different
procedures and thus a different execution history. This is why thread needs
its own stack.
• An operating system that has thread facility, the basic unit of CPU
utilization is a thread.
• A thread has or consists of a program counter (PC), a register set, and a
stack space.
• Threads are not independent of one other like processes as a result threads
shares with other threads their code section, data section, OS resources
also known as task, such as open files and signals.
Single-threaded and a multithreaded process.
Processes Vs Threads
Threads operate in the same way as that of processes. Some of the similarities
and differences are:
Similarities
• Like processes, threads share CPU and only one thread active (running) at a
time.
• Like processes, threads within a process, execute sequentially.
• Like processes, thread can create children.
• And like process, if one thread is blocked, another thread can run.
Differences
• Unlike processes, threads are not independent of one another.
• Unlike processes, all threads can access every address in the task.
• Unlike processes, threads are designed to assist one another. Note that
processes might or might not assist one another because processes may
originate from different users.
Difference between Process and Thread
Advantages of Thread
• Threads minimize the context switching time.
• Use of threads provides concurrency within a process.
• Efficient communication.
• It is more economical to create and context switch threads.
• Threads allow utilization of multiprocessor architectures to a greater
scale and efficiency.
Why Threads?
• Following are some reasons why we use threads in designing operating systems.
A process with multiple threads makes a great server for example printer server.
Because threads can share common data, they do not need to use inter process
communication.
Because of the very nature, threads can take advantage of multiprocessors.
• Threads are cheap in the sense that
They only need a stack and storage for registers therefore, threads are cheap to
create.
Threads use very little resources of an operating system in which they are working.
That is, threads do not need new address space, global data, program code or
operating system resources.
Context switching are fast when working with threads. The reason is that we only
have to save and/or restore PC, SP and registers.
Types of Thread
• There are two types of threads :
i. User Threads
ii. Kernel Threads
User Level Threads
• In this case, the thread management kernel is not aware of the
existence of threads.
• These are user managed threads.
• The thread library contains code for creating and destroying threads,
for passing message and data between threads, for scheduling thread
execution and for saving and restoring thread contexts.
• The application starts with a single thread.
Advantages of User-level threads:
• The most obvious advantage of this technique is that a user-level threads package can
be implemented on an Operating System that does not support threads.
• User level threads are fast to create and manage.
• Simple Representation: Each thread is represented simply by a PC, registers, stack and
a small control block, all stored in the user process address space.
• Simple Management: This simply means that creating a thread, switching between
threads and synchronization between threads can all be done without intervention of
the kernel.
• Fast and Efficient: Thread switching is not much more expensive than a procedure call.
Disadvantages of user-level threads:
• There is a lack of coordination between threads and operating system kernel.
Therefore, process as whole gets one time slice irrespective of whether process has
one thread or 1000 threads within. It is up to each thread to relinquish control to
other threads.
• User-level threads require non-blocking systems call i.e., a multithreaded kernel.
Otherwise, entire process will blocked in the kernel, even if there are runnable
threads left in the processes. For example, if one thread causes a page fault, the
process blocks.
Kernel Level Threads
• In this case, thread management is done by the Kernel.
• There is no thread management code in the application area.
• Kernel threads are supported directly by the operating system.
• Any application can be programmed to be multithreaded.
• All of the threads within an application are supported within a single process.
• The Kernel maintains context information for the process as a whole and for
individuals threads within the process.
• Scheduling by the Kernel is done on a thread basis.
• The Kernel performs thread creation, scheduling and management in Kernel
space.
• Kernel threads are generally slower to create and manage than the user
threads.
• Advantages of kernel-level threads:
• Because kernel has full knowledge of all threads, Scheduler may decide to
give more time to a process having large number of threads than process
having small number of threads.
• Kernel can simultaneously schedule multiple threads from the same process
on multiple processes.
• Kernel-level threads are especially good for applications that frequently block
- If one thread in a process is blocked, the Kernel can schedule another
thread of the same process.
• Disadvantages of kernel level threads:
• The kernel-level threads are slow and inefficient. For instance, threads
operations are hundreds of times slower than that of user-level threads.
• Since kernel must manage and schedule threads as well as processes. It
requires a full thread control block (TCB) for each thread to maintain
information about threads. As a result there is significant overhead and
increased in kernel complexity.
Difference between User-Level & Kernel-Level
Thread
S.N. User-Level Threads Kernel-Level Thread
1 User-level threads are faster to create
and manage.
Kernel-level threads are slower to create
and manage.
2 Implementation is by a thread library at
the user level.
Operating system supports creation of
Kernel threads.
3 User-level thread is generic and can run
on any operating system.
Kernel-level thread is specific to the
operating system.
4 Multi-threaded applications cannot take
advantage of multiprocessing.
Kernel routines themselves can be
multithreaded.
Multithreading Models
• Some operating system provide a combined user level thread and Kernel
level thread facility e.g. Solaris is a good example of this combined
approach.
• In a combined system, multiple threads within the same application can
run in parallel on multiple processors and a blocking system call need not
block the entire process.
i. Many-To-One Model
ii. One-To-One Model
iii. Many-To-Many Model
Many-To-One Threading 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 very efficient.
• However, if a blocking system call is made, then the entire process
blocks, even if the other user threads would otherwise be able to
continue.
• Because a single kernel thread can operate only on a single CPU, the
many-to-one model does not allow individual processes to be split
across multiple CPUs.
• Green threads for Solaris and GNU Portable Threads implement the
many-to-one model in the past, but few systems continue to do so
today.
Many-to-one threading model diagram
One-To-One Threading Model
• The one-to-one model creates a separate kernel thread to handle each
user thread.
• One-to-one model overcomes the problems listed above involving blocking
system calls and the splitting of processes across multiple CPUs.
• However the overhead of managing the one-to-one model is more
significant, involving more overhead and slowing down the system.
• Most implementations of this model place a limit on how many threads
can be created.
• Linux and Windows from 95 implement the one-to-one model for threads.
One-to-one threading model diagram
Many-To-Many Threading 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 have no restrictions on the number of threads created.
• Blocking kernel system calls do not block the entire process.
• Processes can be split across multiple processors.
• Individual processes may be allocated variable numbers of kernel
threads, depending on the number of CPUs present and other factors.
Many-To-Many Threading Model diagram
Thread Libraries
• Thread libraries provides programmers with API for creating and managing of
threads.
• Thread libraries may be implemented either in user space or in kernel space.
• The user space involves API functions implemented solely within user space, with no
kernel support.
• The kernel space involves system calls, and requires a kernel with thread library
support.
Application that Benefits from Threads
• A proxy server satisfying the requests for a number of computers on a LAN would be
benefited by a multi-threaded process. In general, any program that has to do more
than one task at a time could benefit from multitasking. For example, a program that
reads input, process it, and outputs could have three threads, one for each task.
Application that cannot Benefit from Threads
• Any sequential process that cannot be divided into parallel task will not benefit from
thread, as they would block until the previous one completes. For example, a
program that displays the time of the day would not benefit from multiple threads.
Benefits of Multithreading
• Responsiveness
• Resource sharing, hence allowing better utilization of resources -
Threads allow the sharing of a lot resources that cannot be shared in
process, for example, sharing code section, data section, Operating
System resources like open file etc.
• Economy. Creating and managing threads becomes easier.
• Scalability. One thread runs on one CPU. In Multithreaded processes,
threads can be distributed over a series of processors to scale.
• Context Switching is smooth. Context switching refers to the
procedure followed by CPU to change from one task to another.
Resources used in Thread
Creation and Process Creation
• When a new thread is created it shares its code section, data section and
operating system resources like open files with other threads.
• But it is allocated its own stack, register set and a program counter.
• The creation of a new process differs from that of a thread mainly in the fact
that all the shared resources of a thread are needed explicitly for each process.
• So though two processes may be running the same piece of code they need to
have their own copy of the code in the main memory to be able to run.
• Two processes also do not share other resources with each other.
• This makes the creation of a new process very costly compared to that of a
new thread.
Multithreading Issues
• Thread Cancellation - Thread cancellation means terminating a thread before it has
finished working - are no longer needed. There can be two approaches for this, one
is Asynchronous cancellation, which terminates the target thread immediately. The
other is 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. Now in when a Multithreaded process receives a signal, to which
thread it must be delivered?
• There are four major options: a) Deliver the signal to the thread to which the signal
applies, b) Deliver the signal to every thread in the process, c) Deliver the signal to
certain threads in the process, d) Assign a specific thread to receive all signals in a
process.
• fork() System Call - fork() is a system call executed in the kernel through which a
process creates a copy of itself. Now the problem in Multithreaded process is, if one
thread forks, will the entire process be copied or not? The answers included a0 its
system dependent, b) If the new process execs right away, there is no need to copy all
the other threads. If it doesn't, then the entire process should be copied, c) A: Many
versions of UNIX provide multiple versions of the fork call for this purpose.
• Security Issues because of extensive sharing of resources between multiple threads.

More Related Content

What's hot

Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory managementrprajat007
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSKathirvel Ayyaswamy
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecturePankaj Kumar Jain
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
distributed Computing system model
distributed Computing system modeldistributed Computing system model
distributed Computing system modelHarshad Umredkar
 
OS Components and Structure
OS Components and StructureOS Components and Structure
OS Components and Structuresathish sak
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed systemSunita Sahu
 
Distributed File Systems
Distributed File Systems Distributed File Systems
Distributed File Systems Maurvi04
 
Lamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionLamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionNeelamani Samal
 
Agreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemoryAgreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemorySHIKHA GAUTAM
 
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency controlBinte fatima
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 

What's hot (20)

Operating system memory management
Operating system memory managementOperating system memory management
Operating system memory management
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
CS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMSCS9222 ADVANCED OPERATING SYSTEMS
CS9222 ADVANCED OPERATING SYSTEMS
 
Memory management
Memory managementMemory management
Memory management
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecture
 
Replication in Distributed Systems
Replication in Distributed SystemsReplication in Distributed Systems
Replication in Distributed Systems
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
distributed Computing system model
distributed Computing system modeldistributed Computing system model
distributed Computing system model
 
Mainframe systems
Mainframe systemsMainframe systems
Mainframe systems
 
OS Components and Structure
OS Components and StructureOS Components and Structure
OS Components and Structure
 
Clock synchronization in distributed system
Clock synchronization in distributed systemClock synchronization in distributed system
Clock synchronization in distributed system
 
Distributed File Systems
Distributed File Systems Distributed File Systems
Distributed File Systems
 
Lamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionLamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusion
 
Trends in distributed systems
Trends in distributed systemsTrends in distributed systems
Trends in distributed systems
 
Agreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared MemoryAgreement Protocols, distributed File Systems, Distributed Shared Memory
Agreement Protocols, distributed File Systems, Distributed Shared Memory
 
Distributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithmsDistributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithms
 
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency control
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 

Similar to Lecture 3 threads

Similar to Lecture 3 threads (20)

Threads
ThreadsThreads
Threads
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptx
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Thread
ThreadThread
Thread
 
W-9.pptx
W-9.pptxW-9.pptx
W-9.pptx
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Thread
ThreadThread
Thread
 
Threads
ThreadsThreads
Threads
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdfLecture 3- Threads.pdf
Lecture 3- Threads.pdf
 
thread os.pptx
thread os.pptxthread os.pptx
thread os.pptx
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
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
ThreadsThreads
Threads
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Threads ppt
Threads pptThreads ppt
Threads ppt
 

Recently uploaded

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 

Recently uploaded (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 

Lecture 3 threads

  • 2. Introduction • What is a Thread? • A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history. • A thread is a single sequence stream within in a process. • Because threads have some of the properties of processes, they are sometimes called lightweight processes. • A thread shares with its peer threads few information like code segment, data segment and open files. • When one thread alters a code segment memory item, all other threads see that.
  • 3. Introduction ctd’ • Threads provide a way to improve application performance through parallelism. • The CPU switches rapidly back and forth among the threads giving illusion that the threads are running in parallel. • Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process. • Each thread belongs to exactly one process and no thread can exist outside a process. • Each thread represents a separate flow of control. • Threads have been successfully used in implementing network servers and web server. • They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors.
  • 4. Introduction ctd’ • Like a traditional process i.e., process with one thread, a thread can be in any of several states (Running, Blocked, Ready or Terminated). • Each thread has its own stack since thread will generally call different procedures and thus a different execution history. This is why thread needs its own stack. • An operating system that has thread facility, the basic unit of CPU utilization is a thread. • A thread has or consists of a program counter (PC), a register set, and a stack space. • Threads are not independent of one other like processes as a result threads shares with other threads their code section, data section, OS resources also known as task, such as open files and signals.
  • 5. Single-threaded and a multithreaded process.
  • 6. Processes Vs Threads Threads operate in the same way as that of processes. Some of the similarities and differences are: Similarities • Like processes, threads share CPU and only one thread active (running) at a time. • Like processes, threads within a process, execute sequentially. • Like processes, thread can create children. • And like process, if one thread is blocked, another thread can run. Differences • Unlike processes, threads are not independent of one another. • Unlike processes, all threads can access every address in the task. • Unlike processes, threads are designed to assist one another. Note that processes might or might not assist one another because processes may originate from different users.
  • 8. Advantages of Thread • Threads minimize the context switching time. • Use of threads provides concurrency within a process. • Efficient communication. • It is more economical to create and context switch threads. • Threads allow utilization of multiprocessor architectures to a greater scale and efficiency.
  • 9. Why Threads? • Following are some reasons why we use threads in designing operating systems. A process with multiple threads makes a great server for example printer server. Because threads can share common data, they do not need to use inter process communication. Because of the very nature, threads can take advantage of multiprocessors. • Threads are cheap in the sense that They only need a stack and storage for registers therefore, threads are cheap to create. Threads use very little resources of an operating system in which they are working. That is, threads do not need new address space, global data, program code or operating system resources. Context switching are fast when working with threads. The reason is that we only have to save and/or restore PC, SP and registers.
  • 10. Types of Thread • There are two types of threads : i. User Threads ii. Kernel Threads
  • 11. User Level Threads • In this case, the thread management kernel is not aware of the existence of threads. • These are user managed threads. • The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts. • The application starts with a single thread.
  • 12. Advantages of User-level threads: • The most obvious advantage of this technique is that a user-level threads package can be implemented on an Operating System that does not support threads. • User level threads are fast to create and manage. • Simple Representation: Each thread is represented simply by a PC, registers, stack and a small control block, all stored in the user process address space. • Simple Management: This simply means that creating a thread, switching between threads and synchronization between threads can all be done without intervention of the kernel. • Fast and Efficient: Thread switching is not much more expensive than a procedure call. Disadvantages of user-level threads: • There is a lack of coordination between threads and operating system kernel. Therefore, process as whole gets one time slice irrespective of whether process has one thread or 1000 threads within. It is up to each thread to relinquish control to other threads. • User-level threads require non-blocking systems call i.e., a multithreaded kernel. Otherwise, entire process will blocked in the kernel, even if there are runnable threads left in the processes. For example, if one thread causes a page fault, the process blocks.
  • 13. Kernel Level Threads • In this case, thread management is done by the Kernel. • There is no thread management code in the application area. • Kernel threads are supported directly by the operating system. • Any application can be programmed to be multithreaded. • All of the threads within an application are supported within a single process. • The Kernel maintains context information for the process as a whole and for individuals threads within the process. • Scheduling by the Kernel is done on a thread basis. • The Kernel performs thread creation, scheduling and management in Kernel space. • Kernel threads are generally slower to create and manage than the user threads.
  • 14. • Advantages of kernel-level threads: • Because kernel has full knowledge of all threads, Scheduler may decide to give more time to a process having large number of threads than process having small number of threads. • Kernel can simultaneously schedule multiple threads from the same process on multiple processes. • Kernel-level threads are especially good for applications that frequently block - If one thread in a process is blocked, the Kernel can schedule another thread of the same process. • Disadvantages of kernel level threads: • The kernel-level threads are slow and inefficient. For instance, threads operations are hundreds of times slower than that of user-level threads. • Since kernel must manage and schedule threads as well as processes. It requires a full thread control block (TCB) for each thread to maintain information about threads. As a result there is significant overhead and increased in kernel complexity.
  • 15. Difference between User-Level & Kernel-Level Thread S.N. User-Level Threads Kernel-Level Thread 1 User-level threads are faster to create and manage. Kernel-level threads are slower to create and manage. 2 Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads. 3 User-level thread is generic and can run on any operating system. Kernel-level thread is specific to the operating system. 4 Multi-threaded applications cannot take advantage of multiprocessing. Kernel routines themselves can be multithreaded.
  • 16. Multithreading Models • Some operating system provide a combined user level thread and Kernel level thread facility e.g. Solaris is a good example of this combined approach. • In a combined system, multiple threads within the same application can run in parallel on multiple processors and a blocking system call need not block the entire process. i. Many-To-One Model ii. One-To-One Model iii. Many-To-Many Model
  • 17. Many-To-One Threading 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 very efficient. • However, if a blocking system call is made, then the entire process blocks, even if the other user threads would otherwise be able to continue. • Because a single kernel thread can operate only on a single CPU, the many-to-one model does not allow individual processes to be split across multiple CPUs. • Green threads for Solaris and GNU Portable Threads implement the many-to-one model in the past, but few systems continue to do so today.
  • 19. One-To-One Threading Model • The one-to-one model creates a separate kernel thread to handle each user thread. • One-to-one model overcomes the problems listed above involving blocking system calls and the splitting of processes across multiple CPUs. • However the overhead of managing the one-to-one model is more significant, involving more overhead and slowing down the system. • Most implementations of this model place a limit on how many threads can be created. • Linux and Windows from 95 implement the one-to-one model for threads.
  • 21. Many-To-Many Threading 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 have no restrictions on the number of threads created. • Blocking kernel system calls do not block the entire process. • Processes can be split across multiple processors. • Individual processes may be allocated variable numbers of kernel threads, depending on the number of CPUs present and other factors.
  • 23. Thread Libraries • Thread libraries provides programmers with API for creating and managing of threads. • Thread libraries may be implemented either in user space or in kernel space. • The user space involves API functions implemented solely within user space, with no kernel support. • The kernel space involves system calls, and requires a kernel with thread library support. Application that Benefits from Threads • A proxy server satisfying the requests for a number of computers on a LAN would be benefited by a multi-threaded process. In general, any program that has to do more than one task at a time could benefit from multitasking. For example, a program that reads input, process it, and outputs could have three threads, one for each task. Application that cannot Benefit from Threads • Any sequential process that cannot be divided into parallel task will not benefit from thread, as they would block until the previous one completes. For example, a program that displays the time of the day would not benefit from multiple threads.
  • 24. Benefits of Multithreading • Responsiveness • Resource sharing, hence allowing better utilization of resources - Threads allow the sharing of a lot resources that cannot be shared in process, for example, sharing code section, data section, Operating System resources like open file etc. • Economy. Creating and managing threads becomes easier. • Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a series of processors to scale. • Context Switching is smooth. Context switching refers to the procedure followed by CPU to change from one task to another.
  • 25. Resources used in Thread Creation and Process Creation • When a new thread is created it shares its code section, data section and operating system resources like open files with other threads. • But it is allocated its own stack, register set and a program counter. • The creation of a new process differs from that of a thread mainly in the fact that all the shared resources of a thread are needed explicitly for each process. • So though two processes may be running the same piece of code they need to have their own copy of the code in the main memory to be able to run. • Two processes also do not share other resources with each other. • This makes the creation of a new process very costly compared to that of a new thread.
  • 26. Multithreading Issues • Thread Cancellation - Thread cancellation means terminating a thread before it has finished working - are no longer needed. There can be two approaches for this, one is Asynchronous cancellation, which terminates the target thread immediately. The other is 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. Now in when a Multithreaded process receives a signal, to which thread it must be delivered? • There are four major options: a) Deliver the signal to the thread to which the signal applies, b) Deliver the signal to every thread in the process, c) Deliver the signal to certain threads in the process, d) Assign a specific thread to receive all signals in a process. • fork() System Call - fork() is a system call executed in the kernel through which a process creates a copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process be copied or not? The answers included a0 its system dependent, b) If the new process execs right away, there is no need to copy all the other threads. If it doesn't, then the entire process should be copied, c) A: Many versions of UNIX provide multiple versions of the fork call for this purpose. • Security Issues because of extensive sharing of resources between multiple threads.