Recall Modern Process with Multiple Threads Process Operating system abstraction to represent CS162 Operating Systems and Systems Programming Lecture 4 what is needed to run a single multithreaded program Two parts Multiple Threads Each thread is a single sequential stream of execution Protected Resources Thread Dispatching Main Memory State contents of Address Space I O state i e file descriptors Why separate the concept of a thread from that of a process September 9 2009 Prof John Kubiatowicz http inst eecs berkeley edu cs162 Discuss the thread part of a process concurrency Separate from the address space Protection Heavyweight Process Process with one thread 9 9 09 Recall Single and Multithreaded Processes Kubiatowicz CS162 UCB Fall 2009 Lec 4 2 Goals for Today Further Understanding Threads Thread Dispatching Beginnings of Thread Scheduling Threads encapsulate concurrency Active component of a process Address spaces encapsulate protection 9 9 09 Keeps buggy program from trashing the system Passive component of a process Kubiatowicz CS162 UCB Fall 2009 Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and Gagne Gagne Many slides generated from my lecture notes by Kubiatowicz Lec 4 3 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 4 of addr spaces Classification Thread State State shared by all threads in process addr space One Many One MS DOS early Macintosh Traditional UNIX Many Embedded systems Geoworks VxWorks JavaOS etc JavaOS Pilot PC Mach OS 2 Linux Win 95 Mac OS X Win NT to XP Solaris HP UX threads Per AS Contents of memory global variables heap I O state file system network connections etc State private to each thread 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 Kept in TCB Thread Control Block CPU registers including program counter Execution stack what is this Execution Stack Parameters Temporary variables return PCs are kept while called procedures are executing No Users could overwrite process tables System DLLs 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 5 9 9 09 Kubiatowicz CS162 UCB Fall 2009 MIPS Software conventions for Registers Execution Stack Example 0 A tmp 1 ret exit A int tmp if tmp 2 B ret A 2 B C ret B 1 printf tmp B C A tmp 2 ret C 1 Stack Growth zero constant 0 16 A 2 A 1 Stack holds temporary results Permits recursive execution Crucial to modern languages Kubiatowicz CS162 UCB Fall 2009 Lec 4 7 s0 callee saves 1 at reserved for assembler callee must save 2 v0 expression evaluation 23 s7 3 v1 function results 24 t8 4 a0 arguments 25 t9 5 a1 26 k0 6 a2 27 k1 7 a3 28 gp Pointer to global area 8 t0 C 9 9 09 Stack Pointer Lec 4 6 15 temporary cont d reserved for OS kernel temporary caller saves 29 sp Stack pointer callee can clobber 30 fp frame pointer 31 ra Return Address HW t7 Before calling procedure After return assume Save caller saves regs Save v0 v1 Save ra 9 9 09 Callee saves reg OK gp sp fp OK restored Other things trashed Kubiatowicz CS162 UCB Fall 2009 Lec 4 8 Single Threaded Example Use of Threads Version of program with Threads Imagine the following C program main CreateThread ComputePI pi txt CreateThread PrintClassList clist text main ComputePI pi txt PrintClassList clist text What does CreateThread do Start independent thread running given procedure What is the behavior here What is the behavior here Program would never print out class list Why ComputePI would never finish 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 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 9 9 9 09 Memory Footprint of Two Thread Example Stack 1 Questions Address Space How do we position stacks relative to each other What maximum size should we choose for the stacks What happens if threads violate this How might you catch violations Lec 4 10 Per Thread State If we stopped this program and examined it with a debugger we would see Two sets of CPU registers Two sets of Stacks Kubiatowicz CS162 UCB Fall 2009 Stack 2 Heap Global Data Each Thread has a Thread Control Block TCB Execution State CPU registers program counter pointer to stack Scheduling info State more later priority CPU time Accounting Info Various Pointers for implementing scheduling queues Pointer to enclosing process PCB Etc add stuff as you find a need In Nachos Thread is a class that includes the TCB OS Keeps track of TCBs in protected memory In Array or Linked List or Code 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 11 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 12 Lifecycle of a Thread or Process Ready Queue And Various I O Device Queues Thread not running TCB is in some scheduler queue Separate queue for each device signal condition Each queue can have a different scheduler policy As a thread executes it changes state new The thread is being created ready The thread is waiting to run running Instructions are being executed waiting Thread waiting for some event to occur terminated The thread has finished execution Active threads are represented by their TCBs Ready Queue Head Tail Tape Unit 0 Head Tail Disk Unit 0 Head Tail Disk Unit 2 Head Tail Ether Netwk 0 Head Tail TCBs organized into queues based on their state 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Lec 4 13 Administrivia Tentative Group assignments now posted on website Check out the Group Section Assignment link Please attend your newly assigned section As you can see we are a bit unbalanced in sections Many of you didn t listen you only listed one section without sending me email to explain Those of you who only selected one section must send me a 9 9 09 Kubiatowicz CS162 UCB Fall 2009 Link Registers Other State TCB2 Link Registers Other State TCB8 Kubiatowicz CS162 UCB Fall 2009 Link Registers Other State TCB16 Link Registers Other State TCB3 Lec 4 14 Make sure to take a look We understand that there have been problems with the Subversion server Hopefully will be already fixed Other things on Handouts page Interesting papers Synchronization examples Previous finals solutions I expect 15 of these messages Make sure to include your 9 9 09 Link Registers Other State TCB6 Administrivia 2 Information about Subversion on Handouts page NEW email explaining why you can only make one section group number and list of members Anyone without a group Please come up to talk with me afterwards Sections in this class are
View Full Document
Unlocking...