Chapter 4 Threads Operating System Concepts 8 th











































- Slides: 43

Chapter 4: Threads Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

Chapter 4: Threads Topics: n Overview n Multithreading Models n Thread Libraries n Threading Issues n Operating System Examples n Windows XP Threads n Linux Threads Objectives: n To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems n To discuss the APIs for the Pthreads, Win 32, and Java thread libraries n To examine issues related to multithreaded programming Operating System Concepts – 8 th Edition 4. 2 Silberschatz, Galvin and Gagne © 2009

Threads Motivation n Threads run within application n Multiple tasks with the application can be implemented by separate threads l Update display l Fetch data l Spell checking l Answer a network request n Process creation is heavy-weight while thread creation is light- weight n Can simplify code, increase efficiency n Kernels are generally multithreaded Operating System Concepts – 8 th Edition 4. 3 Silberschatz, Galvin and Gagne © 2009

Benefits n Responsiveness n Resource Sharing n Economy n Scalability Operating System Concepts – 8 th Edition 4. 4 Silberschatz, Galvin and Gagne © 2009

Question n Which pieces of Process info: information from a process can be shared by all threads of a process and which ones need to be unique? n State n Program counter n Registers n Open files n Stack n Text section (code) n Data Operating System Concepts – 8 th Edition 4. 5 Silberschatz, Galvin and Gagne © 2009

Single and Multithreaded Processes Operating System Concepts – 8 th Edition 4. 6 Silberschatz, Galvin and Gagne © 2009

Multithreaded Server Architecture Operating System Concepts – 8 th Edition 4. 7 Silberschatz, Galvin and Gagne © 2009

Multicore Programming n Multicore systems putting pressure on programmers, challenges include: l Dividing activities l Balance l Data splitting l Data dependency l Testing and debugging Operating System Concepts – 8 th Edition 4. 8 Silberschatz, Galvin and Gagne © 2009

Concurrency Single-core System Multicore System Operating System Concepts – 8 th Edition 4. 9 Silberschatz, Galvin and Gagne © 2009

MULTITHREADING MODELS Operating System Concepts – 8 th Edition 4. 10 Silberschatz, Galvin and Gagne © 2009

User Threads n Thread management done by user-level threads library n Three primary thread libraries: l POSIX Pthreads l Win 32 threads l Java threads Operating System Concepts – 8 th Edition 4. 11 Silberschatz, Galvin and Gagne © 2009

Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris l Linux l Tru 64 UNIX l Mac OS X Operating System Concepts – 8 th Edition 4. 12 Silberschatz, Galvin and Gagne © 2009

Multithreading Models n Many-to-One n One-to-One n Many-to-Many l Two-level n Main considerations: ① Limited number of threads? ② Concurrency: What if a thread makes a blocking system call? ③ Allows for parallel execution on multiprocessor machines? Operating System Concepts – 8 th Edition 4. 13 Silberschatz, Galvin and Gagne © 2009

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris Green Threads l GNU Portable Threads Operating System Concepts – 8 th Edition 4. 14 Silberschatz, Galvin and Gagne © 2009

One-to-One n Each user-level thread maps to kernel thread n Examples: Windows NT/XP/2000, Linux, Solaris 9 and later Operating System Concepts – 8 th Edition 4. 15 Silberschatz, Galvin and Gagne © 2009

Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n Allows the operating system to create a sufficient number of kernel threads n Examples: l Solaris prior to version 9 l Windows NT/2000 with the Thread. Fiber package Operating System Concepts – 8 th Edition 4. 16 Silberschatz, Galvin and Gagne © 2009

Two-level Model n Similar to M: M, except that it allows a user thread to be bound to kernel thread n Examples l IRIX l HP-UX l Tru 64 UNIX l Solaris 8 and earlier Operating System Concepts – 8 th Edition 4. 17 Silberschatz, Galvin and Gagne © 2009

THREAD LIBRARIES Operating System Concepts – 8 th Edition 4. 18 Silberschatz, Galvin and Gagne © 2009

Thread Libraries n Thread library provides programmer with API for creating and managing threads n Two primary ways of implementing l Library entirely in user space l Kernel-level library supported by the OS Operating System Concepts – 8 th Edition 4. 19 Silberschatz, Galvin and Gagne © 2009

Pthreads n May be provided either as user-level or kernel-level n A POSIX standard (IEEE 1003. 1 c) API for thread creation and synchronization n API specifies behavior of the thread library, implementation is up to development of the library l Policy vs. mechanism n Common in UNIX operating systems (Solaris, Linux, Mac OS X & in Windows via shareware implementations) Operating System Concepts – 8 th Edition 4. 20 Silberschatz, Galvin and Gagne © 2009

