Download presentation
Presentation is loading. Please wait.
1
Outline for this evening
Comments on Programming Project #1 Two digressions Stacks Producer-Consumer models Inter-process Communication Linking & Loading Memory Management If enough time Programming Project #2 assignment CS-502 Fall 2006 Two Digressions
2
Stacks Producer-Consumer models
Two Digressions Stacks Producer-Consumer models CS-502 Fall 2006 Two Digressions
3
Digression: the “Stack”
Imagine the following program:– int factorial(int n){ if (n <= 1) return (1); else int y = factorial(n-1); return (y * n); } Imagine also the caller:– int x = factorial(100); What does compiled code look like? CS-502 Fall 2006 Two Digressions
4
Compiled code: the caller
int x = factorial(100); Put the value “100” somewhere that factorial can find Put the current program counter somewhere so that factorial can return to the right place in caller Provide a place to put the result, so that caller can find it CS-502 Fall 2006 Two Digressions
5
Compiled code: factorial function
Save the caller’s registers somewhere Get the argument n from the agreed-upon place Set aside some memory for local variables and intermediate results – i.e., y, n - 1 Do whatever it was programmed to do Put the result where the caller can find it Restore the caller’s registers Transfer back to the program counter saved by the caller CS-502 Fall 2006 Two Digressions
6
Question: Where is “somewhere”?
So that caller can provide as many arguments as needed (within reason)? So that called routine can decide at run-time how much temporary space is needed? So that called routine can call any other routine, potentially recursively? CS-502 Fall 2006 Two Digressions
7
Answer: a “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Calling program Push arguments & return address onto stack After return, pop result off stack CS-502 Fall 2006 Two Digressions
8
“Stack” (continued) Called routine
Push registers and return address onto stack Push temporary storage space onto stack Do work of the routine Pop registers and temporary storage off stack Leave result on stack Return to address left by calling routine CS-502 Fall 2006 Two Digressions
9
Stack (continued) Definition: context – the region of the stack that provides the execution environment of a call to a function Implementation Usually, a linear piece of memory and a stack pointer contained in a (fixed) register Occasionally, a linked list Recursion Stack discipline allows multiple contexts for the same function in the stack at the same time CS-502 Fall 2006 Two Digressions
10
Questions? CS-502 Fall 2006 Two Digressions
11
Producer-Consumer Model
Definition: a method by which one process communicates a (potentially infinite) stream of data through a finite buffer. Buffer:– a temporary storage area for data Esp. an area by which two processes (or computational activities) at different speeds can be decoupled from each other CS-502 Fall 2006 Two Digressions
12
Example – Ring Buffer Consumer empties items, starting with first full item empty empty empty empty empty empty Item i Item I+2 Item i+1 Item i+3 Item I+4 First item First free Producer fills items, starting with first free slot CS-502 Fall 2006 Two Digressions
13
Implementation with Semphores
struct Item { … }; Item buffer[n]; semaphore empty = n, full = 0; Producer: int j = 0; while (true) { wait_s(empty); produce(buffer[j]); post_s(full); j = (j+1) mod n; } Consumer: int k = 0; while (true) { wait_s(full); consume(buffer[k]); post_s(empty); k = (k+1) mod n; } CS-502 Fall 2006 Two Digressions
14
Real-world example I/O overlapped with computing
Producer: the input-reading process: Reads data as fast as device allows Waits for physical device to transmit records Unbuffers blocked records into ring buffer Consumer Computes on each record in turn Is freed from the details of waiting and unblocking physical input CS-502 Fall 2006 Two Digressions
15
Example (continued) Consumer Producer CS-502 Fall 2006 Two Digressions
16
Summary: Producer-Consumer
Occurs frequently throughout computing Needed for decoupling the timing of two activities Uses whatever synchronization mechanism is available CS-502 Fall 2006 Two Digressions
17
Questions? Next topic CS-502 Fall 2006 Two Digressions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.