Lecture 5 Thread

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

THREAD

P.Ray@CSE
Motivation
In certain situations, a single application may be
required to perform several similar tasks.
For example, a web server accepts client requests for
web pages, images, sound, and so forth. A busy web
server may have several (perhaps thousands) of
clients concurrently accessing it.
If the web server ran as a traditional single-threaded
process, it would be able to service only one client at
a time.
The amount of time that a client might have to wait
for its request to be serviced could be enormous.
P.Ray@CSE
One solution is to have the server run as a
single process that accepts requests. When the
server receives a request, it creates a separate
process to service that request.
In fact, this process-creation method was in
common use before threads became popular.
Process creation is time consuming and
resource intensive.
If the new process will perform the same tasks
as the existing process, why incur all that
overhead?
P.Ray@CSE
It is generally more efficient to use one process
that contains multiple threads. This approach
would multithread the web-server process.

The server would create a separate thread that


would listen for client requests; when a
request was made, rather than creating
another process, the server would create
another thread to service the request.

P.Ray@CSE
A process is divided in to number of light
weight process.
Each light weight process is said to be a
thread.
The thread has a program counter that keeps
the tracks of which instruction to execute
next, it has registers ,which hold its current
working variables.
It has a stack, which contains the execution
history.

P.Ray@CSE
Number of threads can share an address space,
open files, and others resources. Same as
number of process can share physical memory,
disks, printers and other resources.
Because threads have some of the properties of
process. Threads operates in many respects, in
the same manner as process.
Threads can be in one of several states: Ready,
Blocked, running, terminated.

P.Ray@CSE
Like process, Threads share the CPU, and
only one thread at a time is active
(Running).
Threads can create child threads, unlike
process, threads are not independent of
one another, because all threads 'can
access every address in the task.
A thread can read (or) write over any
other thread stacks.

P.Ray@CSE
P.Ray@CSE
P.Ray@CSE
Example
The human body performs a great variety of
operations in concurrently, for example seeing,
touching, smelling, tasting and hearing can occur
concurrently.
An automobile can be accelerating, turning, air
conditioning, and playing music connectively. The
programmer specifies that application contain
"Threads of execution".
Each thread designating portion of program that
may be execute concurrently with other threads.
This capability is called multithreading.
P.Ray@CSE
P.Ray@CSE
Examples of threads
Word processor: A programmer wish to type the
text in word processor. Then the programmer
open a file in word processor, and typing the
text( It is one thread) the text is automatically
formatting ( It is another thread).The text is
automatically specifies the spelling mistakes, (it
is another thread), and the file is automatically
saved in the disk ,(it is another thread).
Typing in a file is the process. But typing,
Formatting, Spell checking, saving these are
threads.
P.Ray@CSE
P.Ray@CSE
Need of Thread:
It takes far less time to create a new thread in an
existing process than to create a new process.
Threads can share the common data, they do not
need to use Inter- Process communication.
Context switching is faster when working with
threads.
It takes less time to terminate a thread than a
process.

P.Ray@CSE
Benefits : Responsiveness
 Multithreading an interactive application may
allow a program to continue running even if
part of it is blocked or is performing a lengthy
operation, thereby increasing responsiveness
to the user.
 For instance, a multithreaded web browser
could still allow user interaction in one thread
while an image was being loaded in another
thread.

P.Ray@CSE
Benefits : Resource sharing

By default, threads share the memory and the


resources of the process to which they belong.
The benefit of sharing code and data is that it
allows an application to have several different
threads of activity within the same address
space.

P.Ray@CSE
Benefits : Economy
Allocating memory and resources for process
creation is costly. Because threads share
resources of the process to which they belong,
it is more economical to create and context-
switch threads.
In Solaris, for example, creating a process is
about thirty times slower than is creating a
thread, and context switching is about five
times slower.

P.Ray@CSE
Benefits : Utilization of multiprocessor
architectures
The benefits of multithreading can be greatly
increased in a multiprocessor architecture,
where threads may be running in parallel on
different processors.
A single threaded process can only run on one
CPU, no matter how many are available.
Multi-threading on a multi-CPU machine
increases concurrency.

