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

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

Procedure Calls Prof. Sirer CS 316 Cornell University.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Code generation I Lecture 20.
Run-Time Storage Organization
Processes 1 CS502 Spring 2006 Processes Week 2 – CS 502.
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Midterm Tuesday October 23 Covers Chapters 3 through 6 - Buses, Clocks, Timing, Edge Triggering, Level Triggering - Cache Memory Systems - Internal Memory.
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Recursion and Function Implementation CS-2301 D-term Recursion and Implementation of Functions CS-2301 System Programming C-term 2009 (Slides include.
Recursion and Implementation of Functions
System Calls 1.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Introduction to Processes CS Intoduction to Operating Systems.
Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
RecursionCIS 1057 Fall Recursion CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed from Professor Hugh C. Lauer. Slides.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Chapter 14 Functions.
Storage Allocation Mechanisms
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Processes and threads.
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
The Stack.
Operating Systems (CS 340 D)
Chapter 3: Process Concept
Outline for this evening
Chapter 14 Functions.
Recursion, Tail Recursion
Operating Systems (CS 340 D)
Chapter 10 The Stack.
Chapter 9 :: Subroutines and Control Abstraction
Stack Frame Linkage.
10/4: Lecture Topics Overflow and underflow Logical operations
Recursion and Implementation of Functions
Languages and Compilers (SProg og Oversættere)
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Procedures and Calling Conventions
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Where is all the knowledge we lost with information? T. S. Eliot
Chapter 3: Process Concept
Chapter 14 Functions.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

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

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?

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

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

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?

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

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

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

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

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

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

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

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;

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

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)