Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models.

Similar presentations


Presentation on theme: "CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models."— Presentation transcript:

1 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models

2 CS 3013 & CS 502 Summer 2006 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?

3 CS 3013 & CS 502 Summer 2006 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

4 CS 3013 & CS 502 Summer 2006 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

5 CS 3013 & CS 502 Summer 2006 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?

6 CS 3013 & CS 502 Summer 2006 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

7 CS 3013 & CS 502 Summer 2006 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

8 CS 3013 & CS 502 Summer 2006 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

9 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 9 Discussion

10 CS 3013 & CS 502 Summer 2006 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

11 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 11 Example – Ring Buffer Item i Item i+1 Item I+2 Item i+3 Item I+4 empty First item First free Producer fills items, starting with first free slot Consumer empties items, starting with first full item

12 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 12 Example (continued) Consumer Producer

13 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 13 Implementation with Semphores 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; } struct Item { … }; Item buffer[n]; semaphore empty = n, full = 0;

14 CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 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

15 CS 3013 & CS 502 Summer 2006 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)


Download ppt "CS 3013 & CS 502 Summer 2006 Digressions:– Producer-Consumer and Stacks 1 Two Digressions Stacks Producer-Consumer models."

Similar presentations


Ads by Google