P.Ray@CSE
Life cycle of thread
A thread is said to be in one of following several thread
states.
1. Born State: A thread that has just created.
2. Ready state: The thread is waiting for processor (CPU)
3. Running: The system assigns the processor to the
thread( i.e the thread being executing)
4. Blocked state: The thread is waiting for an event to
occur (or) waiting for an I/O device.
5. Sleep: A sleeping thread becomes ready after the
designated sleep time expires.
6. Dead: The execution of the thread finished.
P.Ray@CSE
Thread States (Stalling)
As with processes, the key states for a thread
are Running, Ready, and Blocked.
Generally, it does not make sense to associate
suspend states with threads because such
states are process-level concepts.
In particular, if a process is swapped out, all of
its threads are necessarily swapped out
because they all share the address space of
the process.

P.Ray@CSE
There are four basic thread operations associated
with a change in thread state
 Spawn: Typically, when a new process is spawned, a thread for
that process is also spawned. Subsequently, a thread within a
process may spawn another thread within the same process,
providing an instruction pointer and arguments for the new
thread. The new thread is provided with its own register context
and stack space and placed on the ready queue.
 Block: When a thread needs to wait for an event, it will block
(saving its user registers, program counter, and stack pointers).The
processor may now turn to the execution of another ready thread
in the same or a different process.
 Unblock: When the event for which a thread is blocked occurs,
the thread is moved to the Ready queue.
 Finish: When a thread completes, its register context and stacks
are deallocated.
P.Ray@CSE
A significant issue is whether the blocking of a
thread results in the blocking of the entire
process.
In other words, if one thread in a process is
blocked, does this prevent the running of any
other thread in the same process even if that
other thread is in a ready state?
Clearly, some of the flexibility and power of
threads is lost if the one blocked thread blocks
an entire process.

P.Ray@CSE
Remote Procedure Call Using
Single Thread

P.Ray@CSE
Remote Procedure Call Using Threads

P.Ray@CSE
• Figure 4.3 shows a program that performs two remote procedure calls (RPCs) 2
to two different hosts to obtain a combined result.
• In a single-threaded program, the results are obtained in sequence, so that
the program has to wait for a response from each server in turn.
• Rewriting the program to use a separate thread for each RPC results in a
substantial speedup.
• Note that if this program operates on a uniprocessor, the requests must be
generated sequentially and the results processed in sequence; however, the
program waits concurrently for the two replies.
• On a uniprocessor, multiprogramming enables the interleaving of multiple
threads within multiple processes.
• In the example of Figure 4.4, three threads in two processes are interleaved
on the processor.
• Execution passes from one thread to another either when the currently
running thread is blocked or its time slice is exhausted.

P.Ray@CSE
Multithreading

P.Ray@CSE
Multithreading
A process is divided into number of
smaller tasks, each task is called a 'thread'.
Number of threads with in a process
execute at a time is called 'multithreading'.
Supporting of multithreading is the
additional capability of an operating
system.
Based on the functionality threads are
divided into four categories these are
following.
P.Ray@CSE
1. One process one thread: It is a traditional
approach, in which the process maintain only
one thread, so it is called single threaded
approach.
 For example MS-DOS operating system supports
this type of approach.
2. One process, multiple threads: In which a
process divided into number of threads; best
example for this one is 'JAVA runtime
environment'.

P.Ray@CSE
3. Multiple process, one thread per process: An
operating system support multiple user
processes but only support one thread per
process. Best example for this type is UNIX.

4. Multiple processes, multiple threads per


process: In which each process divided into
number of threads, example for this type is
windows 2000, Solaris LINUX.

P.Ray@CSE
P.Ray@CSE
Multithreading

P.Ray@CSE
P.Ray@CSE
The application executing in thread 2 makes a system call
that blocks B. For example, an I/O call is made.
This causes control to transfer to the kernel.The kernel
invokes the I/O action, places process B in the Blocked
state, and switches to another process.
Meanwhile, according to the data structure maintained
by the threads library, thread 2 of process B is still in the
Running state.
It is important to note that thread 2 is not actually
running in the sense of being executed on a processor;
but it is perceived as being in the Running state by the
threads library.
The corresponding state diagrams are shown in Figure
4.7b. P.Ray@CSE
Advantage of a Thread

A thread takes less time to terminate, than the


process.
It takes less time to switch between two threads
within the same process.
It takes less time to create a new thread within
the process.
Efficient communication between threads.

P.Ray@CSE
Thread synchronization
All threads with in a process share the memory
and files.
One thread is trying to modify one record in a
file, it will effect with another threads, so need
to try synchronizing the activities of threads.
User level and kernel level threads.
These are two main ways to implement
threads: in user space and in the kernel.

