5.1 operating system concepts module 5: threads 线程 overview 综述 benefits 益处 user and...

35
Operating System Concepts 5.1 Module 5: Threads 线线 Overview 线线 Benefits 线线 User and Kernel Threads 线线线线线线线 Multithreading Models 线线线线线 Solaris 2 Threads Solaris 2 线线 Java Threads Java 线线

Upload: prosper-burke

Post on 18-Jan-2018

332 views

Category:

Documents


0 download

DESCRIPTION

5.3 Operating System Concepts Single and Multithreaded Processes 单个和多线程进程

TRANSCRIPT

Page 1: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.1

Module 5: Threads 线程• Overview

综述• Benefits

益处• User and Kernel Threads

用户和内核线程• Multithreading Models

多线程模型

• Solaris 2 Threads

Solaris 2 线程• Java Threads

Java 线程

Page 2: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.2

• Responsiveness

响应• Resource Sharing

资源共享• Economy经济性• Utilization of MP Architectures

MP 体系结构的运用

Benefits 益处

Page 3: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.3

Single and Multithreaded Processes单个和多线程进程

Page 4: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.4

Multithreads example

• A www server

Page 5: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.5

Multithreads example

• A www server

Page 6: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.6

Multi-Threading

• Why limit ourselves to a single thread?– Think of a web server that must service a large stream of

requests– If only have one thread, can only process one request at a

time– What to do when reading a file from disk?

• Multi-threading model– Each process can have multiple threads– Each thread has a private stack

Registers are also private– All threads of a process share the code and heap

Objects to be shared across multiple threads should be allocated on the heap

Page 7: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.7

Multi-Threading (cont)

• Implementation– Each thread is described by a thread-control block (TCB)– A TCB typically contains

Thread ID Space for saving registers Pointer to thread-specific data not on stack

• Observation– Although the model is that each thread has a private stack,

threads actually share the process address space There’s no memory protection! Threads could potentially write into each other’s stack

Page 8: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.8

Process Address Space Revisited

OS

CodeGlobals

Stack

Heap

OS

CodeGlobalsStack

Heap

Stack

(a) Single-threaded address space (b) Multi-threaded address space

Page 9: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.9

User Threads用户线程• Thread Management Done by User-Level Threads Library由用户级线程库进行管理的线程• Examples 例子

- POSIX Pthreads

- Mach C-threads

- Solaris threads

Page 10: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.10

Kernel Threads内核线程• Supported by the Kernel由内核支持• Examples 例子

- Windows 95/98/NT

- Solaris

- Digital UNIX

Page 11: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.11

User vs. Kernel Threads

• Advantages of user threads– Performance: low-cost thread operations (do not

require crossing protection domains)– Flexibility: scheduling can be application specific– Portability: user thread library easy to port

• Disadvantages of user threads– If a user-level thread is blocked in the kernel, the entire

process (all threads of that process) are blocked– Cannot take advantage of multiprocessing (the kernel

assigns one process to only one processor)

Page 12: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.12

User vs. Kernel Threads

process

processor

userthreads

threadscheduling

processscheduling

kernelthreads

threadscheduling

kernel

user

processor

threads

threads

processscheduling

Page 13: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.13

User vs. Kernel Threads

• No reason why we shouldn’t have both

• Most systems now support kernel threads

• User-level threads are available as linkable libraries

kernelthreads

processor

userthreads

threadscheduling

threadscheduling

kernel

user

processscheduling

Page 14: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.14

Multithreading Models多线程模型• Many-to-One多对一• One-to-One一对一• Many-to-Many

多对多

Page 15: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.15

Many-to-One 多对一• Many User-Level Threads Mapped to Single Kernel Thread.多个用户级线程映像进单个内核线程• Used on Systems That Do Not Support Kernel Threads.

用于不支持内核线程的系统中

Page 16: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.16

Many-to-one Model 多对一模型

Page 17: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.17

