Download presentation
Presentation is loading. Please wait.
Published byPhoebe Perry Modified over 9 years ago
1
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2
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
3
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
4
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)
5
5Ku-Yaw ChangChapter 4 Multithreaded Programming Single-threaded and Multithreaded processes
6
6Ku-Yaw ChangChapter 4 Multithreaded Programming 4.1.1 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
7
7Ku-Yaw ChangChapter 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. 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.
8
8Ku-Yaw ChangChapter 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 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
9
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
10
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
11
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
12
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
13
13Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2.1 Many-to-One Model Many user-level threads mapped to one kernel thread
14
14Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2.1 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
15
15Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2.2 One-to-One Model Each user thread mapped to one kernel thread
16
16Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2.2 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
17
17Ku-Yaw ChangChapter 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
18
18Ku-Yaw ChangChapter 4 Multithreaded Programming 4.2.3 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
19
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
20
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
21
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
22
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
23
23Ku-Yaw ChangChapter 4 Multithreaded Programming Be skipped
24
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
25
25Ku-Yaw ChangChapter 4 Multithreaded Programming Summary P.141 to P.142
26
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
27
27Ku-Yaw ChangChapter 4 Multithreaded Programming Exercises 4.34.4
28
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.