190 likes | 389 Views
CHAPTER 5: THREADS ( 线程 ). Overview Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Pthreads Solaris 2 Threads W2K Threads Linux Threads Java Threads. OVERVIEW. Lightweight process (LWP, thread) heavyweight process. Overview: Motivation.
E N D
CHAPTER 5: THREADS (线程) • Overview • Multithreading Models (多线程模型) • Thread Implementation Issues (线程实现事项) • Pthreads • Solaris 2 Threads • W2K Threads • Linux Threads • Java Threads
OVERVIEW • Lightweight process (LWP, thread) • heavyweight process
Overview: Motivation • Examples of multithreaded program: • web browser, word processor, • database server, web server. • Benefits • Responsiveness, • Resource Sharing, • Economy(creation 30:1, context switching 5:1), • Utilization of MP Architectures.
Overview: User and Kernel Threads • User Threads • Provided by a thread library at the user level, • Fast/Blocking, • POSIX Pthreads, Mach C-threads, Solaris UI-threads. • Kernel Threads • Provided by the OS directly, • Fast/Nonblocking, • Linux Solaris2, Windows, BeOS.
MULTITHREADING MODELS • Many-to-one model • One-to-one model • Many-to-many model
Multithreading models: Many-to-one model • Many user-level threads mapped to single kernel thread. • Used on systems that do not support kernel threads. • Example: Green threads for Solaris2.
Multithreading models: one-to-one model • Each user-level thread maps to kernel thread. • Examples: Windows 95/98/NT/2000, OS/2.
Multithreading models: 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 2, IRX, HP-UX.
THREAD IMPLEMENTATION ISSUES • The fork and exec system calls • Two versions of fork • To duplicate all the threads • To duplicate the calling thread only • No change for exec. • Thread cancellation. • Asynchronous cancellation: one thread immediately terminates the target thread • Deferred cancellation: the target thread can periodically check if it should terminate, allowing the target thread an opportunity to terminate itself in an orderly fashion.
Thread implementation issues • Signal handling • Signal is generated by the occurrence of a particular event. A generated signal is delivered to a process. Once delivered, the signal must be handled. • Two types: synchronous or asynchronous signals. • Signal handling: default or user-defined signal handler. • Where should a signal be delivered in multithreaded. programs? • To the thread to which the signal applies • To every thread in the process • To certain threads in the process • Assign a specific thread to thread all signals for the process.
Thread implementation issues • How to handle a new request for a multithreaded server • To create a new thread • Time consuming to create. • No limit on the number of new threads. • Thread pool: To create a number of threads at the process startup and place them into a pool, where they sit and wait for work. • Thread specific data • Specific data, • Win32, Pthreads, Java Threads.
PTHREADS • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. • API specifies behavior of the thread library, implementation is up to development of the library. • Common in UNIX operating systems. • A multithreaded C program using the Pthread API.
SOLARIS 2 THREADS User-level threads LWP (LightWeight Process) Kernel threads
Solaris 2 threads A process in Solaris 2
WINDOW 2000 THREADS • Implements the one-to-one mapping. • Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area • Each thread includes three primary data structures • ETHREAD (executive thread block) • KTHREAD (kernel thread block) • TEB (thread environment block)
LINUX THREADS • Linux refers to them as tasks rather than threads. • Thread creation is done through clone() system call. • Clone() allows a child task to share the associated data structures of the parent task (process). • Fork() allows a child task to have a copy of all the associated data structures of the parent task. • Why the address space sharing is possible?
JAVA THREADS • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface • Java threads are managed by the JVM. • Java threads are mapped to the underlying host operating system. • An example using Java threads.
Homework • 5.1 • 5.2 • 5.3 • 5.4/5.5 • 5.8/5.9