Examples n Pthreads l http: //www. cs. mtsu. edu/~hcarroll/3250/private/code/chapter 0 4/pthreads. Example. c n Win 32 API l http: //www. cs. mtsu. edu/~hcarroll/3250/private/code/chapter 0 4/win 32 Threads. Example. c n Java l http: //www. cs. mtsu. edu/~hcarroll/3250/private/code/chapter 0 4/Driver. java Operating System Concepts – 8 th Edition 4. 21 Silberschatz, Galvin and Gagne © 2009

Java Threads n Java threads are managed by the JVM n Typically implemented using the threads model provided by underlying OS n Java threads may be created by: 1. Extending Thread class 2. Implementing the Runnable interface Operating System Concepts – 8 th Edition 4. 22 Silberschatz, Galvin and Gagne © 2009

Tuesday, January 31, 2012 n lab 1 – graded copies pushed out today @ 4: 00 PM n lab 2 – due tomorrow n hwk 04 – due Thursday Operating System Concepts – 8 th Edition 4. 23 Silberschatz, Galvin and Gagne © 2009

THREADING ISSUES Operating System Concepts – 8 th Edition 4. 24 Silberschatz, Galvin and Gagne © 2009

Threading Issues n Scheduler activations n Semantics of fork() and exec() system calls n Thread cancellation of target thread l Asynchronous or deferred n Signal handling l Synchronous and asynchronous n Thread pools n Thread-specific data n Create Facility needed for data private to thread Operating System Concepts – 8 th Edition 4. 25 Silberschatz, Galvin and Gagne © 2009

Scheduler Activations n Both many-to-one and many-to-many models require communication to maintain the appropriate number of kernel threads allocated to the application n Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library n This communication allows an application to maintain the correct number kernel threads Operating System Concepts – 8 th Edition 4. 26 Silberschatz, Galvin and Gagne © 2009

Lightweight Processes n Virtual processor (LWP) between thread library (user thread(s)) and kernel n Not present in all OSes l In some contexts, LWP refers to the kernel thread Operating System Concepts – 8 th Edition 4. 27 Silberschatz, Galvin and Gagne © 2009

Tracking Threads on ranger n ps –e. Lf command: $ ps -e. Lf | head -n 1; ps -e. Lf | grep $USER UID PPID LWP C NLWP STIME TTY TIME CMD hcarroll 13291 14715 13291 0 2 13: 23 pts/22 00: 00. /pthreads. Example 555555 hcarroll 13291 14715 13292 98 2 13: 23 pts/22 00: 31. /pthreads. Example 555555 hcarroll 13458 14715 13458 0 1 13: 24 pts/22 00: 00 ps -e. Lf hcarroll 13459 14715 13459 0 1 13: 24 pts/22 00: 00 grep hcarroll root 14712 1 14712 0 1 12: 21 ? 00: 00 sshd: hcarroll [priv] hcarroll 14714 14712 14714 0 1 12: 21 ? hcarroll 14715 14714 14715 0 1 12: 21 pts/22 00: 00 -bash hcarroll 30081 14715 30081 0 1 12: 45 pts/22 00: 00 emacs Operating System Concepts – 8 th Edition 00: 00 sshd: hcarroll@pts/22 4. 28 Silberschatz, Galvin and Gagne © 2009

Semantics of fork() and exec() n What does fork() do? n What does exec() do? n Does fork() duplicate only the calling thread or all threads for that process? l Discuss with your neighbor(s) how can you test this? Operating System Concepts – 8 th Edition 4. 29 Silberschatz, Galvin and Gagne © 2009

Multi-processes with Multi-threads n http: //www. cs. mtsu. edu/~hcarroll/3250/private/code/chapter 04/web. Server. Skeleton. c n http: //www. cs. mtsu. edu/~hcarroll/3250/private/code/chapter 04/web. Server. Skeleton-with. Fork. c n ps -e. Lf | head -n 1; ps -e. Lf | grep $USER Operating System Concepts – 8 th Edition 4. 30 Silberschatz, Galvin and Gagne © 2009

Thread Cancellation n Terminating a thread before it has finished n Two general approaches: l Asynchronous cancellation terminates the target thread immediately. l Deferred cancellation allows the target thread to periodically check if it should be cancelled. Operating System Concepts – 8 th Edition 4. 31 Silberschatz, Galvin and Gagne © 2009

