第 4 章 Threads2( 线程 2) 中国科学技术大学计算机学院 October 28, 2009
提纲 1 2 OS Examples for 3
Outline 1 2 OS Examples for 3
Windows XP Threads I An Windows XP application runs as a seperate process, and each process may contain one or more threads Implements the one-to-one mapping each user-level thread maps to an assotiated kernel thread any thread belonging to a process can access the address space of the process Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads
Windows XP Threads II The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)
Windows XP Threads III
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) clone() VS fork()
Java Threads Java 在语言级提供线程创建和管理支持功能 Java threads are managed by the JVM, not user-level library or kernel Java threads may be created by: Extending Thread class Implementing the Runnable interface Java
Thread States
Outline OS Examples for 1 2 OS Examples for 3
OS Examples for user-level thread : kernel-level thread (or LWP) Local Scheduling How the threads library decides which thread to put onto an available LWP many-to-one, many-to-many models process-contention scope, PCS Global Scheduling How the kernel decides which kernel thread to run next many-to-one, many-to-many & one-to-one models system-contention scope, SCS
Pthread Scheduling API I OS Examples for POSIX Pthread API allows specifying either PCS or SCS during thread creation PTHREAD SCOPE PROCESS, many-to-many PTHREAD SCOPE SYSTEM, one-to-one create and bind an LWP for each user-level thread example
Pthread Scheduling API II OS Examples for #include <pthreadh> #include <stdioh> #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread t tid[num THREADS]; pthread attr t attr; pthread attr init(&attr); /* get the default attributes */ /* set the scheduling algorithm to PROCESS or SYSTEM */ pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); /* set the scheduling policy - FIFO, RT, or OTHER */ pthread attr setschedpolicy(&attr, SCHED OTHER);
Pthread Scheduling API III OS Examples for for (i = 0; i < NUM THREADS; i++) /* create the threads */ pthread create(&tid[i],&attr,runner,null); } for (i = 0; i < NUM THREADS; i++) /* now join on each thread */ pthread join(tid[i], NULL); /* Each thread will begin control in this function */ void *runner(void *param) { printf( I am a thread\n ); pthread exit(0); }
Solaris scheduling I OS Examples for Solaris: priority-based thread scheduling 4 classes of scheduling, in order of priority Real time System (do not change the priority) Time sharing (default, with a multilevel feedback queue) Interactive, the same as time sharing, but higher priority Within each class there are different priorities and different scheduling algorithms
Solaris scheduling II OS Examples for
Solaris scheduling III Solaris Dispatch Table OS Examples for
Windows XP scheduling OS Examples for Dispatcher: priority-based, preemptive scheduling algorithm the dispatcher uses a-32 level priority scheme to determine the order of thread execution 0: idle thread 1 15: variable classes priorities 16 31: real-time class a queue for each priority Windows XP Priorities (policy classes, relative priority)
Outline 1 2 OS Examples for 3
小结 1 2 OS Examples for 3
作业 非华夏班 :51, 53 华夏班 :44, 47
上机作业 所有 : 写一个多线程的程序, 求素数 要求 1: 用户运行程序时在命令行输入一个素数, 然后程序创建一个独立新的线程来输出小于或等于用户所输入数的所有素数 要求 2: 采用两种 thread 来实现 pthread 或者 Windows thread 或者 Java thread 谢谢!