280 likes | 434 Views
Operating System Principles. Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Chapter 4 Multithreaded Programming. A process With a single thread of control With multiple threads of control
E N D
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 4Multithreaded Programming • A process • With a single thread of control • With multiple threads of control • Multithreaded computer systems • Pthreads • Windows 32 threads • Java thread libraries • Windows XP and Linux • Support at the kernel level Chapter 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
4.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 4 Multithreaded Programming
Single-threaded and Multithreaded processes Chapter 4 Multithreaded Programming
4.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 Chapter 4 Multithreaded Programming
4.1.1 Motivation • If new process will perform the same tasks as the existing process, why incur all that overhead? • Process creation is very time-consuming and resource intensive. • Have the server run as a single process that accepts requests. Chapter 4 Multithreaded Programming
4.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 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
4.2 Multithreading Models • 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 • Win32 threads • Java threads Chapter 4 Multithreaded Programming
4.2 Multithreading Models • 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 XP • Linux • Max OS X • Solaris • Tru64 Unix Chapter 4 Multithreaded Programming
4.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 4 Multithreaded Programming
4.2.1 Many-to-One Model • Many user-level threads mapped to one kernel thread Chapter 4 Multithreaded Programming
4.2.1 Many-to-One Model • 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 • GNU Portable Threads Chapter 4 Multithreaded Programming
4.2.2 One-to-One Model • Each user thread mapped to one kernel thread Chapter 4 Multithreaded Programming
4.2.2 One-to-One Model • Advantage • More concurrency • Disadvantage • Overhead of creating kernel threads can burden the performance of an application • Restrict the number of threads • Example • Linux • Windows operating systems • Windows 95, 98, NT, 2000, and XP Chapter 4 Multithreaded Programming
4.2.3 Many-to-Many Model • Multiplex many user-level threads to a small or equal number of kernel threads – two-level model Chapter 4 Multithreaded Programming
4.2.3 Many-to-Many Model • Advantage • Developers can create as many as user threads as wish • More concurrency • Example • IRIX • HP-UX • Tru64 Unix • Solaris prior to version 9 Chapter 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
4.3 Thread Libraries • A thread library • Provide the programmer an API for creating and managing threads • Two primary approaches • A user-level library • Entirely in user space, with no kernel support • A local function call in user space • A kernel-level library • Supported by the operating system • A system call to the kernel Chapter 4 Multithreaded Programming
4.3 Thread Libraries • Three main thread libraries in use • POSIX Pthreads • A specification for thread behavior • not an implementation • Win32 • Java Chapter 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
Be skipped Chapter 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
Summary • P.141 to P.142 Chapter 4 Multithreaded Programming
Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples Summary Exercises Chapter 4Multithreaded Programming Chapter 4 Multithreaded Programming
Exercises • 4.3 • 4.4 Chapter 4 Multithreaded Programming