Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 4 Thread Dispatching January 28 2010 Ion Stoica http inst eecs berkeley edu cs162 Programs Processes Threads Thread lightweight process unit of execution A sequential execution stream of instructions No protection between threads other than CPU Process heavyweight process unit of resource allocation management Protect memory I O Why separate the concept of a thread from that of a process Discuss the thread part of a process concurrency Separate from the address space Protection Heavyweight Process Process with one thread 1 28 10 CS162 UCB Spring 2010 Lec 4 2 Multiple Processes Collaborate on a Task Proc 1 Proc 2 Proc 3 High Creation memory Overhead Relatively High Context Switch Overhead Need Communication mechanism Separate Address Spaces Isolates Processes Shared Memory Mapping Accomplished by mapping addresses to common DRAM Read and Write through memory Message Passing send and receive messages Works across network 1 28 10 CS162 UCB Spring 2010 Lec 4 3 Shared Memory Communication Code Data Heap Stack Shared Prog 1 Virtual Address Space 1 Data 2 Stack 1 Heap 1 Code 1 Stack 2 Data 1 Heap 2 Code 2 Shared Code Data Heap Stack Shared Prog 2 Virtual Address Space 2 Communication occurs by simply reading writing to shared address page Really low overhead communication Introduces complex synchronization problems 1 28 10 CS162 UCB Spring 2010 Lec 4 4 Inter process Communication IPC Mechanism for processes to communicate and to synchronize their actions Message system processes communicate with each other without resorting to shared variables IPC facility provides two operations send message message size fixed or variable receive message If P and Q wish to communicate they need to establish a communication link between them exchange messages via send receive Implementation of communication link physical e g shared memory hardware bus systcall trap logical e g logical properties 1 28 10 CS162 UCB Spring 2010 Lec 4 5 Single and Multithreaded Processes Threads encapsulate concurrency Active component Address spaces encapsulate protection Passive part Keeps buggy program from trashing the system Why have multiple threads address space CS162 UCBper Spring 2010 Lec 4 6 1 28 10 Examples of multithreaded programs Embedded systems Elevators Planes Medical systems Wristwatches Single Program concurrent operations Most modern OS kernels Internally concurrent because have to deal with concurrent requests by multiple users But no protection needed within kernel Database Servers Access to shared data by many concurrent users Also background utility processing must be done 1 28 10 CS162 UCB Spring 2010 Lec 4 7 Examples of multithreaded programs con t Network Servers Concurrent requests from network Again single program multiple concurrent operations File server Web server and airline reservation systems Parallel Programming More than one physical CPU Split program into multiple threads for parallelism This is called Multiprocessing Some multiprocessors are actually uniprogrammed Multiple threads in one address space but one program at a time 1 28 10 CS162 UCB Spring 2010 Lec 4 8 Thread State State shared by all threads in process addr space Contents of memory global variables heap I O state file system network connections etc State private to each thread Kept in TCB Thread Control Block CPU registers including program counter Execution stack what is this Execution Stack Parameters function arguments return values return PCs are kept while called procedures are executing 1 28 10 CS162 UCB Spring 2010 Lec 4 9 Execution Stack Example addrX addrY A int tmp if tmp 2 B printf tmp B C addrU addrV addrZ 1 28 10 C A 2 A 1 Stack holds function arguments return address Permits recursive execution Crucial to modern languages exit CS162 UCB Spring 2010 Lec 4 10 Execution Stack Example addrX addrY A int tmp if tmp 2 B printf tmp Stack Pointer A tmp 1 ret addrZ Stack Growth B C addrU addrV addrZ 1 28 10 C A 2 A 1 Stack holds function arguments return address Permits recursive execution Crucial to modern languages exit CS162 UCB Spring 2010 Lec 4 11 Execution Stack Example addrX addrY A tmp 1 ret addrZ A int tmp if tmp 2 B printf tmp Stack Pointer B ret addrY Stack Growth B C addrU addrV addrZ 1 28 10 C A 2 A 1 Stack holds function arguments return address Permits recursive execution Crucial to modern languages exit CS162 UCB Spring 2010 Lec 4 12 Execution Stack Example addrX addrY A tmp 1 ret addrZ A int tmp if tmp 2 B ret addrY B printf tmp B Stack Pointer C ret addrU Stack Growth C addrU addrV addrZ 1 28 10 C A 2 A 1 Stack holds function arguments return address Permits recursive execution Crucial to modern languages exit CS162 UCB Spring 2010 Lec 4 13 Execution Stack Example addrX addrY if tmp 2 addrV addrZ 1 28 10 B ret addrY B C ret addrU printf tmp B C addrU A tmp 1 ret addrZ A int tmp C A 2 A 1 Stack Pointer A tmp 2 ret addrV Stack Growth Stack holds function arguments return address Permits recursive execution Crucial to modern languages exit CS162 UCB Spring 2010 Lec 4 14 threads Per AS One Many of addr spaces Classification One Many MS DOS early Traditional UNIX Macintosh Embedded systems Mach OS 2 Linux Geoworks Win 95 Mac OS X VxWorks JavaOS etc Win NT to XP Solaris HP UX JavaOS Pilot PC Real operating systems have either One or many address spaces One or many threads per address space Did Windows 95 98 ME have real memory protection No Users could overwrite process tables System DLLs 1 28 10 CS162 UCB Spring 2010 Lec 4 15 Administriva Project Signup Project Signup Use Group Section Signup Link 4 5 members to a group Everyone in group must be able to actually attend same section Only submit once per group Everyone in group must have logged into their cs162 xx accounts once before you register the group Due Friday 1 29 by 11 59pm Anyone without a group Sections in this class are mandatory Go to the section that you have been assigned Important information will be given in section 5 of grade is participation Reader now available at Copy Central on Hearst Other things on Handouts page Interesting papers Synchronization examples Previous finals solutions 1 28 10 CS162 UCB Spring 2010 Lec 4 16 Goals for Today Further Understanding Threads Thread Dispatching Beginnings of Thread Scheduling Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and 1 28 10 CS162 UCBfrom Spring my 2010lecture notes Lec 4 17


View Full Document

Berkeley COMPSCI 162 - Lecture 4 Thread Dispatching

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Loading Unlocking...
Login

Join to view Lecture 4 Thread Dispatching and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 4 Thread Dispatching and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?