a short digression stacks and heaps
DESCRIPTION
A Short Digression Stacks and Heaps. CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System Concepts , 7 th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems , 2 nd ed., by Tanenbaum). Digression: the “Stack”. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/1.jpg)
Stacks and HeapsCS-502 Fall 2007 1
A Short DigressionStacks and Heaps
CS-502, Operating SystemsFall 2007
(Slides include materials from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems, 2nd ed., by Tanenbaum)
![Page 2: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/2.jpg)
Stacks and HeapsCS-502 Fall 2007 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?
![Page 3: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/3.jpg)
Stacks and HeapsCS-502 Fall 2007 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
![Page 4: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/4.jpg)
Stacks and HeapsCS-502 Fall 2007 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
![Page 5: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/5.jpg)
Stacks and HeapsCS-502 Fall 2007 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?
![Page 6: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/6.jpg)
Stacks and HeapsCS-502 Fall 2007 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
![Page 7: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/7.jpg)
Stacks and HeapsCS-502 Fall 2007 7
“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
![Page 8: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/8.jpg)
Stacks and HeapsCS-502 Fall 2007 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
![Page 9: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/9.jpg)
Stacks and HeapsCS-502 Fall 2007 9
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
![Page 10: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/10.jpg)
Stacks and HeapsCS-502 Fall 2007 10
Process Address Space (Typical)
0x00000000
0xFFFFFFFF
Virtual
address space
program code(text)
static data
heap(dynamically allocated)
stack(dynamically allocated)
PC
SP
See also Silbershatz, figure 3.1
![Page 11: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/11.jpg)
Stacks and HeapsCS-502 Fall 2007 11
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
![Page 12: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/12.jpg)
Stacks and HeapsCS-502 Fall 2007 12
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
![Page 13: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/13.jpg)
Stacks and HeapsCS-502 Fall 2007 13
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
• Some small or RT operating systems are equivalent to multi-threaded environments
![Page 14: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/14.jpg)
Stacks and HeapsCS-502 Fall 2007 14
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.
![Page 15: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/15.jpg)
Stacks and HeapsCS-502 Fall 2007 15
Process Address Space (Typical)
0x00000000
0xFFFFFFFF
Virtual
address space
program code(text)
static data
heap(dynamically allocated)
stack(dynamically allocated)
PC
SP
See also Silbershatz, figure 3.1
![Page 16: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/16.jpg)
Stacks and HeapsCS-502 Fall 2007 16
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!
![Page 17: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/17.jpg)
Stacks and HeapsCS-502 Fall 2007 17
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
![Page 18: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/18.jpg)
Stacks and HeapsCS-502 Fall 2007 18
Heaps in Modern Systems
• Many modern programming languages require a heap
• C++, Java, etc.• NOT Fortran
• Typical process environment• Grows toward stack
• Multi-threaded environments• All threads share the same heap• Data structures may be passed from one thread to
another.
![Page 19: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/19.jpg)
Stacks and HeapsCS-502 Fall 2007 19
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
![Page 20: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/20.jpg)
Stacks and HeapsCS-502 Fall 2007 20
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?
![Page 21: A Short Digression Stacks and Heaps](https://reader035.vdocument.in/reader035/viewer/2022062520/56815bf2550346895dc9e30f/html5/thumbnails/21.jpg)
Stacks and HeapsCS-502 Fall 2007 21
Questions?