提纲 Overview Multithreading Models 3 Thread Libraries Threading Issues OS Examples for Thread 6 Thread Scheduling o OS Examples for Thread Scheduling 小结 口18走卡11月00 东香兰xanchen@ustc.edu.cn http:/staff..u011740i:Operating System操作系统原理斐 March29,20193/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 提纲 1 Overview 2 Multithreading Models 3 Thread Libraries 4 Threading Issues 5 OS Examples for Thread 6 Thread Scheduling OS Examples for Thread Scheduling 7 小结 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 3 / 44
Chapter Objectives Chapter Objectives 0 To introduce the notion of a thread-a fundamental unit of CPU utiliazation that forms the basis of multithreaded computer system. To discuss the APls for Pthreads,Win32,and JAVA thread libraries. 口1回年走1,2月Q0 东香兰xlanchen@ustc,edu.cn http:/staff..u011740i:Operating System操作系统原理 March29,20194/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter Objectives Chapter Objectives 1 To introduce the notion of a thread – a fundamental unit of CPU utiliazation that forms the basis of multithreaded computer system. 2 To discuss the APIs for Pthreads, Win32, and JAVA thread libraries. 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 4 / 44
Outline Overview 口1回年走1,2月Q0 东香兰xlanchen@ustc,edu.cn http:/staff..u011740i:Operating System操作系统原理 March29,20195/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Outline 1 Overview 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 5 / 44
Thread concept overview o A thread is a basic unit of CPU utilization; it comprises a thread ID,a program counter,a register set, and a stack. It shares with other threads belonging to the same process the code section,the data section,and other OS resources, such as open files,signals,etc o A traditional process has a single thread of control: heavyweight process. code data files code data files registers stack registers registers registers stack stack stack thread single-threaded process multithreaded process 陈适兰xlanchen@ustc.edu.cn http:/staf.u01174O1:Operating System操作系统原理 March 29,2019 6/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Thread concept overview A thread is a basic unit of CPU utilization; ▶ it comprises a thread ID, a program counter, a register set, and a stack. ▶ It shares with other threads belonging to the same process the code section, the data section, and other OS resources, such as open files, signals, etc A traditional process has a single thread of control: heavyweight process. code data files registers stack single-threaded process thread code data files registers registers registers stack stack stack multithreaded process 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 6 / 44
Motivation o On modern desktop PC,many APPs are multithreaded. a seperate process with several threads Example 1:A web browser one for displaying images or text; another for retrieving data from network Example 2:A word processor one for displaying graphics; another for responding to keystrokes from the user; and a third for performing spelling grammer checking in the background Example 3:RPC servers for each message,a separate thread is used to service the message concurrency 口⊙生年12月0C 陈话兰xlanchen@ustc.edu.cn http/staff.u0117401 Operating System操作系统原理斐 March29,20197/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Motivation On modern desktop PC, many APPs are multithreaded. ▶ a seperate process with several threads ▶ Example 1: A web browser ⋆ one for displaying images or text; ⋆ another for retrieving data from network ▶ Example 2: A word processor ⋆ one for displaying graphics; ⋆ another for responding to keystrokes from the user; ⋆ and a third for performing spelling & grammer checking in the background ▶ Example 3: RPC servers ⋆ for each message, a separate thread is used to service the message ⋆ concurrency↑ 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 7 / 44
Motivation o Motivation In certain situations,a single application may be required to perform several similar tasks.Example:a web server Allow a server to service several concurrent requests.Example: an RPC server and Java's RMI systems The OS itself needs to perform some specific tasks in kernel, such as manging devices or interrupt handling. PARTICULAR,many OS systems are now multithreaded. Example:Solaris,Linux 口⊙卡生年12月00 陈适兰xlanchen@ustc.edu.cn http:/staf.u01174O1:Operating System操作系统原理 March29,20197/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Motivation Motivation ▶ In certain situations, a single application may be required to perform several similar tasks. Example: a web server ▶ Allow a server to service several concurrent requests. Example: an RPC server and Java’s RMI systems ▶ The OS itself needs to perform some specific tasks in kernel, such as manging devices or interrupt handling. ⋆ PARTICULAR, many OS systems are now multithreaded. ⋆ Example: Solaris, Linux 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 7 / 44
Benefits Example:an interactive application such as web browser, while one thread loading an image,another thread allowing user interaction Resource Sharing address space,memory,and other resources Economy Solaris: creating a process is about 30 times slower then creating a thread; context switching is about 5 times slower Utilization of MP Architectures parallelism and concurrency 口18,走卡11月00 陈话兰xlanchen@ustc.edu:cn http/staff.u0117401 Operating System操作系统原理斐 March29,20198/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Benefits 1 Responsiveness (响应度高) ▶ Example: an interactive application such as web browser, while one thread loading an image, another thread allowing user interaction 2 Resource Sharing ▶ address space, memory, and other resources 3 Economy ▶ Solaris: creating a process is about 30 times slower then creating a thread; context switching is about 5 times slower 4 Utilization of MP Architectures ▶ parallelism and concurrency ↑ 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 8 / 44
Outline 2Multithreading Models 口1⊙生年12月00 东香兰xanchen@ustc.edu.cn http:/staff..u011740i:Operating System操作系统原理斐 March29,20199/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Outline 2 Multithreading Models 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 9 / 44
Two Methods Two methods to support threads o User threads VS.Kernel threads User threads Thread management done by user-level threads library without kernel support Kernel may be multithreaded or not. Three primary thread libraries: POSIX Pthreads Win32 threads Java threads 口18,走卡11月00 东香兰xanchen@ustc.edu.cn http:/staff..u011740i:Operating System操作系统原理斐 March29,201910/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Two Methods Two methods to support threads User threads VS. Kernel threads 1 User threads ▶ Thread management done by user-level threads library without kernel support ⋆ Kernel may be multithreaded or not. ▶ Three primary thread libraries: 1 POSIX Pthreads 2 Win32 threads 3 Java threads 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 10 / 44
Two Methods Two methods to support threads o User threads VS.Kernel threads O Kernel Threads Supported by the Kernel,usually may be slower then user thread Examples Windows XP/2000 ★Solaris ★Linux Tru64 UNIX(formerly Digital UNIX) ★Mac OS X 口18走卡11月00 陈话兰xlanchen@ustc.edu:cn http/staff.u0117401 Operating System操作系统原理斐 March29,201910/44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Two Methods Two methods to support threads User threads VS. Kernel threads 2 Kernel Threads ▶ Supported by the Kernel, usually may be slower then user thread ▶ Examples ⋆ Windows XP/2000 ⋆ Solaris ⋆ Linux ⋆ Tru64 UNIX (formerly Digital UNIX) ⋆ Mac OS X 陈香兰 xlanchen@ustc.edu.cn http://staff.ustc.edu.cn/~xlanchen (Computer Application Laboratory, CS, USTC @ Hefei Embedded System Laboratory, CS, USTC @ Suzhou) 0117401: Operating System 操作系统原理与设计 March 29, 2019 10 / 44