Chapter 5 Threads Overview Multithreading Models Thread Libraries

Chapter 5: Threads Overview Multithreading Models Thread Libraries Thread Pools Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 1

Overview Many software packages are multi-threaded l l l Web browser: one thread display images, another thread retrieves data from the network Word processor: threads for displaying graphics, reading keystrokes from the user, performing spelling and grammar checking in the background Web server: instead of creating a process when a request is received, which is time consuming and resource intensive, server creates a thread to service the request A thread is sometimes called a lightweight process l l l It is comprised over a thread ID, program counter, a register set and a stack It shares with other threads belonging to the same process its code section, data section and other OS resources (e. g. , open files) A process that has multiples threads can do more than one task at a time Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 2

Single and Multithreaded Processes Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 3

Benefits Responsiveness l One part of a program can continue running even if another part is blocked Resource Sharing l Threads of the same process share the same memory space and resources Economy l l Much less time consuming to create and manage threads than processes Solaris 2: creating a process is 30 times slower than creating a thread, context switching is 5 times slower Utilization of MP Architectures l Each thread can run in parallel on a different processor Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 4

User Threads Thread management done by user-level threads library is without the intervention of the kernel l l Fast to create and manage If the kernel is single threaded, any user-level thread performing a blocking system call will cause the entire process to block User thread libraries - POSIX Pthreads - Mach C-threads - Solaris 2 UI-threads Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 5

Kernel Threads Supported by the Kernel l Slower to create and manage than user threads If thread performs a blocking system call, the kernel can schedule another thread in the application for execution In multi-processor environments, the kernel can schedule threads on multiple processors Examples - Windows 95/98/NT/2000 - Solaris - Tru 64 UNIX - Be. OS - Linux Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 6

Multithreading Models Many-to-One One-to-One Many-to-Many Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 7

Many-to-One Many user-level threads mapped to single kernel thread. l Efficient - thread management done in user space l Entire process will block if a thread makes a blocking system call l Only one thread can access the kernel, no parallel processing in MP environment GNU Portable threads and Green threads (thread library from Solaris) use this model Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 8

One-to-One Each user-level thread maps to kernel thread. l l Another thread can run when one thread makes a blocking call Multiple threads can run in parallel on a MP machine Overhead of creating a kernel thread for each user thread Most implementations limit the number of threads supported Examples - Windows 95/98/NT/2000 - OS/2 - Linux Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 9

Many-to-Many Model Allows many user level threads to be mapped to smaller or equal number of kernel threads. As many user threads as necessary can be created Corresponding kernel threads can run in parallel on a multiprocessor When a thread performs a blocking system call, the kernel can schedule another thread for execution Solaris 9, Windows NT/2000 with the Thread. Fiber package Allows true concurrency in a MP environment and does not restrict number of threads that can be created Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 10

Two Level Model Has all the functionality of the many-to many model but also allows a user-level thread to be bound to a kernel thread Supported by Solaris prior to version 9, IRIX, HP-UX and Tru 64 UNIX Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 11

Thread Libraries API for creating and managing threads l l No kernel support, strictly in use space so no system calls involved Kernel level directly supported by the OS. All code and data structures for the library exists in kernel space l l An API call typically invokes a system call Three main libraries in use l l l POSIX (Portable Operating System Interface) threads Win 32 Java Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 12

Java Threads Java threads may be created by extending the Thread class or implementing the Runnable interface Java threads are managed by the JVM l Not really a user- or kernel-level thread, support provided at the language level l No global data in Java, data is shared by passing the reference to the appropriate threads l The JVM specification does not indicate how threads should be mapped to the underlying OS l l l Windows 95/98/NT/2000 use the one-to-one model (each Java thread maps to a kernel thread) Solaris 2 used the many-to-many model See Producer – Consumer Example Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 13

