Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

THREADS

• DEFINATION OF THREADS
• TYPES OF THREAD PROCESS(SINGLE AND
MULTITHREADED PROCESS)
• BENEFITS OF MULTITHREAD
• MULTITHREADING MODELS(MANY- TO ONE
MODEL,ONE –TO –ONE MODEL,MANY TO MANY
MODEL)
DEFINITION OF THREADS
• Imagine you are using a web browser to access
website say facebook. The browser is the process
and within that process there are multiple
threads working together to provide you
browsing experience.
a. Main thread tab manager: Control and
coordinates the event between you and network
b. Graphics: This is responsible for rendering the
visual element of the facebook
c. Networking thread: This thread handles network
requests ..etc..
Definition of Thread
- Thread is light weight process created by process.
- A thread is a semi process that has its own stack, and executes a given
piece of code.
- Thread is a single sequence stream within a process.
- All these threads execute in parallel and The cpu switches rapidly back and
forth among the threads giving illusion that the threads are running in
parallel.
- Like a traditional process i.e process with one thread, a thread can be in
any several states(running, blocked, ready, terminated).
- Threads has its own:
- Program counter that keep track of which instruction to execute next
- System registers which holds its current working variables
- Stack which contains the execution history.
Example of thread
Definition of threads
What are the reasons for having these mini-processes, called
threads
- The main reason for having threads is that in many
applications, multiple activities are going on at once.
- By decomposing such an application into multiple sequential
threads that run in parallel.
Similarities between process and
thread
• Like processes threads share cpu and only one
thread is running at a time
• Like processes threads within a process
execute sequentially
• Like processes thread can create children
• Like a process a thread can be in any one of
several states like running, ready, etc.
• Like process threads have program counter,
stack, registers and state.
Dissimilarities between process and
thread.
• Unlike processes threads are not independent
of one another
• Threads within the same process share an
address space.
• Unlike processes threads can access every
address in the task
• Unlike process threads are design to assist one
another.
Advantages of threads
• Threads minimize the context switching time
• Use of threads provides concurrency within a
process
• Efficient communication
• It is more easy to create and context switch
threads
• Threads can execute in parallel on
multiprocessors.
• Threads have full access to address space(easy
sharing)
Types of Threads
• 1. kernel level thread
• 2. user level thread
Types of threads
User level thread Kernel level thread

User thread are implemented by Kernel threads are implemented by


users OS
OS does not recognize user level Kernel threads are recognized by OS
threads
Implementation of user threads is Implementation of kernel thread is
easy complex

Context switch requires no hardware Context switch requires hardware


support and time is less support and time is more

If one user level thread perform If one kernel thread perform blocking
blocking operation then entire operation then another thread within
process will be blocked same process can continue execution

Example java thread Example Windows kernel threads


Types of thread
Hybrid Thread:
- Combines the advantages of user level and kernel level thread.
- It uses kernel level thread and then multiplex user level thread on the
some or all the kernel threads.
- Gives flexibility to programmer that how many khernel level threads to
use and how many user level thread to multiplex on each one.
- Kernel is always or only one kernel level threads and schedule it.
Multithreading
• Multithreading refers to the ability of a CPU to
execute multiple threads concurrently. It
allows multiple threads to run concurrently,
sharing the resources of a single or multiple
CPU cores.
• For example When we execute MS word
application, then this process can check
spelling, auto-save and read file from hard
disk while working on the document.
Benefits of multithread
• Improved responsiveness: Multithreading can be used
to keep a program responsive to user input, even when
certain threads are performing time consuming tasks.
• Resource Sharing: Threads share the memory and the
resources of the process to which they belong.
• Increased throughput By allowing multiple threads to
execute simultaneously, multithreading can enhance
the overall system throughput and performance.
• Economy : Allocating memory and resources for
process creation is costly. Since, threads share memory
and resources of the process to which they belong, it is
more economical to create and context switch threads.
Multi Threaded Models
Multithreading models
Many –to –One- Model: This model maps many
user- level threads to one kernel thread. Thread
management is done in user space, so it is
efficient but entire process will block if a thread
makes a blocking system call. Also, because only
one thread can access the kernel at a time,
multiple threads are unable to run in parallel on
multiprocessors. Greed threads- a thread library
available for solaris2 uses this mode.
Multithreading models
This model maps each user thread to a kernel
thread, it provides more concurrency that the
many-to-one model by allowing another thread
to run when a thread makes a blocking system
call. It allows multiple threads to run in parallel
on multiprocessors. The only drawback is that
creating a user thread requires creating the
corresponding kernel thread. Windows NT,
Windows 2000 ,etc implement this model
Multithreading models
Many- to –Many Model: This model multiplexes
many user level threads to a smaller or equal
number of kernel threads. The number of kernel
threads may be specific to either a particular
application or a particuler machine. Developers
can create as many user threads as necessary,
and the corresponding kernel threads can run in
parallel on a multiprocessor. Also when a thread
performs a blocking system call, the kernel can
schedule another thread for execution.

You might also like