1 / 15

Two Digressions

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);

Download Presentation

Two Digressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Two Digressions StacksProducer-Consumer models Digressions:– Producer-Consumer and Stacks

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. “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

  8. 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

  9. Discussion Digressions:– Producer-Consumer and Stacks

  10. 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

  11. 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

  12. Example (continued) Consumer Producer Digressions:– Producer-Consumer and Stacks

  13. 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

  14. 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

  15. 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

More Related