P.Ray@CSE
Types of Threads

• In the operating system, there are two types


of threads.

– Kernel level thread.


– User-level thread.

P.Ray@CSE
User level threads
This type of threads loaded entirely in user space, the
kernel knows nothing about them.
When threads are managed in user space, each process
needs its own private thread table.
The thread table consists the information of program
counter, stack pointer, registers, state etc, the thread
table managed by run time system.
When a thread is moved to ready state or blocked state
the information needed to restart it, is stored in the
thread table. Consider the below figure.

P.Ray@CSE
P.Ray@CSE
User-Level Threads

P.Ray@CSE
Advantages
1. User level threads allow each process to have
its own scheduling algorithm.
2. The entire process loaded in user space, so the
process does not switch to the kernel mode to
do thread management. This saves the
overhead of two mode switches.
3. User level threads can run on any operating
system.

P.Ray@CSE
 The user threads can be easily implemented than the
kernel thread.
 It is faster and efficient.
 Context switch time is shorter than the kernel-level
threads.
 It does not require modifications of the operating
system.
 User-level threads representation is very simple. The
register, PC, stack, and mini thread control blocks are
stored in the address space of the user-level process.
 It is simple to create, switch, and synchronize threads
without the intervention of the process.

P.Ray@CSE
Disadvantages
1. When user level thread executes a system call, not
only the particular thread blocked, all of the threads
with in the process are blocked.
2. In user level threads, only a single thread with in a
process can execute at a time. So multi processing is
can't implemented.
3. User-level threads lack coordination between the
thread and the kernel.
4. If a thread causes a page fault, the entire process is
blocked.

P.Ray@CSE
Kernel level Threads
In Kernel level threads the kernel does total
work of thread movement. There is no thread
table in each process.
The kernel has a thread table that keeps track of
all the threads in system.
When a thread wants to create a new thread or
destroy any existing thread, it makes a kernel
call, which takes the action.

P.Ray@CSE
The kernel's thread table holds each thread
registers, state and other information.
The information is the same as with user
level threads, but it is now in the kernel
instead of the user space.
Consider the below figure for better
understanding

P.Ray@CSE
P.Ray@CSE
Kernel-Level Threads

P.Ray@CSE
Advantages
1. The kernel can simultaneously schedule multiple threads from
the same process on multiple processors.
2. It means it supports multi processing where as user level
threads can't support this.
3. A thread in a process is blocked; the kernel can schedule
another thread of the same process.
4. Scheduling by kernel is done on a thread bus.
5. The kernel-level thread is fully aware of all threads.
6. The scheduler may decide to spend more CPU time in the
process of threads being large numerical.
7. The kernel-level thread is good for those applications that
block the frequency.

P.Ray@CSE
Disadvantages
1. More cost for creating and destroying threads in the
kernel.
2. The transfer of control from one thread to another
(switching) with in the same process requires a mode
switch to the kernel. i.e Kernel must do the switching.
3. The kernel thread manages and schedules all threads.
4. The implementation of kernel threads is difficult than
the user thread.
5. The kernel-level thread is slower than user-level
threads.

P.Ray@CSE
User Level Thread Kernel Level Thread
User-level threads are faster to create and Kernel level threads are slower to create
manage. and manage.
Implemented by a thread library user level. Operating system support directly to Kernel
threads.
User level thread can run on any operating Kernel level threads are specific to .the
system. operating system.
Support provided at the user level called Support may be provided by Kernel is called
user-level thread Kernel level threads
Implementation of User threads is easy. Implementation of Kernel thread is
complicated.
User level threads are also called many to Kernel level thread support one to one
one mapping thread. thread mapping.
Context switch time is less. Context switch time is more.

Context switch requires no hardware Hardware support is needed.


support.
Example: User-thread libraries include Example: Windows NT, Windows 2000,
POSIX Pthreads, Mach C-threads, and Solaris 2, BeOS, and Tru64 UNIX (formerly
Solaris 2 UI-threads. Digital UNIX)-support kernel threads.
P.Ray@CSE
Combined Approaches

P.Ray@CSE
Micro Kernels
A micro kernel is a software program, loaded in
main memory, to perform some of the
functions of the operating system. So the size of
the operating system can be reduced.
Micro kernel provides process scheduling,
memory management, and communication
services.
Initially micro kernel approach used in the
'MACH' operating system, then it is populated,
after that this approach used in windows 2000.
P.Ray@CSE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy