DOC PREVIEW
Berkeley COMPSCI 162 - Lecture 4 Cooperating Threads

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 4 Cooperating ThreadsReview: Per Thread StateReview: Yielding through Internal EventsReview: Stack for Yielding ThreadReview: Two Thread Yield ExampleGoals for TodayInterrupt ControllerExample: Network InterruptReview: Preemptive MultithreadingReview: Lifecycle of a Thread (or Process)ThreadFork(): Create a New ThreadHow do we initialize TCB and Stack?AdministriviaHow does Thread get started?What does ThreadRoot() look like?What does ThreadFinish() do?Additional DetailParent-Child relationshipThreadJoin() system callUse of Join for Traditional Procedure CallBREAKKernel versus User-Mode threadsThreading models mentioned by bookMultiprocessing vs MultiprogrammingCorrectness for systems with concurrent threadsInteractions Complicate DebuggingWhy allow cooperating threads?High-level Example: Web ServerThreaded Web ServerThread PoolsSummaryCS162Operating Systems andSystems ProgrammingLecture 4Cooperating ThreadsFebruary 4, 2008Prof. Anthony D. Josephhttp://inst.eecs.berkeley.edu/~cs162Lec 4.22/4/08 Joseph CS162 ©UCB Spring 2008Review: 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)•OS Keeps track of TCBs in protected memory–In Arrays, or Linked Lists, or …OtherStateTCB9LinkRegistersOtherStateTCB6LinkRegistersOtherStateTCB16LinkRegistersHeadTailReadyQueueLec 4.32/4/08 Joseph CS162 ©UCB Spring 2008Review: Yielding through 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 CPUcomputePI() { while(TRUE) { ComputeNextDigit(); yield(); } }–Note that yield() must be called by programmer frequently enough!Lec 4.42/4/08 Joseph CS162 ©UCB Spring 2008Review: Stack for Yielding Thread•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 threadyieldComputePIStack growthrun_new_threadkernel_yieldTrap to OSswitchLec 4.52/4/08 Joseph CS162 ©UCB Spring 2008Review: Two Thread Yield Example•Consider the following code blocks: proc A() { B();}proc B() { while(TRUE) { yield(); }}•Suppose we have 2 threads:–Threads S and TThread SStack growthAB(while)yieldrun_new_threadswitchThread TAB(while)yieldrun_new_threadswitchLec 4.62/4/08 Joseph CS162 ©UCB Spring 2008Goals for Today•More on Interrupts•Thread Creation/Destruction•Cooperating ThreadsNote: Some slides and/or pictures in the following areadapted from slides ©2005 Silberschatz, Galvin, and Gagne Note: Some slides and/or pictures in the following areadapted from slides ©2005 Silberschatz, Galvin, and Gagne. Many slides generated from my lecture notes by Kubiatowicz.Lec 4.72/4/08 Joseph CS162 ©UCB Spring 2008Interrupt Controller•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 disabledNetworkIntIDInterruptInterrupt MaskControlSoftwareInterruptNMICPUPriority EncoderTimerInt DisableLec 4.82/4/08 Joseph CS162 ©UCB Spring 2008Example: Network Interrupt•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add $r1,$r2,$r3subi $r4,$r1,#4slli $r4,$r4,#2PC savedDisable All IntsSupervisor ModeRestore PCUser ModeRaise priorityReenable All IntsSave registersDispatch to HandlerTransfer Network Packet from hardwareto Kernel BuffersRestore registersClear current IntDisable All IntsRestore priorityRTI“Interrupt Handler”lw $r2,0($r4)lw $r3,4($r4)add $r2,$r2,$r3sw 8($r4),$r2External InterruptPipeline FlushLec 4.92/4/08 Joseph CS162 ©UCB Spring 2008Review: Preemptive Multithreading•Use the timer interrupt to force scheduling decisions•Timer Interrupt routine:TimerInterrupt() { DoPeriodicHouseKeeping(); run_new_thread();}•This is often called preemptive multithreading, since threads are prempted for better scheduling–Solves problem of user who doesn’t insert yield();Some Routinerun_new_threadTimerInterruptInterruptswitchStack growthLec 4.102/4/08 Joseph CS162 ©UCB Spring 2008Review: 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 stateLec 4.112/4/08 Joseph CS162 ©UCB Spring 2008ThreadFork(): Create a New Thread•ThreadFork() is a user-level procedure that creates a new thread and places it on ready queue–We called this CreateThread() earlier•Arguments to ThreadFork()–Pointer to application routine (fcnPtr)–Pointer to array of arguments (fcnArgPtr)–Size of stack to allocate•Implementation–Sanity Check arguments–Enter Kernel-mode and Sanity Check arguments again–Allocate new Stack and TCB–Initialize TCB and place on ready list (Runnable).Lec 4.122/4/08 Joseph CS162 ©UCB Spring 2008How 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•Initialize stack data?–No. Important part of stack frame is in registers (ra)–Think of stack frame as just before body of ThreadRoot() really gets startedThreadRoot stubInitial StackStack growthLec 4.132/4/08 Joseph CS162 ©UCB Spring


View Full Document

Berkeley COMPSCI 162 - Lecture 4 Cooperating Threads

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Download Lecture 4 Cooperating Threads
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 4 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 Lecture 4 Cooperating Threads 2 2 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?