1 / 18

CHAPTER 5: THREADS ( 线程 )

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.

papina
Download Presentation

CHAPTER 5: THREADS ( 线程 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 5: THREADS (线程) • Overview • Multithreading Models (多线程模型) • Thread Implementation Issues (线程实现事项) • Pthreads • Solaris 2 Threads • W2K Threads • Linux Threads • Java Threads

  2. OVERVIEW • Lightweight process (LWP, thread) • heavyweight process

  3. 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.

  4. 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.

  5. MULTITHREADING MODELS • Many-to-one model • One-to-one model • Many-to-many model

  6. 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.

  7. Multithreading models: one-to-one model • Each user-level thread maps to kernel thread. • Examples: Windows 95/98/NT/2000, OS/2.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. SOLARIS 2 THREADS User-level threads LWP (LightWeight Process) Kernel threads

  14. Solaris 2 threads A process in Solaris 2

  15. 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)

  16. 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?

  17. 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.

  18. Homework • 5.1 • 5.2 • 5.3 • 5.4/5.5 • 5.8/5.9

More Related