当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PDF,文档页数:43,文件大小:663.27KB,团购合买
点击下载完整版文档(PDF)

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

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共43页,可试读15页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有