Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2Ku-Yaw ChangChapter 4 Multithreaded Programming A process With a single thread of control With a single thread of control With multiple threads 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 Support at the kernel level
3Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
4Ku-Yaw ChangChapter 4 Multithreaded Programming 4.1 Overview A thread A basic unit of CPU utilization A basic unit of CPU utilization A thread ID A program counter A register set A stack Called a lightweight process (LWP) Called a lightweight process (LWP) A traditional process has a single thread of control, called a heavyweight process (HWP)
5Ku-Yaw ChangChapter 4 Multithreaded Programming Single-threaded and Multithreaded processes
6Ku-Yaw ChangChapter 4 Multithreaded Programming Motivation A web browser One thread display images or text One thread display images or text Another thread retrieves data from the network Another thread retrieves data from the network A word processor One thread for displaying graphics One thread for displaying graphics Another thread for reading keystrokes Another thread for reading keystrokes Third thread for performing spelling and grammar checking Third thread for performing spelling and grammar checking
7Ku-Yaw ChangChapter 4 Multithreaded Programming 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. Process creation is very time-consuming and resource intensive. Have the server run as a single process that accepts requests. Have the server run as a single process that accepts requests.
8Ku-Yaw ChangChapter 4 Multithreaded Programming Benefits Responsiveness Allow a program to continue even if part of it is blocked or is performing a lengthy operation Allow a program to continue even if part of it is blocked or is performing a lengthy operation Resource sharing Memory Memory Different threads all within the same address space Resources ResourcesEconomy More economical to create and context switch threads More economical to create and context switch threads Utilization of multiprocessor architectures Increase concurrency Increase concurrency
9Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
10Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2 Multithreading Models User threads Implemented by a thread library at the user level Implemented by a thread library at the user level Supported above the kernel Advantage Advantage Fast to create and manage Disadvantage Disadvantage A thread may cause the entire process to block Examples Examples POSIX Pthreads Win32 threads Java threads
11Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2 Multithreading Models Kernel threads The kernel performs thread creation, scheduling, and management The kernel performs thread creation, scheduling, and management Supported directly by OS Advantage Advantage Kernel can schedule another thread when a thread is blocked Disadvantage Disadvantage Slow to create and manage Examples Examples Windows XP Linux Max OS X Solaris Tru64 Unix
12Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2 Multithreading Models Many systems provide support for both user and kernel threads Different multithreading models Different multithreading models Many-to-one model One-to-one model Many-to-many model
13Ku-Yaw ChangChapter 4 Multithreaded Programming Many-to-One Model Many user-level threads mapped to one kernel thread
14Ku-Yaw ChangChapter 4 Multithreaded Programming Many-to-One Model Advantage Efficient EfficientDisadvantage Entire process may block if a thread makes a blocking system call Entire process may block if a thread makes a blocking system call Unable to run in parallel on multiprocessors Unable to run in parallel on multiprocessors Used on systems that do not support kernel threads Green threads – a thread library available for Solaris Green threads – a thread library available for Solaris GNU Portable Threads GNU Portable Threads
15Ku-Yaw ChangChapter 4 Multithreaded Programming One-to-One Model Each user thread mapped to one kernel thread
16Ku-Yaw ChangChapter 4 Multithreaded Programming One-to-One Model Advantage More concurrency More concurrencyDisadvantage Overhead of creating kernel threads can burden the performance of an application Overhead of creating kernel threads can burden the performance of an application Restrict the number of threads Example Linux Linux Windows operating systems Windows operating systems Windows 95, 98, NT, 2000, and XP
17Ku-Yaw ChangChapter 4 Multithreaded Programming Many-to-Many Model Multiplex many user-level threads to a small or equal number of kernel threads – two-level model
18Ku-Yaw ChangChapter 4 Multithreaded Programming Many-to-Many Model Advantage Developers can create as many as user threads as wish Developers can create as many as user threads as wish More concurrency More concurrencyExample IRIX IRIX HP-UX HP-UX Tru64 Unix Tru64 Unix Solaris prior to version 9 Solaris prior to version 9
19Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
20Ku-Yaw ChangChapter 4 Multithreaded Programming 4.3 Thread Libraries A thread library Provide the programmer an API for creating and managing threads Provide the programmer an API for creating and managing threads Two primary approaches A user-level library A user-level library Entirely in user space, with no kernel support A local function call in user space A kernel-level library A kernel-level library Supported by the operating system A system call to the kernel
21Ku-Yaw ChangChapter 4 Multithreaded Programming 4.3 Thread Libraries Three main thread libraries in use POSIX Pthreads POSIX Pthreads A specification for thread behavior not an implementation not an implementation Win32 Win32 Java Java
22Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
23Ku-Yaw ChangChapter 4 Multithreaded Programming Be skipped
24Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
25Ku-Yaw ChangChapter 4 Multithreaded Programming Summary P.141 to P.142
26Ku-Yaw ChangChapter 4 Multithreaded Programming 1.Overview 2.Multithreading Models 3.Thread Libraries 4.Threading Issues 5.Operating-System Examples 6.Summary 7.Exercises
27Ku-Yaw ChangChapter 4 Multithreaded Programming Exercises
The End