SlideShare a Scribd company logo
1 of 28
Download to read offline
Operating Systems: CSE 3204
ASTU
Department of CSE
May 21, 2019 1
Operating Systems
Lecture 3: Threads
Chapter Two
Process Management
Threads
• Inter-process communication is simple and easy when used occasionally
• If there are many processes sharing many resources, then the
mechanism becomes cumbersome- difficult to handle.
Threads are created to make this kind of resource sharing simple
& efficient
• A thread is a basic unit of CPU utilization that consists of:
• thread id
• program counter
• register set
• stack
• Threads belonging to the same process share:
• its code
• its data section
• other OS resources
May 21, 2019 2
Operating Systems
Processes and Threads
• A thread of execution is the smallest unit of processing that can be
scheduled by an OS.
• The implementation of threads and process differs from one OS to another,
but in most cases, a thread is contained inside a process.
• Multiple threads can exist within the same process and share resources such
as memory, while different processes do not share these resources.
• Like process states, threads also have states:
• New, Ready, Running, Waiting and Terminated
• Like processes, the OS will switch between threads (even though they
belong to a single process) for CPU usage
• Like process creation, thread creation is supported by APIs
• Creating threads is inexpensive (cheaper) compared to processes
• They do not need new address space, global data, program code or
operating system resources
• Context switching is faster as the only things to save/restore are program
counters, registers and stacks
May 21, 2019 3
Operating Systems
Processes and Threads (cont.)
Similarities Differences
• Both share CPU and only one
thread/process is active (running) at
a time.
•Like processes, threads within a
process execute sequentially.
•Like processes, thread can create
children.
•Like process, if one thread is
blocked, another thread can run.
• Unlike processes, threads are
not independent of one another.
• Unlike processes, all threads
can access every address in the
task.
• Unlike processes, thread are
design to assist one other.
May 21, 2019 4
Operating Systems
The design space: Process vs Thread
address
space
thread
one thread/process
many processes
many threads/process
many processes
one thread/process
one process
many threads/process
one process
MS/DOS
Java
older
UNIXes
Mach, NT,
Chorus,
Linux, …
Key
(old) Process address space
0x00000000
0xFFFFFFFF
address space
code
(text segment)
static data
(data segment)
heap
(dynamic allocated mem)
stack
(dynamic allocated mem)
PC
SP
(new) Process address space with threads
0x00000000
0xFFFFFFFF
address space
code
(text segment)
static data
(data segment)
heap
(dynamic allocated mem)
thread 1 stack
PC (T2)
SP (T2)
thread 2 stack
thread 3 stack
SP (T1)
SP (T3)
PC (T1)
PC (T3)
Why we need threads
• The main disadvantage of single threaded processes is that they
don’t provide proper utilization of CPU.
• Multithreaded processes can simultaneously perform different
functions like accepting requests at the network, doing I/O, doing
computation on behalf of a user; each in a separate thread of
control.
• Think about a web server, which have thousands of clients
sending requests for different parts of a website. If there was
only one server side thread running on the server, How much
time will be needed to serve the request of each client?
• The effective delay for each response will be very high in single
threaded processes
May 21, 2019 8
Operating Systems
Advantages of multithreaded processes
There are four main advantages of building multithreaded
applications:
• Responsiveness: For example in case of GUI based user interface, the
time between two clicks is expected to be un-noticeable to a user. By
providing a separate thread for various buttons and menus the response
time is decreased. one thread can give response while other threads are
blocked or slowed down doing computations
• Resource sharing: Threads are lightweight in nature while processes
are heavyweight. It means that, sharing of resources occupied by a
process among various threads is possible in multithreaded
environment. Threads share common code, data and resources of the
process to which they belong. This allows multiple tasks to performed
within the same address space
May 21, 2019 9
Operating Systems
Advantages of multithreaded processes(Cont…)
• Economy: creating, managing and context switch of
processes is more costly as compared to threads.
• Scalability: A single threaded process runs only on
single processor regardless of availability of multicore
architecture. Now a days, multicore and hyper-
threaded architecture allows multiple concurrent
threads running in parallel. This kind of dual benefit of
parallelism and concurrency provide heavily scalable
systems, as the processor cores can be easily added
into architecture.
May 21, 2019 10
Operating Systems
Thread Libraries
• Thread libraries provide programmers an API to create and manage
threads
• They can be implemented in either user space or kernel
space
User space libraries
• All code and data structure of the library are in user space
• Invoking a function in the library results in a local function
call
Kernel space libraries
• Code and data structure of the library is in kernel space
• Invoking an API for the library results in a system call to
the kernel
May 21, 2019 11
Operating Systems
Thread Libraries (cont.)
There are three basic libraries used:
 POSIX pthreads