One-to-One 一对一• Each User-Level Thread Maps to Kernel Thread.每个用户级线程映像进内核线程• Examples

- Windows 95/98/NT

- OS/2

Page 18: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.18

One-to-one Model 一对一模型

Page 19: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.19

Many-to-many Model 多对多模型

Page 20: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.20

Pthreads

• a POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.

• API specifies behavior of the thread library, implementation is up to development of the library.

• Common in UNIX operating systems.

Page 21: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.21

Solaris 2 Threads Solaris 2 线程

Page 22: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.22

solaris

• Unix 的发展分支• 4.XBSD 是由加州大学贝克莱分校计算机系统研究组开发的。该研究组也发布 BSD NET1 和 BSD NET2 版,它们包含了 4.XBSD 系统公众可用源代码。 SVRX 是 AT& T 的系统 V 的简称。 XPG3 是

X/Open 可移植性指南的第三次发行本的简称。 ANSI C 是 C 程序设计语言的 ANSI 标准。 POSIX.1 是 Unix 类系统界面的 IEEE 和ISD 标准。

Page 23: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.23

Solaris Process Solaris 线程

Page 24: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.24

Linux Threads

• Linux refers to them as tasks rather than threads.

• Thread creation is done through clone() system call.

• Clone() allows a child task to share the address space of the parent task (process)

Page 25: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.25

Java Threads Java 线程

• Java Threads May be Created by:

Java 线程可如下创建:– Extending Thread class 扩充线程类– Implementing the Runnable interface 实现可运行接口

Page 26: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.26

Extending the Thread Class线程类型的扩展class Worker1 extends Thread

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 27: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.27

Creating the Thread创建线程public class First

{

public static void main(String args[]) {

Worker runner = new Worker1();

runner.start();

System.out.println(“I am the main thread”);

}

}

Page 28: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.28

The Runnable Interface可运行接口public interface Runnable

{

public abstract void run();

}

Page 29: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.29

Implementing the Runnable Interface可运行接口的实现class Worker2 implements Runnable

{

public void run() {

System.out.println(“I am a Worker Thread”);

}

}

Page 30: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.30

Creating the Thread创建线程public class Second

{

public static void main(String args[]) {

Runnable runner = new Worker2();

Thread thrd = new Thread(runner);

thrd.start();

System.out.println(“I am the main thread”);

}

}

Page 31: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.31

Java Thread ManagementJava 线程的管理

• suspend() – suspends execution of the currently running thread.挂起 - 暂停当前线程的运行

• sleep() – puts the currently running thread to sleep for a specified amount of time.

睡眠 - 让当前线程入睡一段指定的时间• resume() – resumes execution of a suspended thread.

恢复 - 再执行被挂起的线程• stop() – stops execution of a thread.

停止 - 停止一个线程的执行

Page 32: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.32

Java Thread States Java 线程状态

Page 33: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.33

Producer Consumer Problem生产着消费者问题public class Server {

public Server() {MessageQueue mailBox = new MessageQueue();

Producer producerThread = new Producer(mailBox); Consumer consumerThread = new Consumer(mailBox); producerThread.start(); consumerThread.start(); } public static void main(String args[]) {

Server server = new Server(); }}

Page 34: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.34

Producer Thread生产者线程class Producer extends Thread {

public Producer(MessageQueue m) {mbox = m;}

public void run() { while (true) { // produce an item & enter it into the buffer

Date message = new Date(); mbox.send(message); } } private MessageQueue mbox;}

Page 35: 5.1 Operating System Concepts Module 5: Threads 线程 Overview 综述 Benefits 益处 User and Kernel Threads 用户和内核线程 Multithreading Models 多线程模型 Solaris 2

Operating System Concepts 5.35

Consumer Thread消费者线程class Consumer extends Thread {

public Consumer(MessageQueue m) {mbox = m;}

public void run() { while (true) {

Date message = (Date)mbox.receive();if (message != null) // consume the message}

} private MessageQueue mbox;}