Presentation is loading. Please wait.

Presentation is loading. Please wait.

Process Memory COMP 40: Machine Structure and

Similar presentations


Presentation on theme: "Process Memory COMP 40: Machine Structure and"— Presentation transcript:

1 Process Memory COMP 40: Machine Structure and
Assembly Language Programming (Fall 2014) Process Memory Noah Mendelsohn Tufts University Web:

2 Sharing the Computer

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

4 Sharing the Computer

5 All programs share memory
Sharing Memory Multiple Programs Running at once MAIN MEMORY CPU Angry Birds Play Video Browser OPERATING SYSTEM

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

7 Processes The Fundamental Unit of Work

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

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

10 Process Memory

11 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 Now we’ll learn how your program uses these!

12 The process memory illusion
Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 (BSS)

13 The process memory illusion
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): Yes, that’s 47 ‘1’ bits…however The process memory illusion Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 (BSS)

14 The process memory illusion
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): Yes, that’s 47 ‘1’ bits…however The process memory illusion Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 (BSS) This is a good time for a first tutorial on “hex” numbering…

15 Brief interlude… …writing numbers in hex (base 16)

16 Hex conversion and arithmetic will be significant on the midterm!
Hexadecimal You already know base 10 and base 2, hex is just base 16 Base Digits 2 0,1 10 0,1,2,3,4,5,6,7,8,9 16 0,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) Hex conversion and arithmetic will be significant on the midterm! As a professional programmer you must be expert using hex!!

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

18 Process Memory (continued)

19 The process memory illusion
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 The process memory illusion 7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 (BSS)

20 The process memory illusion
7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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); } (BSS)

21 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

22 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Program file tells how much is needed Static initialized Loaded with your program Text (code)

23 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

24 The process memory illusion
7fffffffffff Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 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.

25 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

26 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

27 The process memory illusion
7fffffffffff argv, environ Stack 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); } Heap (malloc’d) Static uninitialized (BSS) Static initialized Loaded with your program Text (code)

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

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

30 The process memory illusion
7fffffffffff 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). Stack Text (code) Static initialized Static uninitialized Heap (malloc’d) argv, environ Loaded with your program 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 (BSS)

31 The process memory illusion
7fffffffffff argv, environ Stack 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. 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. Heap (malloc’d) Static uninitialized (BSS) Static initialized Text (code)

32 The process memory illusion
Kernel The process memory illusion 7fffffffffff argv, environ Stack 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. 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. Heap (malloc’d) Static uninitialized (BSS) Static initialized Text (code)


Download ppt "Process Memory COMP 40: Machine Structure and"

Similar presentations


Ads by Google