Download presentation
Presentation is loading. Please wait.
Published byJosephine Hutchinson Modified over 9 years ago
1
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation Issues (线程实现事项) Pthreads Pthreads Solaris 2 Threads Solaris 2 Threads W2K Threads W2K Threads Linux Threads Linux Threads Java Threads Java Threads
2
OVERVIEW Lightweight process (LWP, thread) Lightweight process (LWP, thread) heavyweight process heavyweight process
3
Overview: Motivation Examples of multithreaded program: Examples of multithreaded program: web browser, word processor, database server, web server. Benefits Benefits Responsiveness, Resource Sharing, Economy(creation 30:1, context switching 5:1), Utilization of MP Architectures.
4
Overview: User and Kernel Threads User Threads User Threads Provided by a thread library at the user level, Fast/Blocking, POSIX Pthreads, Mach C-threads, Solaris UI- threads. Kernel Threads Kernel Threads Provided by the OS directly, Fast/Nonblocking, Linux Solaris2, Windows, BeOS.
5
MULTITHREADING MODELS Many-to-one model Many-to-one model One-to-one model One-to-one model Many-to-many model Many-to-many model
6
Multithreading models: Many-to-one model Many user-level threads mapped to single kernel thread. Many user-level threads mapped to single kernel thread. Used on systems that do not support kernel threads. Used on systems that do not support kernel threads. Example: Green threads for Solaris2. Example: Green threads for Solaris2.
7
Multithreading models: one-to-one model Each user-level thread maps to kernel thread. Each user-level thread maps to kernel thread. Examples: Windows 95/98/NT/2000, OS/2. 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 many user level threads to be mapped to many kernel threads. Allows the operating system to create a sufficient number of kernel threads. Allows the operating system to create a sufficient number of kernel threads. Examples: Solaris 2, IRX, HP-UX. Examples: Solaris 2, IRX, HP-UX.
9
THREAD IMPLEMENTATION ISSUES The fork and exec system calls 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. 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 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 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 Thread specific data Specific data, Win32, Pthreads, Java Threads.
12
PTHREADS A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization. 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. API specifies behavior of the thread library, implementation is up to development of the library. Common in UNIX operating systems. Common in UNIX operating systems. A multithreaded C program using the Pthread API. 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. Implements the one-to-one mapping. Each thread contains Each thread contains - a thread id - register set - separate user and kernel stacks - private data storage area Each thread includes three primary data structures 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. Linux refers to them as tasks rather than threads. Thread creation is done through clone() system call. Thread creation is done through clone() system call. Clone() allows a child task to share the associated data structures of the parent task (process). 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. 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? Why the address space sharing is possible?
17
JAVA THREADS Java threads may be created by: Java threads may be created by: Extending Thread class Implementing the Runnable interface Java threads are managed by the JVM. Java threads are managed by the JVM. Java threads are mapped to the underlying host operating system. Java threads are mapped to the underlying host operating system. An example using Java threads. An example using Java threads.
18
Homework 5.1 5.1 5.2 5.2 5.3 5.3 5.4/5.5 5.4/5.5 5.8/5.9 5.8/5.9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.