cs presentation template · ("1:1") and user-level ("n:1") threading. many to...
TRANSCRIPT
Department of Computer Science
DCS
COMSATS Institute of Information Technology
Threads
Rab Nawaz JadoonAssistant Professor
COMSATS Lahore
Pakistan
Operating System Concepts
Department of Computer Science 2
Threads
Definitions
In the previous discussion, a process was an
executing program with a single thread of control.
Most modern OS now provide features enabling a process to contain multiple threads of control.
A thread is a basic unit of CPU utilization.
It is a light weight process.
If a process has multiple thread of control, it can
perform more than one task at a time.
Seamless execution of two or more sections of a program at the same time. Threads allows us to do
this.
Department of Computer Science
Process, threads
3
What is the relationship of a thread, a process and a program?
Department of Computer Science
Threads
Writing multithreaded programs can be tricky,
Although human mind can function concurrently,
people find it difficult to jump between parallel thoughts.
Why it is difficult to handled multithreaded
applications or programs, just see the example,
Open three books to page 1, and try reading the books concurrently,
Read a few words from book 1, then from 2 and then from 3rd one.
Execute the same work repeatedly.
After this experiment, you will appreciate some of the key challenges of the multithreading.
4
Department of Computer Science
Threads
i.e. switching between books, reading briefly,
remembering your place in each book etc are
hectic at the end.
5
Department of Computer Science
Threads
Thread possesses a subset of the resources
contained in a process, such as,
Processor registers,
Stack and other,
TSD (thread specific data)
Such as signal masks (data that describes which signals a thread will not receive)
Many OS supports threads, Win32 threads used by MS-32 bit Windows.
C-threads these are created from a thread library in the MAC Microkernel.
POSIX threads Pthread standards
Java threads Thread Class / runnable interface
6
Department of Computer Science
Threads
The primary goad of POSIX is to allow
Multithreaded programs to be portable across
multiple OS platforms.
POSIX has been implemented in a variety of
OS, Including,
Solaris
Linux
Windows XP etc.
7
Department of Computer Science
Threads
Single threaded & multithreaded processes
8
Department of Computer Science
Question???
Why is it difficult to write portable multithreaded
applications?
9
ANSWER!There is no standard threading library that is implemented on all platforms.
Department of Computer Science
Threads
Many s/w packages that run on modern desktop
PCs are mulithreaded.
An application typically is implemented as a separate
process with several threads of control.
For example a web browser might have one thread displaying images while other retrieves data from the network.
Next example is of word processor may have
displaying graphics, another thread for responding the keystrokes from the user and a 3rd thread for
performing spelling and grammar checking in the background.
10
Department of Computer Science
Threads
Benefits
Responsiveness
Multithreading interactive application may allow a program to continue running even if a part of it is blocked or is performing a lengthy operation.
For example, a multithreaded web browser could still allow user interaction in one thread while an image was being loaded in another thread.
Resource sharing
By default, threads share the memory and the resources of the process to which they belong.
The benefits of sharing code and data is that it allows an application to have several different threads of activity within the same address space.
11
Department of Computer Science
Threads
Economy
Allocating memory and resources for process
creation is costly.
It is more economical to create and context switch threads.
In Solaris, creating a process is about thirty times
slower than is a creating a thread, and context switching is about five time slower.
12
Department of Computer Science
Threads
Utilization of multiprocessor architecture
The benefit of multithreading can be greatly
increased in a multiprocessor architecture, where threads may be running parallel on different
processor.
A single threaded process can only run on one CPU, no matter how many are available.
Multithreading on a multi CPU machine increases concurrency.
13
Department of Computer Science
Threads
Different threads in a process are not
independent as different processes.
All threads have exactly the same address space
which means that they share the same global variables.
14
Department of Computer Science
Thread states
Like a traditional process, a thread can be in
any one of several states,
15
States Description
Ready The thread is ready to run and waiting for the CPU.
Running The thread is executing on the CPU.
Waiting The thread is waiting for some event to happen.
Sleeping The thread has been told to sleep for a time
Blocked The thread is waiting for I/O to finish
dead The thread is terminated
Department of Computer Science
Thread life cycle (JAVA)
16
Born
Department of Computer Science
Thread stack
17
Department of Computer Science
Thread states
If a process is swapped out, all of its threads
are swapped out, b/c they all share the same
address space of the process.
18
Department of Computer Science
Thread Operations
Threads and processes have many operations in
common such as,
Create
Exit
Suspend
Resume
Sleep
Wake
Etc.
19
Department of Computer Science
Threading Models
Almost all OS’s support one of three primary
threading models,
User level threads (ULTs)
Kernel level threads (KLTs)
Combination of ULTs and KLTs
20
Department of Computer Science
User Level Threads
User Level Threads
These performs operation in user space.
These are created by run time libraries that cannot
access kernel primitives directly.
These are transparent to the OS.
Single execution context i.e. OS dispatches the multithreaded process as a unit, as opposed to
dispatching each individual thread.
For this reason ULTs are also called many to one thread mapping, bc OS maps all threads in a
multithreaded process to a single execution context.
21
Department of Computer Science
User level thread
22
Department of Computer Science
User level threads
In ULTs, OS is unaware that process contains
multiple threads.
In ULTs the multithreaded process continues until its quantum expires.
ULTs are more portable b/c they do not rely on
a particular OS’s threading API.
Application developers can tune the threading
library’s scheduling algorithm to meet the needs of specific applications.
23
Department of Computer Science
ULTs
ULTs presents an API to applications that is independent of the operating system APIs.
Deficiencies
Kernel views a multithreaded process as a single thread of control.
For instance, ULTs do not scale well to multiprocessor systems, b/c the kernel can not simultaneously dispatch a process thread to multiple
processors.
So ULTs can result in suboptimal performance in multiprocessor systems.
24
Department of Computer Science
Kernel Level threads
The KLTs are used to address the limitations in
ULTs.
This is done by mapping each thread to its own
execution context.
As a result KLTs are often described as one to one thread mapping.
Such mappings require the OS to provide each user thread with a kernel thread that the OS can dispatch.
When a user requests a KLT using system calls defined by OS’s API, the OS create a KLT that executes the user thread’s instructions.
25
Department of Computer Science
Kernel level thread
26
Department of Computer Science
Benefits of KTLs
The kernel can dispatch a process’s threads to
several processors at one, which can improve
performance for application designed for
concurrent execution.
Kernel can manage each thread individually,
meaning that OS can dispatch a process ‘s
ready threads even if one of its thread is
blocked.
27
Department of Computer Science
Difficiencies
KLTs are not always the optimal solution for
multithreaded applications.
KLTs implementation is less efficient than ULTs implementation b/c scheduling and
synchronization operations invoke the kernel
which increases overhead.
Software that employs KLTs, are less portable
than the S/W that employs ULTs – b/c
The application programmer that uses KLTs must modify the program to use the thread API for each
OS on which it runs.
28
Department of Computer Science
Hybrid Threading
This is a compromise between kernel-level
("1:1") and user-level ("N:1") threading.
Many to many association mechanism.
In general, "M:N" threading systems are more
complex to implement than either kernel or userthreads, because changes to both kernel and user-space code are required.
In the M:N implementation, the threading library is
responsible for scheduling user threads on theavailable schedulable entities; this makes context
switching of threads very fast, as it avoids systemcalls.
29
Department of Computer Science
Hybrid Threading Model
30
T1 T2 T3 T4 T1 T2 T3 T1 T2
User
Sp
ace
Kern
el
Sp
ace
Process P1 Process P2 Process P3
Thread
Execution Context
Department of Computer Science
Threads Implementation(JAVA)
public class ThreadTester {
public static void main(String args[])
{
//create and name each thread.
PrintThread thread1 = new PrintThread();
PrintThread thread2 = new PrintThread();
PrintThread thread3 = new PrintThread();
system.err.println("starting thread");
thread1.start();
thread2.start();
thread3.start();
system.err.println("Threads Started, main ended");
}//end of main
}//end class ThreadTester
31
Department of Computer Science
Threads in JAVA
// Class PrintThread controls thread execution.
Class PrintThread extends Thread {
private int sleepTime;
//assign name to threads by calling super class constructor.
public PrintThread ( Stirng name )
{
super(name);
//pick random sleep time between 0 and 5 second
sleepTime = ( int ) ( math.random ( ) * 5001);
}
32
Department of Computer Science
Threads in JAVA
pubic void run()
{
try {
system.err.println(getName() + "going to sleep for " + sleep time + "milliseconds");
thread.sleep(sleeptime);
} //end try
catch(InterruptedException e)
{
e.printStackTrace();
} //end catch
system.err.println(getName() + "done sleeping");
}//end method run
}// end class PrintThread.
33
Department of Computer Science
Output
34
Starting threadsTreads started, main ends
thread1 going to sleep for 1217 millisecondsthread2 going to sleep for 3989 millisecondsthread3 going to sleep for 662 milliseconds
thread3 done sleepingthread1 done sleepingthread2 done sleeping
Department of Computer Science 35