Java Threads public class Buffer { final static int BUFFER_SIZE = 5; static int buffer[] = {-1, -1, -1}; static int out=0; } public class Producer extends Thread{ public void run(){ for(int i=0; i<10; i++){ while (((Buffer. in+1)%Buffer. BUFFER_SIZE) == Buffer. out); System. out. print("n. Index " + Buffer. in + " produced "); Buffer. buffer[Buffer. in] = (int)(Math. random()*100); for (int j=0; j<=Buffer. buffer. length-1; j++) System. out. print("["+Buffer. buffer[j]+"] "); public class Prod. Con. Driver { public static void main(String args[]){ Producer prod = new Producer(); Consumer cons = new Consumer(); prod. start(); cons. start(); } } Buffer. in = (Buffer. in+1)%Buffer. BUFFER_SIZE; } } } public class Consumer extends Thread{ public void run(){ for(int i=0; i<10; i++){ while (Buffer. in == Buffer. out); System. out. print("n. Index " + Buffer. out + " consumed "); Buffer. buffer[Buffer. out] = -1; for (int j=0; j<=Buffer. buffer. length-1; j++) System. out. print("["+Buffer. buffer[j]+"] "); Buffer. out = (Buffer. out+1)%Buffer. BUFFER_SIZE; } } } Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 14

Java Thread States Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 15

Thread Pools Though creating a new thread is more efficient than creating a new process, there are still some issues l There is a delay in creating the thread and it will be discarded when done l There is no limit put on how many threads are allowed to be created and system resources could be exhausted Create a number of threads in a pool where they await work. When needed, thread is awoken, when finished returns to pool to await more work Advantages: l Usually slightly faster to service a request with an existing thread than create a new thread l Allows the number of threads in the application(s) to be bound to the size of the pool l Size of the pool can be dynamically adjusted based on usage patterns Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 16
![Matrix Multiplication with Java Threads import java. util. Random; public void print. Matrix(int[][] M) Matrix Multiplication with Java Threads import java. util. Random; public void print. Matrix(int[][] M)](http://slidetodoc.com/presentation_image_h2/222f9081750502a20164b37feefb59bf/image-17.jpg)
Matrix Multiplication with Java Threads import java. util. Random; public void print. Matrix(int[][] M) { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) System. out. print("t"+M[i][j]); System. out. println(); } } public class Matrix { public static int[][] A, B, C; public int n; public Matrix() { n = 3; A = new int[n][n]; B = new int[n][n]; Random r = new Random(); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { A[i][j] = r. next. Int() % 10; B[i][j] = r. next. Int() % 10; } } C = new int[n][n]; public void do. Work() { Mult. Thread[][] mt = new Mult. Thread[n][n]; for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) { mt[i][j] = new Mult. Thread(3, i, j); mt[i][j]. start(); } public static void main(String args[]) { Matrix m = new Matrix(); m. do. Work(); } } // end of class Matrix public class Mult. Thread extends Thread { private int n; // n*n matrix private int i, j; // row, column public Mult. Thread(int size, int ii, int jj) { n = size; i = ii; j = jj; System. out. println("Thread for C["+i+"]["+j+"]"); try { for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) mt[i][j]. join(); } catch(Interrupted. Exception ie) {} System. out. println("n. A ="); System. out. println("n. B ="); System. out. println("n. C ="); print. Matrix(A); print. Matrix(B); print. Matrix(C); } public void run() { Matrix. C[i][j] = 0; for(int x = 0; x < n; x++) Matrix. C[i][j] += Matrix. A[i][x] * Matrix. B[x][j]; } } } Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 17
![Matrix Multiplication with Java Threads Thread for C[0][0] Thread for C[0][1] Thread for C[0][2] Matrix Multiplication with Java Threads Thread for C[0][0] Thread for C[0][1] Thread for C[0][2]](http://slidetodoc.com/presentation_image_h2/222f9081750502a20164b37feefb59bf/image-18.jpg)
Matrix Multiplication with Java Threads Thread for C[0][0] Thread for C[0][1] Thread for C[0][2] Thread for C[1][0] Thread for C[1][1] Thread for C[1][2] Thread for C[2][0] Thread for C[2][1] Thread for C[2][2] A = -4 8 3 5 -4 -9 -8 -7 6 5 -7 -3 6 0 8 0 0 1 -88 -8 66 -8 -7 6 B = C = -31 89 60 Silberschatz / OS Concepts / 6 e - Chapter 5 Threads Slide 18
- Slides: 18