Unformatted text preview:

Review Per Thread State Each Thread has a Thread Control Block TCB CS162 Operating Systems and Systems Programming Lecture 5 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 Cooperating Threads OS Keeps track of TCBs in protected memory September 15 2008 Prof John Kubiatowicz http inst eecs berkeley edu cs162 In Arrays or Linked Lists or Head Tail Link Registers Other State TCB9 Ready Queue 9 15 07 Link Registers Other State TCB16 Kubiatowicz CS162 UCB Fall 2008 Review Yielding through Internal Events Lec 5 2 Review Stack for Yielding Thread ComputePI The act of requesting I O implicitly yields the CPU Trap to OS Waiting on a signal from other thread Thread asks to wait and thus yields the CPU kernel yield run new thread Thread executes a yield Thread volunteers to give up CPU computePI while TRUE ComputeNextDigit yield Note that yield must be called by programmer frequently enough Kubiatowicz CS162 UCB Fall 2008 yield Lec 5 3 switch Stack growth Blocking on I O 9 15 07 Link Registers Other State TCB6 How do we run a new thread run new thread newThread PickNewThread switch curThread newThread ThreadHouseKeeping Later in lecture How does dispatcher switch to a new thread Save anything next thread may trash PC regs stack Maintain isolation for each thread 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Lec 5 4 Review Two Thread Yield Example Goals for Today 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 More on Interrupts Thread Creation Destruction Cooperating Threads Suppose we have 2 threads 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 Threads S and T Kubiatowicz CS162 UCB Fall 2008 Lec 5 5 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Example Network Interrupt Interrupt Controller Interrupt Control CPU Int Disable NMI Interrupts invoked with interrupt lines from devices Interrupt controller chooses interrupt request to honor Mask enables disables interrupts Priority encoder picks highest enabled interrupt Software Interrupt Set Cleared by Software Interrupt identity specified with ID line CPU can disable all interrupts with internal flag Non maskable interrupt line NMI can t be disabled 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Lec 5 7 d ts Raise priority ve In deReenable All Ints a s o ll add r1 r2 r3 PC le A r M Save registers subi r4 r1 4 o Dispatch to Handler b s slli r4 r4 2 sa r v i i D pe Transfer Network Su Packet from hardware Pipeline Flush External Interrupt Priority Encoder Interrupt Mask Timer Network IntID Software Interrupt Lec 5 6 lw lw add sw Re r2 0 r4 Us sto r3 4 r4 er re r2 r2 r3 M PC od 8 r4 r2 e to Kernel Buffers Restore registers Clear current Int Disable All Ints Restore priority RTI Interrupt Handler 9 15 07 Disable Enable All Ints Internal CPU disable bit RTI reenables interrupts returns to user mode Raise lower priority change interrupt mask Software interrupts can be provided entirely in software at priority switching boundaries 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Lec 5 8 Review Preemptive Multithreading Review Lifecycle of a Thread or Process Use the timer interrupt to force scheduling decisions TimerInterrupt run new thread switch Stack growth Some Routine Interrupt Timer Interrupt routine TimerInterrupt DoPeriodicHouseKeeping run new thread This is often called preemptive multithreading since threads are preempted for better scheduling Solves problem of user who doesn t insert yield 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Lec 5 9 As a thread executes it changes state Active threads are represented by their TCBs TCBs organized into queues based on their state 9 15 07 ThreadFork Create a New Thread We called this CreateThread earlier Arguments to ThreadFork Pointer to application routine fcnPtr Pointer to array of arguments fcnArgPtr Size of stack to allocate Sanity Check arguments Enter Kernel mode and Sanity Check arguments again Allocate new Stack and TCB Initialize TCB and place on ready list Runnable Kubiatowicz CS162 UCB Fall 2008 Lec 5 10 Go to Group Section Assignments Everyone should be up there Let Andrey cs162 tc know if there are problems Sections Section Time 102 Tu 1 00P 2 00P Implementation 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Group assignments are complete ThreadFork is a user level procedure that creates a new thread and places it on ready queue 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 Lec 5 11 Location 320 Soda 103 Tu 2 00P 3 00P 87 Evans 104 W 11 00P 12 00P 87 Evans 101 W 1 00P 2 00p 105 W 2 00P 3 00P 9 15 07 320 Soda 3 Evans Big Section Kubiatowicz CS162 UCB Fall 2008 TA Jon Whiteaker Andrey Ermolinskiy Andrey Ermolinskiy Tony Huang Jon Whiteaker Lec 5 12 Administrivia Information about Subversion on Handouts page Make sure to take a look How do we initialize TCB and Stack Initialize Register fields of TCB Stack pointer made to point at stack PC return address OS asm routine ThreadRoot Two arg registers initialized to fcnPtr and fcnArgPtr Other things on Handouts page Synchronization examples Interesting papers Previous finals solutions Initialize stack data Sections in this class are mandatory Make sure that you go to the section that you have been assigned ThreadRoot stub Start working on the first project Set up regular meeting times with your group Try figure out group interaction problems early on Stack growth Reader will be available at Copy Central on Hearst Should be reading Nachos code by now No Important part of stack frame is in registers ra Think of stack frame as just before body of ThreadRoot really gets started Initial Stack 9 15 07 Kubiatowicz CS162 UCB Fall 2008 Lec 5 13 9 15 07 ThreadRoot is the root for the thread routine Other Thread ThreadRoot DoStartupHousekeeping UserModeSwitch enter user mode Call fcnPtr fcnArgPtr ThreadFinish ThreadRoot A B while Startup Housekeeping yield run new thread New Thread switch ThreadRoot stub Includes things like recording start time of thread Other Statistics Eventually run new


View Full Document

Berkeley COMPSCI 162 - Cooperating Threads

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 Cooperating Threads 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 Cooperating Threads 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?