Date post: | 24-Dec-2015 |
Category: |
Documents |
Upload: | christopher-gallagher |
View: | 227 times |
Download: | 0 times |
Copyright 2013 – Noah Mendelsohn
Process Memory
Noah MendelsohnTufts University Email: [email protected]: http://www.cs.tufts.edu/~noah
COMP 40: Machine Structure and
Assembly Language Programming (Fall 2014)
© 2010 Noah Mendelsohn3
Sharing the Computer
© 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
© 2010 Noah Mendelsohn5
Sharing the Computer
© 2010 Noah Mendelsohn
MAIN MEMORY
CPU
Sharing Memory
Angry Birds Play Video Browser
Multiple ProgramsRunning at once
All programs share memory
OPERATING SYSTEM
© 2010 Noah Mendelsohn
MAIN MEMORY
CPU
Sharing the CPU
Angry Birds Play Video Browser
Multiple ProgramsRunning 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 nowassume a simple, traditional computer
© 2010 Noah Mendelsohn8
ProcessesThe Fundamental Unit of Work
© 2010 Noah Mendelsohn
Processes
CPUMEMORY
Angry Birds Angry Birds Browser
Disk PrinterKeyboard,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
© 2010 Noah Mendelsohn
Processes
CPUMEMORY
Angry Birds Angry Birds Browser
Disk PrinterKeyboard,mouse,display
The operating system uses special virtual memory
hardware to give each process the illusion of it’s own private memory
© 2010 Noah Mendelsohn11
Process Memory
© 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!
© 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
© 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
© 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…
© 2010 Noah Mendelsohn16
Brief interlude……writing numbers in hex (base 16)
© 2010 Noah Mendelsohn
Hexadecimal
17
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)
As a professional programmer you must be expert using hex!!
Hex conversion and arithmetic will be significant on the
midterm!
© 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 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)
© 2010 Noah Mendelsohn19
Process Memory(continued)
© 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
7fffffffffff
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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
© 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=4n=3n=2n=1
7fffffffffff
© 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=4n=3n=2
7fffffffffff
© 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
© 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
© 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