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