copyright 2013 – noah mendelsohn process memory noah mendelsohn tufts university email:...

32
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: [email protected] Web: http://www.cs.tufts.edu/~noah COMP 40: Machine Structure and Assembly Language Programming (Fall 2014)

Upload: christopher-gallagher

Post on 24-Dec-2015

232 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

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)

Page 2: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn3

Sharing the Computer

Page 3: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 4: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn5

Sharing the Computer

Page 5: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn

MAIN MEMORY

CPU

Sharing Memory

Angry Birds Play Video Browser

Multiple ProgramsRunning at once

All programs share memory

OPERATING SYSTEM

Page 6: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 7: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn8

ProcessesThe Fundamental Unit of Work

Page 8: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 9: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 10: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn11

Process Memory

Page 11: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

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

Page 12: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 13: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 14: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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…

Page 15: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn16

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

Page 16: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

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

Page 17: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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)

Page 18: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 2010 Noah Mendelsohn19

Process Memory(continued)

Page 19: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 20: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 21: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 22: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 23: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 24: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 25: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 26: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 27: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 28: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 29: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 30: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 31: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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

Page 32: Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Email: noah@cs.tufts.edunoah@cs.tufts.edu Web: noahnoah

© 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