Signal Handling n Signals are used in UNIX systems to notify a process that a particular event has occurred. n A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled n Options for multi-threaded processes: l Deliver the signal to the thread to which the signal applies l Deliver the signal to every thread in the process l Deliver the signal to certain threads in the process l Assign a specific thread to receive all signals for the process Operating System Concepts – 8 th Edition 4. 32 Silberschatz, Galvin and Gagne © 2009

Thread Pools n Create a number of threads in a pool where they await work n 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 Operating System Concepts – 8 th Edition 4. 33 Silberschatz, Galvin and Gagne © 2009

Thread Specific Data n Allows each thread to have its own copy of data n Useful when you do not have control over the thread creation process (i. e. , when using a thread pool) Operating System Concepts – 8 th Edition 4. 34 Silberschatz, Galvin and Gagne © 2009

OPERATING SYSTEM EXAMPLES Operating System Concepts – 8 th Edition 4. 35 Silberschatz, Galvin and Gagne © 2009

Windows XP Threads n Implements the one-to-one mapping, kernel-level n Each thread contains l A thread id l Register set l Separate user and kernel stacks l Private data storage area n The register set, stacks, and private storage area are known as the context of the threads n The primary data structures of a thread include: l ETHREAD (executive thread block) l KTHREAD (kernel thread block) l TEB (thread environment block) Operating System Concepts – 8 th Edition 4. 36 Silberschatz, Galvin and Gagne © 2009

Windows XP Threads Data Structures Operating System Concepts – 8 th Edition 4. 37 Silberschatz, Galvin and Gagne © 2009

Linux Threads n fork() and clone() system calls n Doesn’t distinguish between process and thread n Uses term task rather than thread n clone() takes options to determine sharing on process create n struct task_struct points to process data structures (shared or unique) Operating System Concepts – 8 th Edition 4. 38 Silberschatz, Galvin and Gagne © 2009

Recap ① Provide 2 programming example in which multithreading provides better performance than a single-threaded solution. ② What are at least 2 differences between a kernel thread and a user thread? ③ What is the difference between creating and managing processes and threads (e. g. , what do they share)? ④ Which of the following components of program state are shared across threads in a multithreaded process? (Exercise 4. 10) Register values, Heap memory, Global variables, Stack memory ⑤ Consider a multiprocessor system and a multithreaded program written using the many-to- many threading model. Let the number of user-level threads in the program be more than the number of processors in the system. Discuss the performance implications of the following scenarios: (Exercise 4. 14) l Number of kernel threads allocated to the program is less than the number of cores. l Number of kernel threads allocated to the program is equal to the number of cores. l Number of kernel threads allocated to the program is greater than the number of cores but less than the number of user-level threads. Operating System Concepts – 8 th Edition 4. 39 Silberschatz, Galvin and Gagne © 2009

Recap ① Provide 2 programming example in which multithreading provides better performance than a single-threaded solution. l A Web server that services each request in a separate thread. l A parallelized application such as matrix multiplication where different parts of the matrix may be worked on in parallel. l An interactive GUI program such as a debugger where a thread is used to monitor user input, another thread represents the running application, and a third thread monitors performance. ② What are at least 2 differences between a kernel thread and a user thread? l User-level threads are unknown by the kernel, whereas the kernel is aware of kernel threads. l On systems using either M: 1 or M: N mapping, user threads are scheduled by the thread library and the kernel schedules kernel threads. l Kernel threads need not be associated with a process whereas every user thread belongs to a process. Kernel threads are generally more expensive to maintain than user threads as they must be represented with a kernel data structure. Operating System Concepts – 8 th Edition 4. 40 Silberschatz, Galvin and Gagne © 2009

Recap Operating System Concepts – 8 th Edition 4. 41 Silberschatz, Galvin and Gagne © 2009

Recap ⑤ Consider a multiprocessor system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be more than the number of processors in the system. Discuss the performance implications of the following scenarios: (Exercise 4. 10) l The number of kernel threads allocated to the program is less than the number of processors. 4 l The number of kernel threads allocated to the program is equal to the number of processors. 4 l Not all of the processors will be utilized Whenever a kernel thread is blocked, a processor will go unutilized The number of kernel threads allocated to the program is greater than the number of processors but less than the number of user-level threads. 4 Allows for the greatest utilization of processors and the kernel threads will be scheduled on the processors Operating System Concepts – 8 th Edition 4. 42 Silberschatz, Galvin and Gagne © 2009

End of Chapter 4 Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009