CPS 110 Intro to processes Landon Cox OS

  • Slides: 49
Download presentation
CPS 110: Intro to processes Landon Cox

CPS 110: Intro to processes Landon Cox

OS Complexity êLines of code êXP: 40 million êLinux 2. 6: 6 million ê(mostly

OS Complexity êLines of code êXP: 40 million êLinux 2. 6: 6 million ê(mostly driver code) êSources of complexity êMultiple instruction streams (processes) êMultiple interrupt sources (I/O, timers, faults) êHow can we keep everything straight?

Dealing with complexity êProgram decomposition êFunctions êOO: Classes, types int main () { cout

Dealing with complexity êProgram decomposition êFunctions êOO: Classes, types int main () { cout << “input: ”; cin >> input; output = sqrt (input); output = pow (output, 3); cout << output << endl; } int main () { get. Input (); compute. Result (); print. Output (); } void get. Input () { cout << “input: ”; cin >> input; } void computer. Result () { output = sqrt (input); output = pow (output, 3); } void print. Output () { cout << output << endl; }

Intro to processes êDecompose activities into separate tasks êAllow them to run in parallel

Intro to processes êDecompose activities into separate tasks êAllow them to run in parallel ê“Independently” (what does this mean? ) ê“without dependencies” … êKey OS abstraction: processes êRun independently of each other êDon’t have to know about others

Intro to processes êRemember, for any area of OS, ask êWhat interface does the

Intro to processes êRemember, for any area of OS, ask êWhat interface does the hardware provide? êWhat interface does the OS provide? êWhat is physical reality? êSingle computer (CPU + memory) êExecute instructions from many programs êWhat does an application see? êEach app thinks it has its own CPU + memory

Hardware, OS interfaces Applications Job 1 Job 2 Job 3 CPU, Mem Hardware OS

Hardware, OS interfaces Applications Job 1 Job 2 Job 3 CPU, Mem Hardware OS Memory CPU

What is a process? êInformal êA program in execution êRunning code + things it

What is a process? êInformal êA program in execution êRunning code + things it can read/write êProcess ≠ program êFormal ê≥ 1 threads in their own address space ê(soon threads will share an address space)

Course administration ê Project 0 due soon (+), groups due today (+) ê Post

Course administration ê Project 0 due soon (+), groups due today (+) ê Post questions to the blackboard message board ê Once I have your group + you have your CS account, you can submit ê Project 1 will be out next Wednesday ê Will be due in about a month ê Discussion sections ê F 2: 50 – 4: 05 ê Any other questions?

Parts of a process êThread êSequence of executing instructions êActive: does things êAddress space

Parts of a process êThread êSequence of executing instructions êActive: does things êAddress space êData the process uses as it runs êPassive: acted upon by threads ê Identity, security context, open resources, etc…

Play analogy êProcess is like a play performance êProgram is like the play’s script

Play analogy êProcess is like a play performance êProgram is like the play’s script What are threads? What is the address space? Threads Address space

What is in the address space? êProgram code êInstructions, also called “text” êData segment

What is in the address space? êProgram code êInstructions, also called “text” êData segment êGlobal variables, static variables êHeap (where “new” memory comes from) êStack êWhere local variables are stored

Review of the stack êEach stack frame contains a function’s êLocal variables êParameters êReturn

Review of the stack êEach stack frame contains a function’s êLocal variables êParameters êReturn address êSaved values of calling function’s registers êThe stack enables recursion

Example stack Code 0 x 8048347 void C () { A (0); } 0

Example stack Code 0 x 8048347 void C () { A (0); } 0 x 8048354 void B () { C (); } 0 x 8048361 void A (int tmp){ if (tmp) B (); } 0 x 804838 c int main () { A (1); return 0; } Memory 0 xffff … Stack A tmp=0 RA=0 x 8048347 SP C const=0 RA=0 x 8048354 SP B A main 0 x 0 SP RA=0 x 8048361 SP tmp=1 RA=0 x 804838 c SP const 1=1 const 2=0

The stack and recursion Code Memory 0 xfffffff 0 x 8048361 0 x 804838

The stack and recursion Code Memory 0 xfffffff 0 x 8048361 0 x 804838 c void A (int bnd){ if (bnd) A (bnd-1); } int main () { A (3); return 0; } How can recursion go wrong? Can overflow the stack … Keep adding frame after frame … 0 x 0 Stack SP A bnd=0 RA=0 x 8048361 SP A bnd=1 RA=0 x 8048361 SP bnd=2 A RA=0 x 8048361 P S bnd=3 A RA=0 x 804838 c P S const 1=3 main const 2=0

The stack and buffer overflows Memory Code void cap (char* b){ for (int i=0;

The stack and buffer overflows Memory Code void cap (char* b){ for (int i=0; b[i]!=‘’; i++) 0 x 8048361 } b[i]+=32; int main(char*arg) { char wrd[4]; strcpy(arg, wrd); cap (wrd); return 0; 0 x 804838 c } What can go wrong? Can overflow wrd variable … Overwrite cap’s RA Stack 0 xfffffff SP … 0 x 0 b= 0 x 00234 cap RA=0 x 804838 c SP wrd[3] wrd[2] wrd[1] main wrd[0] 0 x 00234 const 2=0

What is missing? êWhat process state isn’t in the address space? êRegisters êProgram counter

What is missing? êWhat process state isn’t in the address space? êRegisters êProgram counter (PC) êGeneral purpose registers êReview 104 for more details

Multiple threads in an addr space êSeveral actors on a single set êSometimes they

Multiple threads in an addr space êSeveral actors on a single set êSometimes they interact (speak, dance) êSometimes they are apart (different scenes)

Private vs global thread state êWhat state is private to each thread? êPC (where

Private vs global thread state êWhat state is private to each thread? êPC (where actor is in his/her script) êStack, SP (actor’s mindset) êWhat state is shared? êGlobal variables, heap ê(props on set) êCode (like lines of a play)

Looking ahead: concurrency êConcurrency êHaving multiple threads active at one time êThread is the

Looking ahead: concurrency êConcurrency êHaving multiple threads active at one time êThread is the unit of concurrency êPrimary topics êHow threads cooperate on a single task êHow multiple threads can share the CPU êSubject of Project 1

Looking ahead: address spaces êAddress space êUnit of “state partitioning” êPrimary topics êMany addr

Looking ahead: address spaces êAddress space êUnit of “state partitioning” êPrimary topics êMany addr spaces sharing physical memory êEfficiency êSafety (protection) êSubject of Project 2

Thread independence ê Ideal decomposition of tasks: êTasks are completely independent êRemember our earlier

Thread independence ê Ideal decomposition of tasks: êTasks are completely independent êRemember our earlier definition of independence ê Is such a pure abstraction really feasible? êWord saves a pdf, starts acroread, which reads the pdf? êRunning mp 3 player, while compiling 110 project? ê Sharing creates dependencies êSoftware resource (file, address space) êHardware resource (CPU, monitor, keyboard)

True thread independence ê What would pure independence actually look like? ê(system with no

True thread independence ê What would pure independence actually look like? ê(system with no shared software, hardware resources) ê Multiple computer systems êEach running non-interacting programs êTechnically still share the power grid … ê “Pure” independence is infeasible êTension between software dependencies, “features” ê Key question: is the thread abstraction still useful? êEasier to have one thread with multiple

Consider a web server êOne processor (say) êMultiple disks êTasks êReceives multiple, simultaneous requests

Consider a web server êOne processor (say) êMultiple disks êTasks êReceives multiple, simultaneous requests êReads web pages from disk êReturns on-disk files to requester

Web server (single thread) êOption 1: could handle requests serially Client 1 WS Client

Web server (single thread) êOption 1: could handle requests serially Client 1 WS Client 2 R 1 arrives Receive R 1 Disk request 1 a R 2 arrives 1 a completes R 1 completes Receive R 2 êEasy to program, but painfully slow (why? )

Web server (event-driven) êOption 2: use asynchronous I/O êFast, but hard to program (why?

Web server (event-driven) êOption 2: use asynchronous I/O êFast, but hard to program (why? ) Client 2 Client 1 WS Disk R 1 arrives Receive R 1 Disk request 1 a R 2 arrives Receive R 2 1 a completes R 1 completes Start 1 a Finish 1 a

Web server (multi-threaded) êOption 3: assign one thread per request Client 1 WS 2

Web server (multi-threaded) êOption 3: assign one thread per request Client 1 WS 2 Client 2 R 1 arrives Receive R 1 Disk request 1 a R 2 arrives 1 a completes R 1 completes Receive R 2 êWhere is each request’s state stored?

Threads are useful êIt cannot provide total independence êBut it is still a useful

Threads are useful êIt cannot provide total independence êBut it is still a useful abstraction! êThreads make concurrent programming easier êThread system manages sharing the CPU ê(unlike in event-driven case) êApps can encapsulate task state w/i a thread ê(e. g. web request state)

Where are threads used? ê When a resource is slow, don’t want to wait

Where are threads used? ê When a resource is slow, don’t want to wait on it ê Windowing system êOne thread per window, waiting for window input êWhat is slow? êHuman input, mouse, keyboard ê Network file/web/DB server êOne thread per incoming request êWhat is slow? êNetwork, disk, remote user (e. g. ATM bank customer)

Where are threads used? ê When a resource is slow, don’t want to wait

Where are threads used? ê When a resource is slow, don’t want to wait on it ê Operating system kernel êOne thread waits for keyboard input êOne thread waits for mouse input êOne thread writes to the display êOne thread writes to the printer êOne thread receives data from the network card êOne thread per disk … êJust about everything except the CPU is slow

Cooperating threads êAssume each thread has its own CPU êWe will relax this assumption

Cooperating threads êAssume each thread has its own CPU êWe will relax this assumption later Memory Thread A Thread B Thread C CPU CPU êCPUs run at unpredictable speeds êSource of non-determinism

Non-determinism and ordering Thread A Thread B Thread C Global ordering Why do we

Non-determinism and ordering Thread A Thread B Thread C Global ordering Why do we care about the global ordering? Might have dependencies between events Different orderings can produce different results Why is this ordering unpredictable? Can’t predict how fast processors will run Time

Non-determinism example 1 ê Thread A: cout << “ABC”; ê Thread B: cout <<

Non-determinism example 1 ê Thread A: cout << “ABC”; ê Thread B: cout << “ 123”; ê Possible outputs? ê“A 1 BC 23”, “ABC 123”, … ê Impossible outputs? Why? ê“ 321 CBA”, “B 12 C 3 A”, … ê What is shared between threads? êScreen, maybe the output buffer

Non-determinism example 2 ê y=10; ê Thread A: int x = y+1; ê Thread

Non-determinism example 2 ê y=10; ê Thread A: int x = y+1; ê Thread B: y = y*2; ê Possible results? êA goes first: x = 11 and y = 20 êB goes first: y = 20 and x = 21 ê What is shared between threads? êVariable y

Non-determinism example 3 êx=0; êThread A: x = 1; êThread B: x = 2;

Non-determinism example 3 êx=0; êThread A: x = 1; êThread B: x = 2; êPossible results? êB goes first: x = 1 êA goes first: x = 2 êIs x = 3 possible?

Example 3, continued êWhat if “x = <int>; ” is implemented as êx :

Example 3, continued êWhat if “x = <int>; ” is implemented as êx : = x & 0 êx : = x | <int> êConsider this schedule êThread A: êThread B: êThread A: x x : = : = x x & & | | 0 0 1 2

Atomic operations êMust know what operations are atomic êbefore we can reason about cooperation

Atomic operations êMust know what operations are atomic êbefore we can reason about cooperation êAtomic êIndivisible êHappens without interruption êBetween start and end of atomic action êNo events from other threads can occur

Review of examples êPrint example (ABC, 123) êWhat did we assume was atomic? êWhat

Review of examples êPrint example (ABC, 123) êWhat did we assume was atomic? êWhat if “print” is atomic? êWhat if printing a char was not atomic? êArithmetic example (x=y+1, y=y*2) êWhat did we assume was atomic?

Atomicity in practice êOn most machines êMemory assignment/reference is atomic êE. g. : a=1,

Atomicity in practice êOn most machines êMemory assignment/reference is atomic êE. g. : a=1, a=b êMany other instructions are not atomic êE. g. : double-precision floating point store ê(often involves two memory operations)

Virtual/physical interfaces Applications OS If you don’t have atomic operations, you can’t make one.

Virtual/physical interfaces Applications OS If you don’t have atomic operations, you can’t make one. Hardware

Another example êTwo threads (A and B) êA tries to increment i êB tries

Another example êTwo threads (A and B) êA tries to increment i êB tries to decrement i Thread A: i = o; while (i < 10){ i++; } print “A done. ” Thread B: i = o; while (i > -10){ i--; } print “B done. ”

Example continued êWho wins? êDoes someone have to win? Thread A: i = o;

Example continued êWho wins? êDoes someone have to win? Thread A: i = o; while (i < 10){ i++; } print “A done. ” Thread B: i = o; while (i > -10){ i--; } print “B done. ”

Example continued ê Will it go on forever if both threads êStart at about

Example continued ê Will it go on forever if both threads êStart at about the same time êAnd execute at exactly the same speed? êYes, if each C statement is atomic. Thread A: i = o; while (i < 10){ i++; } print “A done. ” Thread B: i = o; while (i > -10){ i--; } print “B done. ”

Example continued êWhat if i++/i-- are not atomic? êtmp : = i+1 êi :

Example continued êWhat if i++/i-- are not atomic? êtmp : = i+1 êi : = tmp ê(tmp is private to A and B)

Example continued ê Non-atomic i++/i-êIf A starts ½ statement ahead, B can win ê

Example continued ê Non-atomic i++/i-êIf A starts ½ statement ahead, B can win ê How? Thread A: Thread B: tmp. A tmp. B i : = : = i + 1 // tmp. A : = i - 1 // tmp. B tmp. A // i == tmp. B // i == == 1 == -1 1 -1

Example continued ê Non-atomic i++/i-êIf A starts ½ statement ahead, B can win ê

Example continued ê Non-atomic i++/i-êIf A starts ½ statement ahead, B can win ê How? ê Do you need to worry about this? êYes!!! No matter how unlikely

Debugging non-determinism êRequires worst-case reasoning êEliminate all ways for program to break êDebugging is

Debugging non-determinism êRequires worst-case reasoning êEliminate all ways for program to break êDebugging is hard êCan’t test all possible interleavings êBugs may only happen sometimes êHeisenbug êRe-running program may make the bug disappear êDoesn’t mean it isn’t still there!

Constraining concurrency êSynchronization êControlling thread interleavings êSome events are independent êNo shared state êRelative

Constraining concurrency êSynchronization êControlling thread interleavings êSome events are independent êNo shared state êRelative order of these events don’t matter êOther events are dependent êOutput of one can be input to another êTheir order can affect program results

Goals of synchronization 1. All interleavings must give correct result êCorrect concurrent program êWorks

Goals of synchronization 1. All interleavings must give correct result êCorrect concurrent program êWorks no matter how fast threads run êImportant for your projects! 2. Constrain program as little as possible êWhy? êConstraints slow program down êConstraints create complexity

Conclusion êNext class: more cooperation ê“How do actors interact on stage? ” êAfter next

Conclusion êNext class: more cooperation ê“How do actors interact on stage? ” êAfter next week êShould be able to start Project 1 êReview C++/STL this weekend êRemember to send me your groups!