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