Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:

Similar presentations


Presentation on theme: "Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:"— Presentation transcript:

1 Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noahhttp://www.cs.tufts.edu/~noah COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)

2 © 2010 Noah Mendelsohn 2 Today  Brief look at: operating system processes  How processes use memory

3 © 2010 Noah Mendelsohn 3 Sharing the Computer

4 © 2010 Noah Mendelsohn Operating systems do two main things for us: 4 They make the computer easier to use The facilitate sharing of the computer by multiple programs and users

5 © 2010 Noah Mendelsohn 5 Sharing the Computer

6 © 2010 Noah Mendelsohn MAIN MEMORY CPU Sharing Memory Angry Birds Play Video Browser Multiple Programs Running at once All programs share memory OPERATING SYSTEM

7 © 2010 Noah Mendelsohn MAIN MEMORY CPU Sharing the CPU Angry Birds Play Video Browser Multiple Programs Running at once OPERATING SYSTEM CPU is shared…can only do one thing at a time* *Actually, modern CPUs can do a few things at a time, but for now assume a simple, traditional computer

8 © 2010 Noah Mendelsohn 8 Processes The Fundamental Unit of Work

9 © 2010 Noah Mendelsohn Processes CPU MEMORY Angry Birds Browser Disk Printer Keyboard, mouse,display There is one OS “process” for each running copy of a program The operating switches rapidly between them…giving the illusion they are all running at once

10 © 2010 Noah Mendelsohn Processes CPU MEMORY Angry Birds Browser Disk Printer Keyboard, mouse,display The operating system uses special virtual memory hardware to give each process the illusion of it’s own private memory

11 © 2010 Noah Mendelsohn 11 Process Memory

12 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 12 Now we’ll learn how your program uses these!

13 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 13 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0

14 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 14 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in binary): 11111111111111111111111111111111111111111111111 Yes, that’s 47 ‘1’ bits…however

15 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 15 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in binary): 11111111111111111111111111111111111111111111111 Yes, that’s 47 ‘1’ bits…however This is a good time for a first tutorial on “hex” numbering…

16 © 2010 Noah Mendelsohn 16 Brief interlude… …writing numbers in hex (base 16)

17 © 2010 Noah Mendelsohn Hexadecimal 17 You already know base 10 and base 2, hex is just base 16 BaseDigits 20,1 100,1,2,3,4,5,6,7,8,9 160,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, A few simple examples: 16(dec) = 10(hex) 19(dec) = 13(hex) 32(dec) = 20(hex) 256(dec) = 100(hex) As a professional programmer you must be expert using hex!! Hex conversion and arithmetic will be significant on the midterm!

18 © 2010 Noah Mendelsohn Conversions between hex and binary are trivial 18 Consider the decimal number 538 Which happens to be 1000011010 (binary) and 21A (hex) 2 1A You get hex from binary by grouping the bits …and that 48 bit address looks a lot better this way: 011111111111111111111111111111111111111111111111 (bin) 7fffffffffff (hex)

19 © 2010 Noah Mendelsohn 19 Process Memory (continued)

20 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 20 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 Our systems use 48 bit addressing, but it turns out the “top” half of memory is for the operating system. Therefore the highest address you’ll see in your program is (in hex): 7fffffffffff

21 © 2010 Noah Mendelsohn The process memory illusion 21 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

22 © 2010 Noah Mendelsohn The process memory illusion 22 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

23 © 2010 Noah Mendelsohn The process memory illusion 23 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } Program file tells how much is needed 7fffffffffff

24 © 2010 Noah Mendelsohn The process memory illusion 24 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argv[]) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

25 © 2010 Noah Mendelsohn The process memory illusion 25 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argv[], char *envp[]) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

26 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 26 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 malloc/free are library functions that run in user mode to allocate space within the heap…. …when they run out of space to play with, they call the sbrk system call to ask the OS to grow the heap. 7fffffffffff

27 © 2010 Noah Mendelsohn The process memory illusion 27 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 111”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

28 © 2010 Noah Mendelsohn The process memory illusion 28 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 111”; int main(int argc, char *argvp[]*) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

29 © 2010 Noah Mendelsohn The process memory illusion 29 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 char notInitialized[10000]; char initialized[] = “I love COMP 40”; int main(int argc, char *argv[]) { float f; int i; // yes, we should check return codes char *cp = malloc(10000); } 7fffffffffff

30 © 2010 Noah Mendelsohn int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } Of course, the stack enables recursion 30 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 n=4 n=3 n=2 n=1 7fffffffffff

31 © 2010 Noah Mendelsohn int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } Of course, the stack enables recursion 31 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 n=4 n=3 n=2 7fffffffffff

32 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space  Separated into segments, from address 0  Stack: memory for executing subroutines  Heap: memory for malloc/new  Global static variables  Text segment: where program lives 32 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 0 In Linux, there is a system call to grow the heap, but not the stack. When the system faults on an access to the next page below the end of the stack, the OS maps more memory automatically. (up to configurable limit). 7fffffffffff

33 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space.  Separated into segments, from address 0:  Text segment: where program lives.  Global variables.  Heap: memory for malloc/new.  Stack: memory for executing subroutines. 33 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Nothing here Memory between the stack and the heap is not mapped by the OS. As far as the process is concerned, it doesn’t exist. Access causes a segfault. 7fffffffffff

34 © 2010 Noah Mendelsohn The process memory illusion  Process thinks it's running in a private space.  Separated into segments, from address 0:  Text segment: where program lives.  Global variables.  Heap: memory for malloc/new.  Stack: memory for executing subroutines. 34 Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Kernel Surprisingly: the kernel actual lives in every process space at the “top”. The maps are set so ordinary user code segfaults on access, but… …when in the kernel executing a system call, the sytem can access its own kernel segments as well as the user’s. 7fffffffffff


Download ppt "Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:"

Similar presentations


Ads by Google