Lecture 4 Process Memory Layout 1 Review Unix

  • Slides: 18
Download presentation
Lecture 4: Process Memory Layout 1

Lecture 4: Process Memory Layout 1

Review: Unix File Access Control • Subject – Users(owner, group, others) • Object –

Review: Unix File Access Control • Subject – Users(owner, group, others) • Object – Directory/File • Actions – read – write – execute 2 Copyright © 2002 Thomas W. Doeppner. All rights reserved.

System Calls For File Management 3

System Calls For File Management 3

System Calls For Directory Management 4

System Calls For Directory Management 4

System Calls For Miscellaneous Tasks 5

System Calls For Miscellaneous Tasks 5

In this lecture • Process memory layout 6

In this lecture • Process memory layout 6

Linux process memory layout Stack Address space or core image Heap Static data Text

Linux process memory layout Stack Address space or core image Heap Static data Text (program)

Linux process memory layout • Stack: – Automatically allocated and deallocated – Local variables,

Linux process memory layout • Stack: – Automatically allocated and deallocated – Local variables, parameters • Heap: – Manually allocated (malloc) and deallocated (free) – Dynamic sized data structures • Static data – Global variables • Text – Program 8

An example int g; int main() { int a; int*b = (int*)malloc(sizeof(int) *2); return

An example int g; int main() { int a; int*b = (int*)malloc(sizeof(int) *2); return 0; } 9

An example Stack, a Stack, b Heap, b_content Static data, g Text (program)

An example Stack, a Stack, b Heap, b_content Static data, g Text (program)

Multiple processes in memory Unused Stack Process 1 Heap Static data Text (program) Stack

Multiple processes in memory Unused Stack Process 1 Heap Static data Text (program) Stack Process 2 Heap Static data Text (program) Unused 11

Function Call int main() { int a; int f() { int a, b; a=f();

Function Call int main() { int a; int f() { int a, b; a=f(); a=10; b=10; return (a*b); } } 12

Stack Frame To previous stack frame pointer Stack arguments return address stack frame pointer

Stack Frame To previous stack frame pointer Stack arguments return address stack frame pointer local variables Heap Static data Text (program) To the point at which this function was called

Function Call Hierarchy int main() { int a; int f() { int a; int

Function Call Hierarchy int main() { int a; int f() { int a; int g() { int a; a=10; return (a*g()); a=f(); } } a=100; return (a); } 14

Fibonacci Numbers int fib (int n) { fib (3): how many if (n <

Fibonacci Numbers int fib (int n) { fib (3): how many if (n < 0) return 0; stack frames? if (n == 0) return 1; if (n==1) return 1; return (fib(n-1)+fib(n-2)); } 15

Memory leakage Stack, a Stack, b (pointer) Heap, b_content Static data, g Text (program)

Memory leakage Stack, a Stack, b (pointer) Heap, b_content Static data, g Text (program)

Garbage Collection • No need to explicitly deallocate memory i. e. no need for

Garbage Collection • No need to explicitly deallocate memory i. e. no need for free() or equivalent • No memory leaks • Automatic Garbage Collection http: //sleibowitz. home. att. net/CPP_Java/AGC. html • A key feature of Java is its garbagecollected heap http: //www. javaworld. com/javaworld/jw-08 -1996/jw-08 gc. html 17

Summarizing • Process memory layout – Stack – Heap – Static data – Text

Summarizing • Process memory layout – Stack – Heap – Static data – Text • Stack frame for function calls 18