Lecture 4: threads

Lecture 4: threads

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 Pointer A: tmp=2 ret=C+1

Stack Growth } C() { A(2); } Stack holds temporary results Permits recursive execution Crucial to modern languages A(1); 1/30/08 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 Before calling procedure: Save caller-saves regs Save v0, v1 Save ra 1/30/08 temporary (contd) reserved for OS kernel After return, assume 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 Address Space Questions: 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? 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 nGo to desired section this Location 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 havent logged in yet, you need to do so Email addresses We need an email address from you If you havent 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 Joseph CS162 UCB Spring 2008 Lec 3.14 1/30/08 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 looks as follows: Loop { RunThread(); ChooseNextThread(); SaveStateOfCPU(curTCB);

LoadStateOfCPU(newTCB); } 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? 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.16 Running a thread Consider first portion: RunThread() How do I run a thread? 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? Internal events: thread returns control voluntarily External events: thread gets preempted 1/30/08 Joseph CS162 UCB Spring 2008

Lec 3.17 Internal Events Blocking on I/O The act of requesting I/O implicitly yields the CPU Waiting on a signal from other thread Thread asks to wait and thus yields the CPU Thread executes a yield() Thread volunteers to give up CPU computePI() { while(TRUE) { ComputeNextDigit(); yield(); } } 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.18 Stack for Yielding Thread yield Trap to OS kernel_yield run_new_thread switch Stack growth

ComputePI How do we run a new thread? run_new_thread() { newThread = PickNewThread(); switch(curThread, newThread); ThreadHouseKeeping(); /* next Lecture */ } How does dispatcher switch to a new thread? Save anything next thread may trash: PC, regs, stack Maintain isolation for each thread 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.19 What do the stacks look like? proc A() { B(); } proc B() { while(TRUE) { yield(); } } Stack growth

Consider the following code blocks: Thread S Thread T A A B(while) B(while) yield yield run_new_thread run_new_thread switch switch Suppose we have 2 threads: Threads S and T 1/30/08

Joseph CS162 UCB Spring 2008 Lec 3.20 BREAK Saving/Restoring state (often called Context Switch) Switch(tCur,tNew) { /* Unload old thread */ TCB[tCur].regs.r7 = CPU.r7; TCB[tCur].regs.r0 = CPU.r0; TCB[tCur].regs.sp = CPU.sp; TCB[tCur].regs.retpc = CPU.retpc; /*return addr*/ /* Load and execute new thread */ CPU.r7 = TCB[tNew].regs.r7; CPU.r0 = TCB[tNew].regs.r0; CPU.sp = TCB[tNew].regs.sp; CPU.retpc = TCB[tNew].regs.retpc; return; /* Return to CPU.retpc */ } 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.22 Switch Details How many registers need to be saved/restored?

MIPS 4k: 32 Int(32b), 32 Float(32b) Pentium: 14 Int(32b), 8 Float(80b), 8 SSE(128b), Sparc(v7): 8 Regs(32b), 16 Int regs (32b) * 8 windows = 136 (32b)+32 Float (32b) Itanium: 128 Int (64b), 128 Float (82b), 19 Other(64b) retpc is where the return should jump to. In reality, this is implemented as a jump There is a real implementation of switch in Nachos. See switch.s Normally, switch is implemented as assembly! Of course, its magical! But you should be able to follow it! 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.23 Switch Details (continued) What if you make a mistake in implementing switch? Suppose you forget to save/restore register 4 Get intermittent failures depending on when context switch occurred and whether new thread uses register 4 System will give wrong result without warning Can you devise an exhaustive test to test switch code? No! Too many combinations and inter-leavings

Cautionary tail: For speed, Topaz kernel saved one instruction in switch() Carefully documented! Only works As long as kernel size < 1MB What happened? Time passed, People forgot Later, they added features to kernel (no one removes features!) Very weird behavior started happening Moral of story: Design for simplicity 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.24 What happens when thread blocks on I/ O? read Trap to OS kernel_read run_new_thread switch Stack growth CopyFile

What happens when a thread requests a block of data from the file system? User code invokes a system call Read operation is initiated Run new thread/switch Thread communication similar Wait for Signal/Join Networking 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.25 External Events What happens if thread never does any I/O, never waits, and never yields control? Could the ComputePI program grab all resources and never release the processor? What if it didnt print to console? Must find way that dispatcher can regain control! Answer: Utilize External Events Interrupts: signals from hardware or software that stop the running code and jump to kernel Timer: like an alarm clock that goes off every some many milliseconds If we make sure that external events occur

frequently enough, can ensure dispatcher runs 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.26 to Kernel Buffers lw lw add sw $r2,0($r4) $r3,4($r4) $r2,$r2,$r3 8($r4),$r2 Restore registers Clear current Int Disable All Ints Restore priority RTI Interrupt Handler Raise priority s

