Vous êtes sur la page 1sur 19

TOPIC: THREADS

AUTHOR: SILBERCHATZ ,
GALVIN AND

GAGNE

PRESENTED
BY: HEENAZ

Topics covered:
Threads
Difference

between threads and

processes
Types of processes with threads
Benefits of multithreaded
programming
User and kernel threads
Threading implementations
Threading Issues

Threads:
Relatively

recent development in

OS.
Light weight process which is a
basic unit of CPU utilization.
Process with single thread is
called as classical process.
Each thread is associated with a
single process ( sequential flow of
control ).
Takes over the role of process as

processes:
Processes are typically
independent, while threads exist
as subsets of a process.
Processes carry considerably
moreinformation than threads,
whereas multiple threads within a
process share memory as well as
otherresources.
Processes have separateaddress
space, whereas threads share
their address space.

Types of processes with threads:


1.Single threaded process
2. Multithreaded process
Single threaded
process:
code

registers

data

files
stack

Thread

Multithreaded
process:
hread may
display image while other thread may retrieve th

a network.
code
registe
rs
stac
k

data
register
stack

files
register
stack

Thread

Benefits of multithreaded
programming:

Responsiveness: Multithreading allows to


continue running even if part of it is blocked or is
performing a lengthy operation, thereby
increasing responsiveness to the user.
E g .a web browser allows user interaction with
one thread while the image is loading in the other
thread.
Resource sharing: Threads share resources
and memory of the process to which it belong
(several threads within the same address space).
Economy: It is economical to create switch
threads.
Utilization of the multiprocessor
architecture: Each thread runs parallely on

User and Kernel threads:

r threads : ( visible to programmer and are unkonwn to Ke

- implemented by the thread library at the user leve


- provides support for thread creation, scheduling an
management with no support from the kernel.
- fast to create and manage.
Process
User Space
Thread
Thread-Table
Kernel Space

nel threads: - supported directly by the operating system


- performs thread creation, scheduling, and
management in Kernel space.
- slower to create and manage.

Types of threading implementation


1. Many-to-one Model
2. One-to-one Model
3. Many-to-many Model

Many-to-one Model

ps many user level threads to one Kernel thread.


s done in the user space.
hread makes a blocking system call, entire process will block
one thread can access the kernel at a time (multiple threads
ble to run in parallel).

One-to-one Model
It maps each user thread to kernel thread.
If a thread makes a blocking system call, then it allows
the other thread to run parallely.
Drawback: Creating a user thread requires creating
the corresponding Kernel thread.

Many-to-many Model
It maps many user level threads to smaller or equal
Kernel threads.
We can create as many user threads as necessary and
the corresponding Kernel threads run in parallel on
multiprocessor.

Threading Issues:
1.Fork system call
2.Cancellation
3.Signal Handling
4.Thread Pools

Fork system call:

New process is created.


Copy of the address space.
Easy communication of parent with its child process.
Process identifier of the child is returned to the paren

xec system call:

sed after the fork system call by one of the two proc
to replace the process memory space.

Cancellation:
It is the task of terminating a thread before it has
completed.
Eg. If multiple threads are searching through the
data base and one thread returns the result, the
remaining threads might be cancelled.
The thread which is to be cancelled is known as
target thread.
Two scenarios:
1)Asynchronous cancellation: One thread
immediately terminates the target thread.
2) Defered cancellation: Allowing the target

Signal Handling:
It is used to notify a process that a particular event
has occured.
The signal may occur synchronously or
asynchronously.
All signals follow the folowing pattern:
A signal is generated by the occurence of the
particular event.
A generated
signal is delivered to a process.
Thread
Pools:
Once delivered, the signal must be handled.
The idea is to create the threads at process startup
and place them into a pool.
When a request is received, the thread becomes active
and completes its service.

Java
Threads:
Threads
are managed by Java Virtual
Machine.
Difficult to classify whether the thread
belongs to user level or Kernel level.
Each process has its own thread.
Even a simple java program consisting of only
a main method runs as a single thread in the
JVM.

Thread Creation:
One way to create a thread explicitly is to create a new
class that is derived from the Thread class, and to
override the run method of the Thread class .
Calling the start method for the new object does two
things:
1. It allocates memory and initializes a new thread in
the JVM.
2. It calls the run method, making the thread eligible to
be run by the JVM.
When the summation program runs, two threads are
created by the JVM. The first is the thread associated
with the application-the thread that starts execution
at the main method. The second thread is the
summation thread that is created explicitly with the

Class summation extends Thread


{
public summation ( int n )
{
upper = n;
}
public void run()
{
int sum = 0;
if ( upper > 0)
{
for ( int i = 1; i <= upper; i++)
sum += i;
}
System.out.println(The sum of
+upper+ is +sum);
}
private int upper;
}
Public class ThreadTester
{
public static void main(String[] args)
{
if ( args.length > 0)
{

if ( Integer.parseInt (args[0]) < 0)


System.err.println(args[0] + must be >=0.);
else
{
Summation thrd = new Summation( Integer.parseInt ( arg
thrd.start();
}
}
else
System.err.println ( Usage: Summation <integer value>
}
}

Java program for the summation of a non-negative in

u
o
y
k
n
a
h
T

Vous aimerez peut-être aussi