• They may be provided as either a user or kernel library, as an
extension to the POSIX standard
• Systems like Solaris, Linux and Mac OS X implement pthreads
specifications
 WIN32 threads
• These are provided as a kernel-level library on Windows systems.
 Java threads
• Since Java generally runs on a Java Virtual Machine, the implementation of
threads is based upon whatever OS and hardware the JVM is running on, i.e.
either Pthreads or Win32 threads depending on the system.
May 21, 2019 12
Operating Systems
Examples of Threads
In a word processor,
• a background thread may check spelling and grammar, while a
foreground thread processes user input ( keystrokes ), while yet a
third thread loads images from the hard drive, and a fourth does
periodic automatic backups of the file being edited
In a spreadsheet program,
• one thread could display menus and read user input, while another
thread executes user commands and updates the spreadsheet
In a web server,
• Multiple threads allow for multiple requests to be satisfied
simultaneously, without having to service requests sequentially or to
fork off separate processes for every incoming request.
May 21, 2019 13
Operating Systems
Multithreading
• Multithreading refers to the ability on an operating system to support
multiple threads of execution within a single process.
• A traditional (heavy weight) process has a single thread of control
• There’s one program counter and a set of instructions carried out
at a time
• If a process has multiple thread of control, it can perform more than
one task at a time
• Each threads have their own program counter, stacks and registers
• But they share common code, data and some operating system
data structures like files
May 21, 2019 14
Operating Systems
Multi-threading(cont...)
• Traditionally there is a single thread of execution per process.
• Example: MSDOS supports a single user process and single thread.
• UNIX supports multiple user processes but only support one thread per process.
• Multithreading
• Java run time environment is an example of one process with multiple threads.
• Examples of supporting multiple processes, with each process supporting multiple
threads
• Windows 2000, Solaris, Linux, Mach, and OS/2
One process one thread One process multiple threads
Multiple processes,
one thread per process
Multiple processes,
multiple threads per process
Instruction
trace
May 21, 2019 15
Operating Systems
Single and Multithreaded Processes
 In a single threaded process model, the representation of a process includes its PCB,
user address space, as well as user and kernel stacks.
When a process is running. The contents of these registers are controlled by that
process, and the contents of these registers are saved when the process is not running.
In a multi threaded environment
• There is a single PCB and address space,
• However, there are separate stacks for each thread as well as separate control
blocks for each thread containing register values, priority, and other thread related
state information.
May 21, 2019 16
Operating Systems
Multithreading Models
There are two types of multithreading models in modern operating systems:
user threads & kernel threads
1. Kernel threads
• are supported by the OS kernel itself. All modern OS support kernel threads
• Need user/kernel mode switch to change threads
2. User threads
• are threads application programmers put in their programs. They are managed
without the kernel support, Does not require O.S. support
•Has problems with blocking system calls
•Cannot support multiprocessing
• There must be a relationship between the kernel threads and the user threads.
•There are 3 common ways to establish this relationship:
1. Many-to-One
2. One-to-One
3. Many-to-Many
May 21, 2019 17
Operating Systems
Multithreading Models: Many-to-One
•It maps many user level threads in to
one kernel thread
•Thread management is done by
thread library in user space
•It’s efficient but if a thread makes
blocking system call, it blocks
•Only one thread access the kernel at
a time, so multiple threads can not
run on multiprocessor systems
•Used on systems that do not support
kernel threads.
May 21, 2019 18
Operating Systems
Multithreading models: One-to-One
• Each user-level thread maps to kernel thread.
• A separate kernel thread is created to
handle each user-level thread
• It provides more concurrency and solves
the problems of blocking system calls
• Managing the one-to-one model involves
more overhead slowed down system
• Drawback: creating user thread requires
creating the corresponding kernel thread.
• Most implementations of this thread puts
restrictions on the number of threads
created
May 21, 2019 19
Operating Systems
Multithreading Models: Many-to-Many
• allows the mapping of many user threads in
to many kernel threads
• Allows the OS to create sufficient number of
kernel threads
• It combines the best features of one-to-one
and many-to-one model
• Users have no restrictions on the numbers of
threads created
• Blocking kernel system calls do not block the
entire process.
May 21, 2019 20
Operating Systems
Multithreading Models: Many-to-Many
• 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
• One popular variation of the many-to-
many model is the two-tier model, which
allows either many-to-many or one-to-
one operation.
Two-tier model
May 21, 2019 21
Operating Systems
Threading Issues
• Some of the issues to be considered for multi-threaded
programs
• Semantics of fork() and exec() system calls.
• Thread cancellation.
• Signal handling
• Thread pools
• Thread-specific data
• Hogging the CPU
May 21, 2019 22
Operating Systems
Threading Issues in multithreading
• Semantics of fork() and exec() system calls
• In multithreaded program the semantics of the fork and exec systems calls
change.
• If one thread calls fork, there are two options.
• New process can duplicate all the threads or new process is a process
with single thread
• Some systems have chosen two versions of fork
• Thread cancellation
• Task of terminating thread before its completion
• Example: if multiple threads are searching database, if one gets the result
others should be cancelled
• Asynchronous cancellation
• One thread immediately terminates the target thread
• Deferred cancellation
• The target thread can paradoxically check if it should terminate
May 21, 2019 23
Operating Systems
Threading Issue: Thread signaling
• Signals are used in UNIX systems to notify a process that a particular
event has occurred
• A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
• Options:
• Deliver the signal to the thread to which the signal applies
• Deliver the signal to every thread in the process
• Deliver the signal to certain threads in the process
• Assign a specific thread to receive all signals for the process
May 21, 2019 24
Operating Systems
Threading Issue: Threading Pool
• Creating new threads every time one is needed and then deleting it when
it is done can be inefficient, and can also lead to a very large ( unlimited
) number of threads being created.
• An alternative solution is to create a number of threads when the process
first starts, and put those threads into a thread pool.
• Threads are allocated from the pool as needed, and returned to the
pool when no longer needed.
• When no threads are available in the pool, the process may have to
wait until one becomes available.
• The ( maximum ) number of threads available in a thread pool may be
determined by adjustable parameters, possibly dynamically in response
to changing system loads.
• Win32 provides thread pools through the "PoolFunction" function.
• Java also provides support for thread pools.
May 21, 2019 25
Operating Systems
Threading Issue: Thread- specific data
• Most data is shared among threads, and this is one of the major benefits
of using threads in the first place.
• However sometimes threads need thread-specific data also in some
circumstances
• Such data is called thread-specific data.
• Most major thread libraries ( pThreads, Win32, Java ) provide support for
thread-specific data.
May 21, 2019 26
Operating Systems
Threading Issue: Scheduler activation
• Both M:M and Two-level models require communication to maintain the
appropriate number of kernel threads allocated to the application
• Scheduler activations provide upcalls - a communication mechanism
from the kernel to the thread library
• This communication allows an application to maintain the correct number
of kernel threads
• Hogging: threads struck beyond time allowed to them on CPU
May 21, 2019 27
Operating Systems
 Strategy 1: force everyone to cooperate
 a thread willingly gives up the CPU by calling yield()
 yield() calls into the scheduler, which context switches to
another ready thread
 what happens if a thread never calls yield()?
 Strategy 2: use preemption
 scheduler requests that a timer interrupt be delivered by the
OS periodically
 usually delivered as a UNIX signal (man signal)
 signals are just like software interrupts, but delivered to
user-level by the OS instead of delivered to OS by
hardware
 at each timer interrupt, scheduler gains control and context
switches as appropriate
How to keep a user-level thread from
hogging the CPU?

More Related Content

Similar to Lecture 3- Threads.pdf

Operating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxOperating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxPrudhvi668506
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Multithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfMultithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfHarika Pudugosula
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 
Operating system 21 multithreading models
Operating system 21 multithreading modelsOperating system 21 multithreading models
Operating system 21 multithreading modelsVaibhav Khanna
 
1. What important part of the process switch operation is not shown .pdf
1. What important part of the process switch operation is not shown .pdf1. What important part of the process switch operation is not shown .pdf
1. What important part of the process switch operation is not shown .pdffathimaoptical
 
Apos week 1 4
Apos week 1   4Apos week 1   4
Apos week 1 4alixafar
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.pptshreesha16
 
EMBEDDED OS
EMBEDDED OSEMBEDDED OS
EMBEDDED OSAJAL A J
 
OS multiprocessing1.pptx
OS multiprocessing1.pptxOS multiprocessing1.pptx
OS multiprocessing1.pptxamerdawood2
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programmingAnyapuPranav
 

Similar to Lecture 3- Threads.pdf (20)

Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
Operating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxOperating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptx
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Epc 3.ppt
Epc 3.pptEpc 3.ppt
Epc 3.ppt
 
4 threads
4 threads4 threads
4 threads
 
Multithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfMultithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdf
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Treads
TreadsTreads
Treads
 
Topic 4- processes.pptx
Topic 4- processes.pptxTopic 4- processes.pptx
Topic 4- processes.pptx
 
Operating system 21 multithreading models
Operating system 21 multithreading modelsOperating system 21 multithreading models
Operating system 21 multithreading models
 
1. What important part of the process switch operation is not shown .pdf
1. What important part of the process switch operation is not shown .pdf1. What important part of the process switch operation is not shown .pdf
1. What important part of the process switch operation is not shown .pdf
 
Apos week 1 4
Apos week 1   4Apos week 1   4
Apos week 1 4
 
Thread
ThreadThread
Thread
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.ppt
 
EMBEDDED OS
EMBEDDED OSEMBEDDED OS
EMBEDDED OS
 
OS multiprocessing1.pptx
OS multiprocessing1.pptxOS multiprocessing1.pptx
OS multiprocessing1.pptx
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
Thread
ThreadThread
Thread
 
Chapter04 new
Chapter04 newChapter04 new
Chapter04 new
 

More from Amanuelmergia

Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxLecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxAmanuelmergia
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxOperating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxAmanuelmergia
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxOperating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxAmanuelmergia
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxLecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxAmanuelmergia
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfAmanuelmergia
 
OS course Outilne 2021.doc
OS course Outilne 2021.docOS course Outilne 2021.doc
OS course Outilne 2021.docAmanuelmergia
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdfLecture 2- Processes.pdf
Lecture 2- Processes.pdfAmanuelmergia
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfLecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfAmanuelmergia
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxLecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxAmanuelmergia
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxLecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxAmanuelmergia
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxAmanuelmergia
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxLecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxAmanuelmergia
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxAmanuelmergia
 

