Presentation is loading. Please wait.

Presentation is loading. Please wait.

线程(Thread).

Similar presentations


Presentation on theme: "线程(Thread)."— Presentation transcript:

1 线程(Thread)

2 举例:从fork()看线程 main() { int pid = 0;
printf(“the parent is going to fork\n”); if ((pid = fork()) != 0) printf(“I am the father of %d\n”, pid); else printf(“I am the child\n”); }

3 Single-threaded vs Multi-threaded (单线程 vs 多线程)

4 线程间共享内存空间

5 线程相对于进程的优势 Responsiveness (e.g. Web应用前端)
Resource Sharing (e.g. shared variable) Economy (e.g. save memory) Utilization of MP Architectures

6 用户级线程(User Threads) 线程管理(创建、资源申请、调度、通信等)由user-level threads library“一手包办”,不靠OS内核 举例,Three primary thread libraries: Java threads POSIX pthreads Win32 threads

7 内核级(Kernel Threads) 线程管理由操作系统内核的kernel-level threads实现 举例
Windows XP/2000 Solaris

8 多线程(Multithreading)模型
Many-to-One One-to-One Many-to-Many

9 Many-to-One模型 这种模型将多个user-level threads映射至同一个kernel thread。构成一组对应关系。
操作系统运行环境里,可以存在很多组。 举例 Linux GNU Portable Threads,即GNU pth 凡是不支持线程的OS内核,都可以用这个模型

10 Many-to-One模型

11 One-to-One模型 这种模型将每个user-level thread映射至一个kernel thread 举例
Windows NT/XP/2000 Solaris 9 and later

12 One-to-one模型

13 Many-to-Many模型 这种模型将m个user-level threads映射至n个kernel threads
操作系统既可以一对一支持线程,又可以让一个kernel level thread兼顾多个用户级线程 OS内核相对较复杂 举例 Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

14 Many-to-Many模型

15 Two-level模型 与m:n的Many-to-Many模型相似,突出了n=1的情形 举例 IRIX HP-UX Tru64 UNIX
Solaris 8 and earlier

16 Two-level模型

17 线程管理相对于进程管理,带来的新问题 fork()操作和exec()操作的语义有变 撤销线程(Thread cancellation)
Signal handling Thread pools Thread specific data Scheduler activations …………

18 fork()操作和exec()操作的语义
某个线程调用了fork(),那么,这次fork()操作仅仅复制调用线程呢?还是复制(与调用线程同属于一个task的)所有线程?

19 撤销线程(Thread Cancellation)
语义:在线程正常完成操作前,终止它 至少有“撤销”语义: 异步(Asynchrous)撤销:terminates the target thread immediately 延后(Deferred)撤销:allows the target thread to periodically check if it should be cancelled

20 END


Download ppt "线程(Thread)."

Similar presentations


Ads by Google