150 likes | 225 Views
Two Digressions. Stacks Producer-Consumer models. 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);
E N D
Two Digressions StacksProducer-Consumer models Digressions:– Producer-Consumer and Stacks
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? Digressions:– Producer-Consumer and Stacks
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 Digressions:– Producer-Consumer and Stacks
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 Digressions:– Producer-Consumer and Stacks
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? Digressions:– Producer-Consumer and Stacks
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 Digressions:– Producer-Consumer and Stacks
“Stack” (continued) • Called routine • Push registers 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 program counter left by calling routine Digressions:– Producer-Consumer and Stacks
Stack (continued) • Definition: context – the region of the stack that provides the execution environment of (a particular 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 Digressions:– Producer-Consumer and Stacks
Discussion Digressions:– Producer-Consumer and Stacks
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 Digressions:– Producer-Consumer and Stacks
Example – Ring Buffer Consumer empties items, starting with first full item empty empty empty Item i+3 empty Item i+1 empty empty Item I+4 Item I+2 Item i First item First free Producer fills items, starting with first free slot Digressions:– Producer-Consumer and Stacks
Example (continued) Consumer Producer Digressions:– Producer-Consumer and Stacks
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; } Implementation with Semphores struct Item { …}; Item buffer[n]; semaphore empty = n, full = 0; Digressions:– Producer-Consumer and Stacks
Real-world exampleI/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 Digressions:– Producer-Consumer and Stacks
Summary: Producer-Consumer • Occurs frequently throughout computing • Needed for de-coupling the timing of two activities • Uses whatever synchronization mechanism is available (next topic) Digressions:– Producer-Consumer and Stacks