ECEC 201 · Week 4

Modern Computer: Von Neumann

A running program is a process, and a process has a map of its memory: text for the code, data and bss for globals, the heap for malloc, and the stack for locals.

Week 4 of 10 · Das Frite

ECEC 201 · Week 4 · In class

A running program is a process, and a process has a map of its memory: text for the code, data and bss for globals, the heap for malloc, and the stack for locals.

ECEC 201 · Week 4 · In class

Print one address from each region and find it on the map from the same run.

ECEC 201 · Week 4 · Lecture

Where each kind of object lives

RegionHoldsLives for
textmachine code of functionsthe whole run, read-only
dataglobals and statics with an initial valuethe whole run
bssglobals and statics that start at zerothe whole run
heapmalloc blocksuntil free
stacklocals and call framesuntil the function returns

ECEC 201 · Week 4 · Lecture

Reading /proc/self/maps

start-end     perms offset   dev   inode  pathname

ECEC 201 · Week 4 · Lecture

Printing an address and the map in one run

#include <stdio.h>

int main(void)
{
	char line[256];
	FILE *f = fopen("/proc/self/maps", "r");
	if (!f)
		return 1;
	printf("main at %p\n", (void *)main);
	while (fgets(line, sizeof line, f))
		fputs(line, stdout);
	fclose(f);
	return 0;
}

ECEC 201 · Week 4 · Lecture

Why addresses move between runs

ECEC 201 · Week 4 · Lab

Lab

  1. Print the address of a function, a global, a static variable, a `malloc` block, and a local variable.
  2. In the same run, print `/proc/self/maps` and mark the region each address falls in. Second pass: run it twice more and note which addresses move between runs.
  3. Assemble the lab hand-in: The program, the five addresses marked on the memory map, and one sentence on which addresses moved between runs and why.

ECEC 201 · Week 4 · Lab

Hand in

The program, the five addresses marked on the memory map, and one sentence on which addresses moved between runs and why.

ECEC 201 · Week 4 · Homework

Homework

ECEC 201 · Week 4 · Homework

Turn in

The drawing with each region named.

ECEC 201 · Week 4 · Read

Read

On the lab site: Module 5, Memory bring-up, covers the DRAM. This week grades one process's map of that memory.

ECEC 201 · Week 4 · Next

Week 5

Memory you ask for with malloc is yours to give back with free.

Arrow keys or space to move, F for full screen, N for speaker notes, Home and End for the first and last slide. Print gives one slide per page.