CSE 120 Principles of Operating Systems Lecture 2 Processes September 30 2003 Prof Joe Pasquale Department of Computer Science and Engineering University of California San Diego 2003 by Joseph Pasquale 1 Before We Begin Read Chapters 4 and 5 on Processes and Threads for background and overview read Chapters 1 3 Discussion section Wednesdays 12 1 HSS 1330 2 Introduction Most fundamental kernel function run a program Users want ability to run multiple programs at once How is this achieved given single CPU and memory 3 What is a Process Abstraction of a running program a program in execution Dynamic has state changes over time whereas a program is static Basic operations start end suspend resume send receive messages 4 Goal Support Multiple Processes browser editor email calendar Users would like multiple programs running at same time some are more important foreground background not all actively using the CPU some waiting for input devices e g disk How to do this given single CPU or small number 5 Multiprogramming Given a running process at some point it needs a resource e g I O device say resource is busy process can t proceed so voluntarily gives up CPU to another process Yield p let process p run voluntarily give up CPU to p requires context switching 6 Process Memory Structure Text code program instructions Data global variables Text Data heap dynamic allocation Stack activation records automatic growth shrinkage Stack 7 Process Stack Stack of activation records one per pending procedure Each activation record stores where to return to link to previous record automatic local variables Stack pointer points to last record RETURN instruction relies on it Local var n Local var 1 Return address Previous record Local var n Local var 1 Return address Previous record 8 Context Switching Allocating CPU to a process requires context switching first save context of currently running process next load context of next process to run Loading the context load general registers stack pointer etc load the program counter must be last instruction 9 Simple Context Switching Two processes P1 and P2 P1 calls yield to voluntarily give up CPU to P2 Save and restore registers general purpose stack pointer program counter Switch text and data not necessary if shared threads Switch stacks note that PC is in the middle of yield 10 The Magic of yield magic 0 Save P1 s context GP general purpose registers SP stack pointer Lastly PC program counter note inside yield If magic 1 return else magic 1 Restore P2 s context GP registers SP Lastly restore PC 11 In this example P1 is about to set x to 7 and yield to P2 P2 had already yielded to P1 note P2 s saved PC and SP P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X return to previous magic 1 stack stack PC SP Not shown are declarations x is a global variable in each process and magic is a local variable in yield shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 stack return to previous magic 1 why is this 1 12 P1 has just set x to 7 and is about to call yield The PC always points to the instruction to be executed next P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 13 Upon entering yield an activation record is pushed on the stack It contains links and local variable magic P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 14 Magic an automatic variable because it is dynamically allocated on the stack is set to 0 Next save context P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic 0 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 15 P1 s context is now saved The saved PC points just after the save context Compare this to P2 s saved context P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic 0 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 16 P1 just checked whether magic equals 1 which was false and so on to the else clause to set magic to 1 P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic 0 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 17 P1 sets magic to 1 and is about to restore P2 s context just like P2 s situation when it restored P1 s context P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic 1 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x 11 yield yield magic 0 save p2 context if magic 1 ret else magic 1 restore p1 context data X 11 return to previous magic 1 stack 18 P2 s context is restored i e the machine state is now that of P2 The PC points to the if statement P1 text main x 7 yield yield magic 0 save p1 context if magic 1 ret else magic 1 restore p2 context data X 7 return to previous magic 1 stack PC SP shared memory p1 context PC SP p2 context PC SP P2 text main x …
View Full Document
Unlocking...