Unformatted text preview:

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


View Full Document

UCSD CSE 120 - Processes

Documents in this Course
Threads

Threads

14 pages

Deadlocks

Deadlocks

19 pages

Processes

Processes

14 pages

Paging

Paging

13 pages

Processes

Processes

18 pages

Threads

Threads

29 pages

Security

Security

16 pages

Paging

Paging

13 pages

Processes

Processes

32 pages

Lecture 2

Lecture 2

13 pages

Paging

Paging

8 pages

Threads

Threads

14 pages

Paging

Paging

13 pages

Paging

Paging

26 pages

Paging

Paging

13 pages

Lecture

Lecture

13 pages

Processes

Processes

14 pages

Paging

Paging

13 pages

Security

Security

17 pages

Threads

Threads

15 pages

Processes

Processes

34 pages

Structure

Structure

10 pages

Lecture 3

Lecture 3

13 pages

Lecture 1

Lecture 1

28 pages

Threads

Threads

15 pages

Paging

Paging

30 pages

Load more
Loading Unlocking...
Login

Join to view Processes 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 Processes 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?