DOC PREVIEW
Berkeley COMPSCI 162 - Lecture 5 Cooperating Threads

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS162 Operating Systems and Systems Programming Lecture 5 Cooperating Threads September 14 2005 Prof John Kubiatowicz http inst eecs berkeley edu cs162 Review 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 Head Link Link Link InTail Arrays or Linked Lists Registers or Registers Ready Queue 9 14 05 Other State TCB9 Other State TCB6 Kubiatowicz CS162 UCB Fall 2005 Registers Other State TCB16 Lec 5 2 Review 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 CPU computePI while TRUE ComputeNextDigit yield Note that yield must be called by programmer frequently enough 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 3 Review 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 Later in lecture How does dispatcher switch to a new thread 9 14 05 Save anything next thread may trash PC regs stack Maintain isolation for each thread Kubiatowicz CS162 UCB Fall 2005 Lec 5 4 Review Two Thread Yield Example 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 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 5 Goals for Today More on Interrupts Thread Creation Destruction Cooperating Threads Note Some slides and or pictures in the following are adapted from slides 2005 Silberschatz Galvin and 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 6 Gagne Interrupt Controller Priority Encoder Interrupt Mask Timer Network Software Interrupt IntID Interrupt CPU Int Disable Control 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 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 7 d ts Raise priority e n de I v a ll o Reenable All Ints s A M Save registers add r1 r2 r3 C P bl e o r subi r4 r1 4 Dispatch to Handler s i a v slli r4 r4 2 is D per Transfer Network u Packet from hardware Pipeline Flush S lw lw add sw Re r2 0 r4 U st se o r3 4 r4 r re M P r2 r2 r3 od C 8 r4 r2 e to Kernel Buffers Restore registers Clear current Int Disable All Ints Restore priority RTI Interrupt Handler External Interrupt Example 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 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 8 software at priority switching boundaries Review Preemptive Multithreading 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 This is often called preemptive multithreading since threads are prempted for better scheduling Solves problem of user who doesn t insert yield 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 9 Review 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 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 10 ThreadFork 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 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 11 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 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 started Stack growth ThreadRoot stub Initial Stack 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 12 Administrivia If you haven t generated a new key yet and given a passcode you must do this NOW We need the ssh keys to make the group accounts Sections in this class are mandatory Make sure that you go to the section that you have been assigned Should be reading Nachos code by now You should know enough to start working on the first project Set up regular meeting times with your group Let s try to get group interaction problems figured out early 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 13 How does Thread get started Other Thread Stack growth ThreadRoot A B while yield run new thread New Thread switch ThreadRoot stub Eventually run new thread will select this TCB and return into beginning of ThreadRoot 9 14 05 This reallyKubiatowicz CS162new UCB thread Fall 2005 starts the Lec 5 14 What does ThreadRoot look like ThreadRoot is the root for the thread routine Startup Housekeeping Includes things like recording start time of thread Other Statistics ThreadRoot Thread Code Stack will grow and shrink Running Stack with execution of thread Final return from thread returns into ThreadRoot which calls ThreadFinish Stack growth ThreadRoot DoStartupHousekeeping UserModeSwitch enter user mode Call fcnPtr fcnArgPtr ThreadFinish ThreadFinish will start at user level 9 14 05 Kubiatowicz CS162 UCB Fall 2005 Lec 5 15 What does ThreadFinish do Needs to re


View Full Document

Berkeley COMPSCI 162 - Lecture 5 Cooperating Threads

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Download Lecture 5 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 5 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 5 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?