Presentation is loading. Please wait.

Presentation is loading. Please wait.

Digression on Stack and Heaps CS-502 (EMC) Fall 20091 A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.

Similar presentations


Presentation on theme: "Digression on Stack and Heaps CS-502 (EMC) Fall 20091 A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include."— Presentation transcript:

1 Digression on Stack and Heaps CS-502 (EMC) Fall 20091 A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne)

2 Digression on Stack and Heaps CS-502 (EMC) Fall 20092 Heads Up This topic is not covered explicitly in the textbooks It’s something you are supposed to know … … but that often slips through the cracks!

3 Digression on Stack and Heaps CS-502 (EMC) Fall 20093 The Stack (from C background) The place where arguments of a function call are stored The place where registers of the calling function are saved The place where local data of called function is allocated Automatic data The place where called function leaves result for calling function Supports recursive function calls …

4 Digression on Stack and Heaps CS-502 (EMC) Fall 20094 Introduction: 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?

5 Digression on Stack and Heaps CS-502 (EMC) Fall 20095 Compiled code: the caller int x = factorial(100); Put the value “100” somewhere that factorial function can find Put the current program counter somewhere so that factorial function can return to the right place in calling function Provide a place to put the result, so that calling function can find it

6 Digression on Stack and Heaps CS-502 (EMC) Fall 20096 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 factorial 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

7 Digression on Stack and Heaps CS-502 (EMC) Fall 20097 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?

8 Digression on Stack and Heaps CS-502 (EMC) Fall 20098 Answer: a “Stack” Stack – a 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

9 Digression on Stack and Heaps CS-502 (EMC) Fall 20099 “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

10 Digression on Stack and Heaps CS-502 (EMC) Fall 200910 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 (designated) register Occasionally, a linked list Recursion Stack discipline allows multiple contexts for the same function in the stack at the same time

11 Digression on Stack and Heaps CS-502 (EMC) Fall 200911 Stacks in Modern Systems All modern programming languages require a stack Fortran and Cobol did not (non-recursive) All modern processors provide a designated stack pointer register All modern process address spaces provide room for a stack Able to grow to a large size May grow upward or downward

12 Digression on Stack and Heaps CS-502 (EMC) Fall 200912 Process Address Space (Typical) 0x00000000 0xFFFFFFFF Virtual address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP

13 Digression on Stack and Heaps CS-502 (EMC) Fall 200913 Stacks in Multi-threaded Environments Every thread requires its own stack Separate from all other stacks Each stack may grow separately Address space must be big enough to accommodate stacks for all threads

14 Digression on Stack and Heaps CS-502 (EMC) Fall 200914 Stacks in Multi-threaded Address Space 0x00000000 0xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC

15 Digression on Stack and Heaps CS-502 (EMC) Fall 200915 Heap A place for allocating memory that is not part of last-in, first-out discipline I.e., dynamically allocated data structures that survive function calls E.g., strings in C new objects in C++, Java, etc. Anything allocated with malloc() or calloc()

16 Digression on Stack and Heaps CS-502 (EMC) Fall 200916 Process Address Space (Typical) 0x00000000 0xFFFFFFFF Virtual address space program code (text) static data heap (dynamically allocated) stack (dynamically allocated) PC SP

17 Digression on Stack and Heaps CS-502 (EMC) Fall 200917 Dynamically Allocating from Heap malloc() – POSIX standard function Allocates a chunk of memory of desired size Remembers size Returns pointer free () – POSIX standard function Returns previously allocated chunk to heap for reallocation Assumes that pointer is correct!

18 Digression on Stack and Heaps CS-502 (EMC) Fall 200918 Dynamically Allocating from Heap malloc() – POSIX standard function Allocates a chunk of memory of desired size Remembers size Returns pointer free () – POSIX standard function Returns previously allocated chunk to heap for reallocation Assumes that pointer is correct! Storage leak – failure to free something

19 Digression on Stack and Heaps CS-502 (EMC) Fall 200919 Heaps in Modern Systems Many modern programming languages require a heap C++, Java, etc. NOT Fortran Typical process environment Heap grows toward stack — but never shrinks! Multi-threaded environments All threads share the same heap Data structures may be passed from one thread to another.

20 Digression on Stack and Heaps CS-502 (EMC) Fall 200920 Heap in Multi-threaded Address Space 0x00000000 0xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC Heap

21 Digression on Stack and Heaps CS-502 (EMC) Fall 200921 Stacks in Multi-threaded Address Space 0x00000000 0xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC What’s this?

22 Digression on Stack and Heaps CS-502 (EMC) Fall 200922 Questions?


Download ppt "Digression on Stack and Heaps CS-502 (EMC) Fall 20091 A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include."

Similar presentations


Ads by Google