d nt Reenable e All Ints e d I v a ll Mo Save registers s A r add $r1,$r2,$r3 C e Dispatch to Handler subi $r4,$r1,#4 P bl iso a v slli $r4,$r4,#2 is r Transfer Network D pe u Packet from hardware S Pipeline Flush PC e re d to Mo es r R se

U External Interrupt Example: Network Interrupt An interrupt is a hardware-invoked context switch No separate step to choose what to run next 1/30/08 Josephinterrupt CS162 UCB handler Spring 2008 immediately Lec 3.27 Always run the Use of Timer Interrupt to Return Control Solution to our dispatcher problem Some Routine Interrupt TimerInterrupt run_new_thread switch Stack growth Use the timer interrupt to force scheduling decisions Timer Interrupt routine: TimerInterrupt() { DoPeriodicHouseKeeping();

run_new_thread(); } I/O interrupt: same as timer interrupt except that DoHousekeeping() replaced by ServiceIO(). 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.28 Choosing a Thread to Run How does Dispatcher decide what to run? Zero ready threads dispatcher loops Alternative is to create an idle thread Can put machine into low-power mode Exactly one ready thread easy More than one ready thread: use scheduling priorities Possible priorities: LIFO (last in, first out): put ready threads on front of list, remove from front Pick one at random FIFO (first in, first out): Put ready threads on back of list, pull them from front This is fair and is what Nachos does Priority queue: keep ready list sorted by TCB priority field 1/30/08

Joseph CS162 UCB Spring 2008 Lec 3.29 Summary The state of a thread is contained in the TCB Registers, PC, stack pointer States: New, Ready, Running, Waiting, or Terminated Multithreading provides simple illusion of multiple CPUs Switch registers and stack to dispatch new thread Provide mechanism to ensure dispatcher regains control Switch routine Can be very expensive if many registers Must be very carefully constructed! Many scheduling options Decision of which thread to run complex enough for complete lecture 1/30/08 Joseph CS162 UCB Spring 2008 Lec 3.30

Recently Viewed Presentations

  • Botany Part II Plant Structure and Growth

    Botany Part II Plant Structure and Growth

    Apical Meristems. Apical meristems are found at the tips of roots and at the buds of shoots. These are for PRIMARY growth (LENGTH). They give rise to the primary plant body. They allow roots to extend through the soil and...
  • Transitioning to California Common Core State Standards

    Transitioning to California Common Core State Standards

    Hand out pacing guide at this point with teachers having had the background of its development first. Point out features provided by the arrows. TO DO: Swap out the pacing guide on this screen with your own grade level. Move...
  • Writing levels checklist Level 1 o I have

    Writing levels checklist Level 1 o I have

    My pronunciation is accurate and my intonation follows French rhythms. Level 6. I have used knowledge from past work to help me to extend what I am saying. I have used a variety of verbs in my speaking. I have...
  • Introduction to Probability and Statistics Eleventh Edition

    Introduction to Probability and Statistics Eleventh Edition

    INTRODUCTION TO PROBABILITY AND STATISTICS ... 1535.5 hours, etc. HOW MANY VARIABLES HAVE YOU MEASURED? Univariate data: One variable is measured on a single experimental unit. ... Outliers will be defined formally in Ch. 2 Outlier No Outliers EXAMPLE A...
  • Alzheimers Disease -> The Disease of Darkness Varun

    Alzheimers Disease -> The Disease of Darkness Varun

    efficacy and safety in maintaining cognitive function, as measured by ADAS-cog in patients with mild-to-moderate AD for up to 1 year - relative to placebo!! Donepezil. 1. 38 weeks. Rivastigmine. 2. 38-42 weeks. Galantamine. 3. 52 weeks (25-30% better) Presumably...
  • System Implementation and Evaluation Start Next Sanchai Yeewiyom

    System Implementation and Evaluation Start Next Sanchai Yeewiyom

    การเปลี่ยนระบบแบบนำร่อง (Pilot Operation or Single Location Installation) ข้อเสีย ใช้เวลาในการปรับเปลี่ยนนาน เพิ่มงานด้านการช่วยเหลือสนับสนุน ทั้งในงาน ...
  • PowerPoint-Präsentation

    PowerPoint-Präsentation

    LEED - less simple Henzler, Göpel Abb. 3.8.4, p.167 Facets and mosaic Henzler, Göpel, fig. 3.8.3, p.165 Van Hove et al., fig. 3.6, p.58 Regular atomic steps T=1000K Pt(9,11,11) 0.2 ML 1 ML 7 MLE* Example: Si(001)vic [-110] [110] Si(001)...
  • Physical Quantities and Units - Faculty Pages

    Physical Quantities and Units - Faculty Pages

    Today's Agenda Primary and Secondary Physical Quantities and Units Unit Conversion Standard We Will Use We will follow the International System of Units - also known as the SI standard It includes: Distance measured in meters (m) Mass measured in...