上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-thread

OPERATING SYSTEMS 1 龚玲 lgong@sjtu.edu.cn
OPERATING SYSTEMS 龚玲 lgong@sjtu.edu.cn 1

REVIEW o Why introduce thread? o What is the concept of thread? o How to implement the thread? ohttp://wenku.baidu.com/course/study/77fldcccda38376ba flfae94#665ea0c7aa00b52acfc7ca94
REVIEW Why introduce thread? What is the concept of thread? How to implement the thread? http://wenku.baidu.com/course/study/77f1dcccda38376ba f1fae94#665ea0c7aa00b52acfc7ca94

MULTITHREADING MODEL o Process:Operating system abstraction to code data files represent what is needed to run a single, registers registers registers multithreaded program o Two parts: stack stack stack Multiple Threads o Each thread is a single, sequential stream of execution 3 thread Protected Resources: o Main Memory State (contents of Address Space) oI/O state (i.e.file descriptors) multithreaded process
MULTITHREADING MODEL Process: Operating system abstraction to represent what is needed to run a single, multithreaded program Two parts: Multiple Threads Each thread is a single, sequential stream of execution Protected Resources: Main Memory State (contents of Address Space) I/O state (i.e. file descriptors)

Single-Threaded Example o Imagine the following C program: main(){ ComputepI(pi.txt"); PrintclassList("clist.text"); } o What is the behavior here? Program would never print out class list Why?ComputePI would never finish
Single-Threaded Example Imagine the following C program: main() { ComputePI(“pi.txt”); PrintClassList(“clist.text”); } What is the behavior here? Program would never print out class list Why? ComputePI would never finish

Use of Threads o Version of program with Threads: main () CreateThread(ComputepI("pi.txt")); CreateThread(PrintclassList ("clist.text")); o What does“CreateThread”do? Start independent thread running given procedure o What is the behavior here? Now,you would actually see the class list This should behave as if there are two separate CPUs CPU1 CPU2 CPU1 CPU2 CPU1 CPU2 Time
Use of Threads Version of program with Threads: main() { CreateThread(ComputePI(“pi.txt”)); CreateThread(PrintClassList(“clist.text”)); } What does “CreateThread” do? Start independent thread running given procedure What is the behavior here? Now, you would actually see the class list This should behave as if there are two separate CPUs CPU1 CPU2 CPU1 CPU2 Time CPU1 CPU2

GOALS FOR TODAY o THREADING ISSUES
GOALS FOR TODAY THREADING ISSUES

THREAD LIBRARIES o POSIX Pthreads Either a user-or kernel-level library o Win32 Kernel-level library o Java Implemented using a thread library available on the host system
THREAD LIBRARIES POSIX Pthreads Either a user- or kernel-level library Win32 Kernel-level library Java Implemented using a thread library available on the host system

JJAVA THREADS o Java threads are managed by the JVM o Java threads may be created by: Implementing the Runnable interface public interface Runnable public abstract void run();
JAVA THREADS Java threads are managed by the JVM Java threads may be created by: Implementing the Runnable interface

JAVA THREADS-EXAMPLE PROGRAM class MutableInteger { private int value; public int getValue(){ return value; public void setValue(int value){ this.value value; } class Summation implements Runnable private int upper; private MutableInteger sumValue; public Summation(int upper,MutableInteger sumValue){ this.upper upper; this.sumValue sumValue; public void run(){ int sum =0; for (int i =0;i <upper;i++) 8um+=1; sumValue.setValue(sum);
JAVA THREADS - EXAMPLE PROGRAM

JAVA THREADS-EXAMPLE PROGRAM public class Driver { public static void main(String[]args){ if (args.length 0){ if (Integer.parseInt(args [O])0."); else //create the object to be shared MutableInteger sum new MutableInteger(); int upper Integer.parseInt (args [O]); Thread thrd =new Thread(new Summation(upper,sum)); thrd.start(); try thrd.join(); System.out.println ("The sum of "+upper+"is "+sum.getValue()) } catch (InterruptedException ie){} else System.err.println("Usage:Summation "); }
JAVA THREADS - EXAMPLE PROGRAM
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-OPERATING SYSTEMS(LINUX SYSTEM CALLS).pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-semaphore EXERCISES.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-process_Lec8.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-process_Lec7.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-process_Lec6.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-process_Lec5.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-process_Lec4.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Process Synchronization_Lec14.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Process Synchronization_Lec13.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Process Synchronization_Lec12.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec24_Review.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec24_DISK MANAGEMENT AND I/O.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec23_file system implementation.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec22_file system implementation.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec21_file system interface.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec20_virtual memory.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec19_virtual memory.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec18_mainmemory.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec17_mainmemory.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资源(课件讲稿)OS-Lec16_mainmemory.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java Introduction.docx
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec1.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec2.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec3.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec4.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(JAVA PPT)lec5.ppt
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java Primer.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java多线程应用实例(制作烟花效果).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java多线程编程.pdf
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)JAVA多线程编程详解(详细操作例子).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料(Java)Java学习笔记(JAVA的面向对象编程——课堂笔记).doc
- 上海交通大学:《操作系统 Operating System》课程教学资料_管程.docx
- 上海交通大学:《操作系统 Operating System》课程教学资料_往年试卷.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture01.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture02.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture03 CONDITIONALS AND SEQUENCES Strings, lists and file objects.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture04 MODULAR PROGRAMMING Functions.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture05 ITERATION Control Structure.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture06 OBJECTS AND GRAPHICS GUI.pdf
- 上海交通大学:《Computational Thinking and Approach》教学资源(课件讲稿)Lecture07 OBJECT ORIENTED DEVELOPMENT Class and Object.pdf