1 / 32

Process Memory

COMP 40: Machine Structure and Assembly Language Programming (Fall 2014 ). Process Memory. Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah. Sharing the Computer. Operating systems do two main things for us:.

Download Presentation

Process Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 Play Video Angry Birds Browser OPERATING SYSTEM MAIN MEMORY CPU

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

  7. ProcessesThe 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 Disk Keyboard,mouse,display

  9. 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 Printer Disk Keyboard,mouse,display

  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 usesthese!

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

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

  14. 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 The process memory illusion argv, environ Stack • 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 Heap (malloc’d) Static uninitialized This is a good time for a first tutorial on “hex” numbering… Static initialized Loaded with your program Text(code) 0

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

  16. Hexadecimal You already know base 10 and base 2, hex is just base 16 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 mustbe expert using hex!!

  17. Conversions between hex and binary are trivial Consider the decimal number 538 Which happens to be 1000011010 (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: 011111111111111111111111111111111111111111111111 (bin) 7fffffffffff (hex)

  18. Process Memory(continued)

  19. 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 argv, environ Stack • 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 Heap (malloc’d) Static uninitialized Static initialized Loaded with your program Text(code) 0

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

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

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

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

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

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

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

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

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

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

  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). argv, environ Stack • 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 Heap (malloc’d) Static uninitialized Static initialized Loaded with your program Text(code) 0

  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 Static initialized Text(code)

  32. 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 Static initialized Text(code)

More Related