0% found this document useful (0 votes)
98 views

Software Requirements Specification Srs

The document discusses threads and threading models in operating systems. It defines a thread as a basic unit of CPU utilization that allows a process to perform multiple tasks simultaneously. There are three main threading models: user-level threads managed by user-space libraries, kernel-level threads with one-to-one mapping to kernel threads, and a combination of both. Kernel-level threads allow better performance on multiprocessor systems but with less efficiency than user-level threads.

Uploaded by

awais sarwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Software Requirements Specification Srs

The document discusses threads and threading models in operating systems. It defines a thread as a basic unit of CPU utilization that allows a process to perform multiple tasks simultaneously. There are three main threading models: user-level threads managed by user-space libraries, kernel-level threads with one-to-one mapping to kernel threads, and a combination of both. Kernel-level threads allow better performance on multiprocessor systems but with less efficiency than user-level threads.

Uploaded by

awais sarwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

Operating

Systems
LECTURE 11-12
Threads 2

 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.
Process, threads 3

What is the relationship of a thread, a process and a program?


Threads 4

 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.
Threads 5

 i.e. switching between books, reading briefly,


remembering your place in each book etc are
hectic at the end.
Threads 6

 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
Threads 7

 The primary goal 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.
Threads 8

 Single threaded & multithreaded processes


Question??? 9

 Why is it difficult to write portable multithreaded applications?

ANSWER!
There is no standard threading library that is implemented on
all platforms.
Threads 10

 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.
Threads 11

 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.
Threads 12

 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.
Threads 13

 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.
Threads 14

 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.
Thread states 15

 Like a traditional process, a thread can be in any one of


several states,

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
Thread life cycle (JAVA) 16

Born
Thread stack 17
Thread states 18

 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.
Thread Operations 19

 Threads and processes have many operations in


common such as,
 Create
 Exit
 Suspend
 Resume
 Sleep
 Wake
 Etc.
Threading Models 20

 Almost all OS’s support one of three primary threading


models,
 User level threads (ULTs)
 Kernel level threads (KLTs)
 Combination of ULTs and KLTs
User Level Threads 21

 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.
User level thread 22
User level threads 23

 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.
ULTs 24

 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.
Kernel Level threads 25

 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.
Kernel level thread 26
Benefits of KTLs 27

 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.
Difficiencies 28

 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.
Hybrid Threading 29

 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 user threads, 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 the available schedulable entities;
this makes context switching of threads very fast, as it avoids
system calls.
Hybrid Threading Model 30

Process P1 Process P2 Process P3

T1 T2 T3 T4 T1 T2 T3
User Space

T1 T2
Kernel Space

 Thread

 Execution Context
Threads
Implementation(JAVA)
31

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

}
Threads in JAVA 32

// 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);
}
Threads in JAVA 33

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.
Output 34

Starting threads
Treads started, main ends

thread1 going to sleep for 1217 milliseconds


thread2 going to sleep for 3989 milliseconds
thread3 going to sleep for 662 milliseconds

thread3 done sleeping


thread1 done sleeping
thread2 done sleeping

You might also like