More from Amanuelmergia (13)

Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptxLecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptx
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptxOperating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptx
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptxOperating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptx
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptxLecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
OS course Outilne 2021.doc
OS course Outilne 2021.docOS course Outilne 2021.doc
OS course Outilne 2021.doc
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdfLecture 2- Processes.pdf
Lecture 2- Processes.pdf
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdfLecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdf
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptxLecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptx
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptxLecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptx
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptxLecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptxLecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptx
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptxLecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
 

Recently uploaded

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Lecture 3- Threads.pdf

  • 1. Operating Systems: CSE 3204 ASTU Department of CSE May 21, 2019 1 Operating Systems Lecture 3: Threads Chapter Two Process Management
  • 2. Threads • Inter-process communication is simple and easy when used occasionally • If there are many processes sharing many resources, then the mechanism becomes cumbersome- difficult to handle. Threads are created to make this kind of resource sharing simple & efficient • A thread is a basic unit of CPU utilization that consists of: • thread id • program counter • register set • stack • Threads belonging to the same process share: • its code • its data section • other OS resources May 21, 2019 2 Operating Systems
  • 3. Processes and Threads • A thread of execution is the smallest unit of processing that can be scheduled by an OS. • The implementation of threads and process differs from one OS to another, but in most cases, a thread is contained inside a process. • Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources. • Like process states, threads also have states: • New, Ready, Running, Waiting and Terminated • Like processes, the OS will switch between threads (even though they belong to a single process) for CPU usage • Like process creation, thread creation is supported by APIs • Creating threads is inexpensive (cheaper) compared to processes • They do not need new address space, global data, program code or operating system resources • Context switching is faster as the only things to save/restore are program counters, registers and stacks May 21, 2019 3 Operating Systems
  • 4. Processes and Threads (cont.) Similarities Differences • Both share CPU and only one thread/process is active (running) at a time. •Like processes, threads within a process execute sequentially. •Like processes, thread can create children. •Like process, if one thread is blocked, another thread can run. • Unlike processes, threads are not independent of one another. • Unlike processes, all threads can access every address in the task. • Unlike processes, thread are design to assist one other. May 21, 2019 4 Operating Systems
  • 5. The design space: Process vs Thread address space thread one thread/process many processes many threads/process many processes one thread/process one process many threads/process one process MS/DOS Java older UNIXes Mach, NT, Chorus, Linux, … Key
  • 6. (old) Process address space 0x00000000 0xFFFFFFFF address space code (text segment) static data (data segment) heap (dynamic allocated mem) stack (dynamic allocated mem) PC SP
  • 7. (new) Process address space with threads 0x00000000 0xFFFFFFFF address space code (text segment) static data (data segment) heap (dynamic allocated mem) thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3)
  • 8. Why we need threads • The main disadvantage of single threaded processes is that they don’t provide proper utilization of CPU. • Multithreaded processes can simultaneously perform different functions like accepting requests at the network, doing I/O, doing computation on behalf of a user; each in a separate thread of control. • Think about a web server, which have thousands of clients sending requests for different parts of a website. If there was only one server side thread running on the server, How much time will be needed to serve the request of each client? • The effective delay for each response will be very high in single threaded processes May 21, 2019 8 Operating Systems
  • 9. Advantages of multithreaded processes There are four main advantages of building multithreaded applications: • Responsiveness: For example in case of GUI based user interface, the time between two clicks is expected to be un-noticeable to a user. By providing a separate thread for various buttons and menus the response time is decreased. one thread can give response while other threads are blocked or slowed down doing computations • Resource sharing: Threads are lightweight in nature while processes are heavyweight. It means that, sharing of resources occupied by a process among various threads is possible in multithreaded environment. Threads share common code, data and resources of the process to which they belong. This allows multiple tasks to performed within the same address space May 21, 2019 9 Operating Systems
  • 10. Advantages of multithreaded processes(Cont…) • Economy: creating, managing and context switch of processes is more costly as compared to threads. • Scalability: A single threaded process runs only on single processor regardless of availability of multicore architecture. Now a days, multicore and hyper- threaded architecture allows multiple concurrent threads running in parallel. This kind of dual benefit of parallelism and concurrency provide heavily scalable systems, as the processor cores can be easily added into architecture. May 21, 2019 10 Operating Systems
  • 11. Thread Libraries • Thread libraries provide programmers an API to create and manage threads • They can be implemented in either user space or kernel space User space libraries • All code and data structure of the library are in user space • Invoking a function in the library results in a local function call Kernel space libraries • Code and data structure of the library is in kernel space • Invoking an API for the library results in a system call to the kernel May 21, 2019 11 Operating Systems
  • 12. Thread Libraries (cont.) There are three basic libraries used:  POSIX pthreads • They may be provided as either a user or kernel library, as an extension to the POSIX standard • Systems like Solaris, Linux and Mac OS X implement pthreads specifications  WIN32 threads • These are provided as a kernel-level library on Windows systems.  Java threads • Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pthreads or Win32 threads depending on the system. May 21, 2019 12 Operating Systems
  • 13. Examples of Threads In a word processor, • a background thread may check spelling and grammar, while a foreground thread processes user input ( keystrokes ), while yet a third thread loads images from the hard drive, and a fourth does periodic automatic backups of the file being edited In a spreadsheet program, • one thread could display menus and read user input, while another thread executes user commands and updates the spreadsheet In a web server, • Multiple threads allow for multiple requests to be satisfied simultaneously, without having to service requests sequentially or to fork off separate processes for every incoming request. May 21, 2019 13 Operating Systems
  • 14. Multithreading • Multithreading refers to the ability on an operating system to support multiple threads of execution within a single process. • A traditional (heavy weight) process has a single thread of control • There’s one program counter and a set of instructions carried out at a time • If a process has multiple thread of control, it can perform more than one task at a time • Each threads have their own program counter, stacks and registers • But they share common code, data and some operating system data structures like files May 21, 2019 14 Operating Systems
  • 15. Multi-threading(cont...) • Traditionally there is a single thread of execution per process. • Example: MSDOS supports a single user process and single thread. • UNIX supports multiple user processes but only support one thread per process. • Multithreading • Java run time environment is an example of one process with multiple threads. • Examples of supporting multiple processes, with each process supporting multiple threads • Windows 2000, Solaris, Linux, Mach, and OS/2 One process one thread One process multiple threads Multiple processes, one thread per process Multiple processes, multiple threads per process Instruction trace May 21, 2019 15 Operating Systems
  • 16. Single and Multithreaded Processes  In a single threaded process model, the representation of a process includes its PCB, user address space, as well as user and kernel stacks. When a process is running. The contents of these registers are controlled by that process, and the contents of these registers are saved when the process is not running. In a multi threaded environment • There is a single PCB and address space, • However, there are separate stacks for each thread as well as separate control blocks for each thread containing register values, priority, and other thread related state information. May 21, 2019 16 Operating Systems
  • 17. Multithreading Models There are two types of multithreading models in modern operating systems: user threads & kernel threads 1. Kernel threads • are supported by the OS kernel itself. All modern OS support kernel threads • Need user/kernel mode switch to change threads 2. User threads • are threads application programmers put in their programs. They are managed without the kernel support, Does not require O.S. support •Has problems with blocking system calls •Cannot support multiprocessing • There must be a relationship between the kernel threads and the user threads. •There are 3 common ways to establish this relationship: 1. Many-to-One 2. One-to-One 3. Many-to-Many May 21, 2019 17 Operating Systems
  • 18. Multithreading Models: Many-to-One •It maps many user level threads in to one kernel thread •Thread management is done by thread library in user space •It’s efficient but if a thread makes blocking system call, it blocks •Only one thread access the kernel at a time, so multiple threads can not run on multiprocessor systems •Used on systems that do not support kernel threads. May 21, 2019 18 Operating Systems
  • 19. Multithreading models: One-to-One • Each user-level thread maps to kernel thread. • A separate kernel thread is created to handle each user-level thread • It provides more concurrency and solves the problems of blocking system calls • Managing the one-to-one model involves more overhead slowed down system • Drawback: creating user thread requires creating the corresponding kernel thread. • Most implementations of this thread puts restrictions on the number of threads created May 21, 2019 19 Operating Systems
  • 20. Multithreading Models: Many-to-Many • allows the mapping of many user threads in to many kernel threads • Allows the OS to create sufficient number of kernel threads • It combines the best features of one-to-one and many-to-one model • Users have no restrictions on the numbers of threads created • Blocking kernel system calls do not block the entire process. May 21, 2019 20 Operating Systems
  • 21. Multithreading Models: Many-to-Many • 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 • One popular variation of the many-to- many model is the two-tier model, which allows either many-to-many or one-to- one operation. Two-tier model May 21, 2019 21 Operating Systems
  • 22. Threading Issues • Some of the issues to be considered for multi-threaded programs • Semantics of fork() and exec() system calls. • Thread cancellation. • Signal handling • Thread pools • Thread-specific data • Hogging the CPU May 21, 2019 22 Operating Systems
  • 23. Threading Issues in multithreading • Semantics of fork() and exec() system calls • In multithreaded program the semantics of the fork and exec systems calls change. • If one thread calls fork, there are two options. • New process can duplicate all the threads or new process is a process with single thread • Some systems have chosen two versions of fork • Thread cancellation • Task of terminating thread before its completion • Example: if multiple threads are searching database, if one gets the result others should be cancelled • Asynchronous cancellation • One thread immediately terminates the target thread • Deferred cancellation • The target thread can paradoxically check if it should terminate May 21, 2019 23 Operating Systems
  • 24. Threading Issue: Thread signaling • Signals are used in UNIX systems to notify a process that a particular event has occurred • A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled • Options: • Deliver the signal to the thread to which the signal applies • Deliver the signal to every thread in the process • Deliver the signal to certain threads in the process • Assign a specific thread to receive all signals for the process May 21, 2019 24 Operating Systems
  • 25. Threading Issue: Threading Pool • Creating new threads every time one is needed and then deleting it when it is done can be inefficient, and can also lead to a very large ( unlimited ) number of threads being created. • An alternative solution is to create a number of threads when the process first starts, and put those threads into a thread pool. • Threads are allocated from the pool as needed, and returned to the pool when no longer needed. • When no threads are available in the pool, the process may have to wait until one becomes available. • The ( maximum ) number of threads available in a thread pool may be determined by adjustable parameters, possibly dynamically in response to changing system loads. • Win32 provides thread pools through the "PoolFunction" function. • Java also provides support for thread pools. May 21, 2019 25 Operating Systems
  • 26. Threading Issue: Thread- specific data • Most data is shared among threads, and this is one of the major benefits of using threads in the first place. • However sometimes threads need thread-specific data also in some circumstances • Such data is called thread-specific data. • Most major thread libraries ( pThreads, Win32, Java ) provide support for thread-specific data. May 21, 2019 26 Operating Systems
  • 27. Threading Issue: Scheduler activation • Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application • Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library • This communication allows an application to maintain the correct number of kernel threads • Hogging: threads struck beyond time allowed to them on CPU May 21, 2019 27 Operating Systems
  • 28.  Strategy 1: force everyone to cooperate  a thread willingly gives up the CPU by calling yield()  yield() calls into the scheduler, which context switches to another ready thread  what happens if a thread never calls yield()?  Strategy 2: use preemption  scheduler requests that a timer interrupt be delivered by the OS periodically  usually delivered as a UNIX signal (man signal)  signals are just like software interrupts, but delivered to user-level by the OS instead of delivered to OS by hardware  at each timer interrupt, scheduler gains control and context switches as appropriate How to keep a user-level thread from hogging the CPU?