Threads Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS Lahore Pakistan Operating System Concepts
Definitions Threads 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. 2
Process, threads What is the relationship of a thread, a process and a program? 3
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 3 rd one. Execute the same work repeatedly. After this experiment, you will appreciate some of the key challenges of the multithreading. 4
Threads i.e. switching between books, reading briefly, remembering your place in each book etc are hectic at the end. 5
Thread possesses a subset of the resources contained in a process, such as, Processor registers, Stack and other, TSD (thread specific data) Threads 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
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
Single threaded & multithreaded processes Threads 8
Question??? Why is it difficult to write portable multithreaded applications? ANSWER! There is no standard threading library that is implemented on all platforms. 9
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 3 rd thread for performing spelling and grammar checking in the background. 10
Benefits Responsiveness Threads 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
Economy Allocating memory and resources for process creation is costly. Threads 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
Utilization of multiprocessor architecture Threads 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
Different threads in a process are not independent as different processes. Threads All threads have exactly the same address space which means that they share the same global variables. 14
Thread states Like a traditional process, a thread can be in any one of several states, States Ready Running Waiting Sleeping Blocked dead Description The thread is ready to run and waiting for the CPU. The thread is executing on the CPU. The thread is waiting for some event to happen. The thread has been told to sleep for a time The thread is waiting for I/O to finish The thread is terminated 15
Born Thread life cycle (JAVA) 16
Thread stack 17
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
Thread Operations Threads and processes have many operations in common such as, Create Exit Suspend Resume Sleep Wake Etc. 19
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
User Level Threads These performs operation in user space. User Level Threads 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
User level thread 22
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
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
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
Kernel level thread 26
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
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
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 user threads, because changes to both kernel and userspace 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. 29
Kernel Space User Space Hybrid Threading Model Process P1 Process P2 Process P3 T1 T2 T3 T4 T1 T2 T3 T1 T2 Thread Execution Context 30
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
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
pubic void run() { try { Threads in JAVA 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
Output 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 34
35