460 likes | 478 Views
Explore the concepts of threads in OS, covering single and multiple threads of control, models, issues, and benefits with examples.
E N D
Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 5 Threads • A process • With a single thread of control • With multiple threads of control • Multithreaded computer systems • Pthreads • Solaris 2 threads • Windows 2000 threads • Linux threads • Java threads Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.1 Overview • A thread • A basic unit of CPU utilization • A thread ID • A program counter • A register set • A stack • Called a lightweight process (LWP) • A traditional process has a single thread of control, called a heavyweight process (HWP) Chapter 5 Threads
Single- and Multithreaded processes Chapter 5 Threads
5.1.1 Motivation • A web browser • One thread display images or text • Another thread retrieves data from the network • A word processor • One thread for displaying graphics • Another thread for reading keystrokes • Third thread for performing spelling and grammar checking • Process creation very heavyweight. • If new process will perform the same tasks as the existing process, why incur all that overhead? Chapter 5 Threads
5.1.2 Benefits • Responsiveness • Allow a program to continue even if part of it is blocked or is performing a lengthy operation • Resource sharing • Memory • Different threads all within the same address space • Resources • Economy • More economical to create and context switch threads • Utilization of multiprocessor architectures • Increase concurrency Chapter 5 Threads
5.1.3 User and Kernel Threads • User threads • Implemented by a thread library at the user level • Supported above the kernel • Advantage • Fast to create and manage • Disadvantage • A thread may cause the entire process to block • Examples • POSIX Pthreads • Mach C-threads • Solaris 2 UI-threads Chapter 5 Threads
5.1.3 User and Kernel Threads • Kernel threads • The kernel performs thread creation, scheduling, and management • Supported directly by OS • Advantage • Kernel can schedule another thread when a thread is blocked • Disadvantage • Slow to create and manage • Examples • Windows NT/2000 • Solaris 2 • BeOS • Tru64 Unix Chapter 5 Threads
5.1.3 User and Kernel Threads • Java threads • Created and managed by the Java virtual machine (JVM) • Do not fall under the realm of either user or kernel threads Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.2 Multithreading Models • Many systems provide support for both user and kernel threads • Different multithreading models • Many-to-one model • One-to-one model • Many-to-many model Chapter 5 Threads
5.2.1 Many-to-One Model • Many user-level threads mapped to one kernel thread • Advantage • Efficient • Disadvantage • Entire process may block if a thread makes a blocking system call • Unable to run in parallel on multiprocessors • Used on systems that do not support kernel threads • Green threads – a thread library available for Solaris 2 Chapter 5 Threads
5.2.1 Many-to-One Model Chapter 5 Threads
5.2.2 One-to-One Model • Each user thread mapped to one kernel thread • Advantage • More concurrency • Disadvantage • Overhead of creating kernel threads can burden the performance of an application • Restrict the number of threads • Example • Windows NT/2000 and OS/2 Chapter 5 Threads
5.2.2 One-to-One Model Chapter 5 Threads
5.2.3 Many-to-Many Model • Multiplex many user-level threads to a small or equal number of kernel threads • Advantage • Developers can create as many as user threads as wish • More concurrency • Example • Solaris 2 • Windows NT/2000 with the ThreadFiber package • IRIX • HP-UX • Tru64 Unix Chapter 5 Threads
5.2.3 Many-to-Many Model Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.3 Threading Issues • The fork and exec System Calls • Cancellation • Signal Handling • Thread Pools • Thread-Specific Data Chapter 5 Threads
5.3.1 The fork and execSystem Calls • One thread in a program calls fork • New process duplicate all threads • New process is single threaded • The exec system call works in the same way as described before. • Some systems have two versions of fork • If exec is called immediately, then duplicating all threads is unnecessary. • If not, the separate process should duplicate all threads. Chapter 5 Threads
5.3.2 Cancellation • Thread cancellation • The task of terminating a thread before it has completed • Target thread • The thread that is to be cancelled • Two different scenarios • Asynchronous cancellation • One thread immediately terminates the target threads. • Deferred cancellation • The target thread periodically checks if it is should terminate, allowing itself an opportunity to terminate itself in an orderly fashion. • Cancellation points (Pthreads) Chapter 5 Threads
5.3.3 Signal Handling • A signal • Used in Unix systems • Notify a process that a particular event has occurred • May be received either synchronously or asynchronously • All signals follow the same pattern • A 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. Chapter 5 Threads
5.3.3 Signal Handling • Synchronous signal • An illegal memory access or divided by zero • Asynchronous signal • Terminating a process with specific keystrokes ( <control><C> ) • Signal may be handled by • A default signal handler • Every signal has a default signal handler run by the kernel • A user-defined signal handler • Override the default signal handler Chapter 5 Threads
5.3.3 Signal Handling • Delivering signals is more complicated in multithreaded programs • 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 Chapter 5 Threads
5.3.3 Signal Handling • Windows 2000 • Does not explicitly support signals • Emulation using Asynchronous Procedure Calls (APCs) • Allow a thread to specify a function that is to be called when the thread receives notification of a particular event Chapter 5 Threads
5.3.4 Thread Pools • Potential problems • Amount of time required to create the thread prior to servicing the request • Unlimited threads could exhaust the system resources • Thread pool • To create a number of threads at process startup and place them in a pool • Sit and wait for work • A server receives a request, a thread is awaken from the pool • Once the thread completes its work, it returns to the pool • If the pools contains no available thread, the server waits until one become free Chapter 5 Threads
5.3.5 Thread-Specific Data • Threads belonging to a process share the data of the process. • One of the benefits of multithreaded programming • Each thread also needs its own copy of certain data – called thread specific data Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.4 Pthreads • a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • A specification for thread behavior, not an implementation • Common in UNIX-based systems • Generally not supported in Windows systems Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.5 Solaris 2 Threads • Read it by yourself • P. 141 to 143 Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.6 Windows 2000 Threads • Win32 API • Primary API for the family of MS OSs • Provide one-to-one mapping model • A fiber library provides the many-to-many model • Primary data structures • ETHREAD (executive thread block) • In kernel space • KTHREAD (kernel thread block) • In kernel space • TEB (Thread environment block) • User-space data structure Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.7 Linux Threads • Read it by yourself • P. 144 to 145 Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
5.8 Java Threads • Java • Support threads at the language level • For the creation and management of threads • Threads are managed by JVM • Not by a user-level library or kernel • A Java program • Only a main method runs as a single thread in JVM Chapter 5 Threads
5.8.1 Thread Creation • Thread creation • To create a new class that derived from the Thread class • Override the run method of the Thread class • Calling the start method actually creates the new thread Chapter 5 Threads
Java Thread States Chapter 5 Threads
5.8.2 The JVM andthe Host Operation System • JVM does not indicate how Java thread are to be mapped to the underlying OS • Leaving the decision to the particular implementation of the JVM • Windows use one-to-one model Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
Summary • P.147 Chapter 5 Threads
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads Summary Exercises Chapter 5 Threads Chapter 5 Threads
Exercises • 5.1 • 5.2 • 5.3 • 5.8 Chapter 5 Threads