Unformatted text preview:

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 10 2008 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 10 08 Recall Single and Multithreaded Processes Kubiatowicz CS162 UCB Fall 2008 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 10 08 Keeps buggy program from trashing the system Passive component of a process Kubiatowicz CS162 UCB Fall 2008 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 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 4 threads Per AS of addr spaces Classification Recall Execution Stack Example One A tmp 1 ret exit A int tmp Many if tmp 2 B ret A 2 B 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 C ret B 1 printf tmp B C Stack Pointer Stack Growth Real operating systems have either C One or many address spaces One or many threads per address space A 2 Did Windows 95 98 ME have real memory protection A tmp 2 ret C 1 A 1 Stack holds temporary results Permits recursive execution Crucial to modern languages No Users could overwrite process tables System DLLs 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 5 9 10 08 MIPS Software conventions for Registers zero constant 0 16 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 15 callee saves Imagine the following C program temporary cont d reserved for OS kernel main ComputePI pi txt PrintClassList clist text What is the behavior here temporary caller saves 29 sp Stack pointer callee can clobber 30 fp frame pointer 31 ra Return Address HW t7 Lec 4 6 Single Threaded Example 0 s0 Kubiatowicz CS162 UCB Fall 2008 Program would never print out class list Why ComputePI would never finish Before calling procedure After return assume Save caller saves regs Save v0 v1 Save ra 9 10 08 Callee saves reg OK gp sp fp OK restored Other things trashed Kubiatowicz CS162 UCB Fall 2008 Lec 4 7 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 8 Use of Threads Memory Footprint of Two Thread Example Version of program with Threads main CreateThread ComputePI pi txt CreateThread PrintClassList clist text 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 CPU1 Two sets of CPU registers Two sets of Stacks Stack 1 Questions 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 Address Space What does CreateThread do If we stopped this program and examined it with a debugger we would see Stack 2 Heap Global Data CPU2 Code Time 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 9 9 10 08 Per Thread State Kubiatowicz CS162 UCB Fall 2008 Lec 4 10 Lifecycle of a Thread or Process 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 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 TCBs organized into queues based on their state 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 11 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 12 Administriva Last Try for Project Signup 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 Ready Queue Head Tail Tape Unit 0 Head Tail Disk Unit 0 Head Tail Disk Unit 2 Head Tail Ether Netwk 0 9 10 08 Head Tail Link Registers Other State TCB9 Link Registers Other State TCB6 Link Registers Other State TCB2 Link Registers Other State TCB16 Link Registers Other State TCB3 Link Registers Other State TCB8 Kubiatowicz CS162 UCB Fall 2008 Lec 4 13 Still working on section assignments Posted final sections at 2 00 yesterday If you signed up before then you need to do it again Project Signup Group Section Assignment Link Due date Tonight 9 10 by 11 59pm 103 Tu 2 00P 3 00P 81 Evans 104 W 11 00P 12 00P 81 Evans 101 105 W 1 00P 2 00p W 2 00P 3 00P 9 10 08 Dispatch Loop Andrey Ermolinskiy Andrey Ermolinskiy Tony Huang Jon Whiteaker Kubiatowicz CS162 UCB Fall 2008 Lec 4 14 Consider first portion RunThread How do I run a thread Loop RunThread ChooseNextThread SaveStateOfCPU curTCB LoadStateOfCPU newTCB Load its state registers PC stack pointer into CPU Load environment virtual memory space etc Jump to the PC How does the dispatcher get control back This is an infinite loop One could argue that this is all that the OS does Should we ever exit this loop When would that be Kubiatowicz CS162 UCB Fall 2008 TBA 3 Evans TA Jon Whiteaker Running a thread Conceptually the dispatching loop of the operating system looks as follows 9 10 08 Location 320 Soda Section Time 102 Tu 1 00P 2 00P Internal events thread returns control voluntarily External events thread gets preempted Lec 4 15 9 10 08 Kubiatowicz CS162 UCB Fall 2008 Lec 4 16 Internal Events Stack for Yielding Thread ComputePI The act of


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?