* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Figure 5.01
Library (computing) wikipedia , lookup
Berkeley Software Distribution wikipedia , lookup
Plan 9 from Bell Labs wikipedia , lookup
Mobile operating system wikipedia , lookup
Copland (operating system) wikipedia , lookup
Unix security wikipedia , lookup
Spring (operating system) wikipedia , lookup
Distributed operating system wikipedia , lookup
Process management (computing) wikipedia , lookup
Chapter 4 Multithreaded Programming Objectives To introduce a notion of a thread – a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for thread libraries (skip) To examine issues related to multithreaded programming Operating System Principles 4.1 Silberschatz, Galvin and Gagne ©2005 Chapter 4: Multithreaded Programming Overview Multithreading Models Thread Library (skip) Threading Issues Linux Threads (skip) Operating System Principles 4.2 Silberschatz, Galvin and Gagne ©2005 4.1 Overview Single and Multithreaded Processes Operating System Principles 4.3 Silberschatz, Galvin and Gagne ©2005 Multithreaded Server Architecture Operating System Principles 4.4 Silberschatz, Galvin and Gagne ©2005 Benefits Responsiveness: Allow an interactive program to continue running even if part of it is blocked or is performing a lengthy operation Resource Sharing Threads share the memory and the resources of the process Economy It is more economical to create and context-switch threads. For example, in Solaris, creating a process is 30 times slower than creating a thread, and context switching a process is 5 times slower than context switching a thread Scalability Utilization of Multiprocessor Architectures Threads may be running in parallel on different processors Operating System Principles 4.5 Silberschatz, Galvin and Gagne ©2005 Multicore Programming Multicore systems putting pressure on programmers, challenges include Dividing activities Balance Data splitting Data dependency Testing and debugging Operating System Principles 4.6 Silberschatz, Galvin and Gagne ©2005 4.2 Multithreading Models User Threads Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads, Win32 threads, Java threads Kernel threads Supported by the kernel (operating system) Examples: Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X How to establish the relationship between user and kernel threads Many-to-One One-to-One Many-to-Many Operating System Principles 4.7 Silberschatz, Galvin and Gagne ©2005 Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads, GNU Portable Threads Thread management is done in user space, so it is efficient. If a thread makes a blocking system call, then the entire process will block Operating System Principles 4.8 Silberschatz, Galvin and Gagne ©2005 One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000, Linux, Solaris 9 and later Drawback: creating a user thread requires creating the corresponding kernel thread Restrict the number of threads supported by the system Operating System Principles 4.9 Silberschatz, Galvin and Gagne ©2005 Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Examples: Solaris prior to version 9, Windows NT/2000 with the ThreadFiber package Operating System Principles 4.10 Silberschatz, Galvin and Gagne ©2005 Two-level Model Similar to Many-to-Many, except that it allows a user thread to be bound to kernel thread Examples IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier Skip 4.3 Operating System Principles 4.11 Silberschatz, Galvin and Gagne ©2005 4.4 Threading Issues (1) Semantics of fork( ) and exec( ) system calls Does fork( ) duplicate only the calling thread or all threads? Thread cancellation of target thread Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled Operating System Principles 4.12 Silberschatz, Galvin and Gagne ©2005 Threading Issues (2) Signal Handling Signals are used in UNIX systems to notify a process that a particular event has occurred 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 Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process Operating System Principles 4.13 Silberschatz, Galvin and Gagne ©2005 Threading Issues (3) Thread Pools Create a number of threads in a pool where they await work Advantages: 1. Usually slightly faster to service a request with an existing thread than create a new thread 2. Allows the number of threads in the application(s) to be bound to the size of the pool Skip p.169 這 2 點以下的部分 Operating System Principles 4.14 Silberschatz, Galvin and Gagne ©2005 Threading Issues (4) Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Scheduler Activations Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library Handled by an upcall handler running on a virtual processor This communication allows an application to maintain the correct number kernel threads Skip 4.5, 4.6 Operating System Principles 4.15 Silberschatz, Galvin and Gagne ©2005 Light Weight Process Operating System Principles 4.16 Silberschatz, Galvin and Gagne ©2005