Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 3 Thread Dispatching January 30 2008 Prof Anthony D Joseph http inst eecs berkeley edu cs162 Recall Modern Process with Multiple Threads 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 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 30 08 Joseph CS162 UCB Spring 2008 Lec 3 2 Recall Single and Multithreaded Processes Threads encapsulate concurrency Active component of a process Address spaces encapsulate protection Keeps buggy program from trashing the system Passive component of a process 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 3 threads Per AS One Many of addr spaces Recall 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 30 08 Joseph CS162 UCB Spring 2008 Lec 3 4 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 30 08 Josephgenerated CS162 UCB Spring Lec 3 5 Gagne Many slides Gagne from2008 my lecture notes Recall Execution Stack Example A tmp 1 ret exit A int tmp if tmp 2 B ret A 2 B C ret B 1 printf tmp B C Stack Growth C A 2 A 1 1 30 08 Stack Pointer A tmp 2 ret C 1 Stack holds temporary results Permits recursive execution Crucial to modern languages Joseph CS162 UCB Spring 2008 Lec 3 6 0 MIPS Software conventions for Registers zero constant 0 16 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 temporary caller saves 29 sp Stack pointer callee can clobber 30 fp frame pointer 31 ra Return Address HW 15 t7 temporary cont d reserved for OS kernel Before calling procedure After return assume Save caller saves regs Save v0 v1 Save ra 1 30 08 Callee saves reg OK gp sp fp OK restored Other things trashed Joseph CS162 UCB Spring 2008 Lec 3 7 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 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 8 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 CPU1 CPU2 Time 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 9 Memory Footprint of Two Thread Example If we stopped this program and examined it with a debugger we would see Two sets of CPU registers Two sets of Stacks Stack 1 How do we position stacks relative toStack 2 each other What maximum size should we choose for the stacks What happens if threads violate this Heap How might you catch violations Address Space Questions Global Data Code 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 10 Administriva Time for Project Signup Project Signup Watch Group Section Assignment Link 4 5 members to a group Everyone in group must be able to actually attend same section The sections assigned to you by Telebears are temporary Only submit once per group Everyone in group must have logged into their cs162xx accounts once before you register the group Make sure that you select at least 2 potential sections Due date Thursday 1 31 by 11 59pm Sectio Sections Time Location nGo to desired section this week Thurs Fri TA 101 Th 10 00 11 00A 45 Evans Barret 102 Th 11 00 12 00P 85 Evans Barret 103 Th 4 00 5 00P 320 Soda Man Kit 104 F 2 00 3 00P 310 Soda Manu 105 1 30 08 F 3 00 4 00p 405 Soda Joseph CS162 UCB Spring 2008 Manu Lec 3 11 Administrivia 2 Cs162 xx accounts Make sure you got an account form If you haven t logged in yet you need to do so Email addresses We need an email address from you If you haven t given us one already you should get prompted when you log in again or type register Nachos reader Required TBA Copy Central at corner of Hearst Euclid Includes lectures and printouts of all of the code Start Project 1 Go to Nachos page and start reading up Note that all the Nachos code is printed in your reader 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 12 Per Thread State 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 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 13 Lifecycle of a Thread or Process 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 1 30 08 Joseph CS162 UCB Spring 2008 Lec 3 14 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 Head Netwk 0 Tail 1 30 08 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 Joseph CS162 UCB Spring 2008 Lec 3 15 Dispatch Loop Conceptually the dispatching loop of the operating system


View Full Document

Berkeley COMPSCI 162 - Lecture 3 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 3 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 3 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?