# Memory Maps, Locality, and Isolation

Lab 3 · Memory · Operating Systems
Page: https://alfonsocampodonico.com/classes/operating-systems/lab-memory?lang=en
Code: https://github.com/AlfonsoCampodonico/os-lab/tree/main/starter/lab3-memory-probe (`gh repo clone AlfonsoCampodonico/os-lab`)

> How does the operating system present memory as one address space while still controlling mapping, protection, and performance?

## Guiding Question

How does the operating system present memory as one address space while still
controlling mapping, protection, and performance?

## Concepts

- stack vs heap
- virtual memory intuition
- memory maps
- locality
- `mmap`
- protection and segmentation faults

## Scenario

You are building a memory probe for students who need to see how address spaces
change at runtime. The tool should expose mappings and let you compare access
patterns.

Starter project: `starter/lab3-memory-probe`

## Observation Tasks

1. Run `./zig-out/bin/memory_probe maps`
2. Inspect `/proc/self/maps`
3. Compare sequential and random access modes (use `touch 512` or larger;
   at 8 MB the difference is noise)

Record:

- where the executable, stack, heap, and shared libraries appear
- which access pattern is faster
- one example of a mapping created by `mmap`

## Implementation Tasks

The starter already has a minimal version of items 1–3; read it, explain
each part, then extend it so that it supports:

1. Printing selected memory regions with labels.
2. Sequential and random touch workloads.
3. A small `mmap` demo over a file.
4. One controlled experiment that shows protection or isolation behavior.
5. A short explanation of why locality matters.

## Checkpoints

- What is the difference between heap allocation and memory mapping?
- Why does access order matter for performance?
- What information in `/proc/<pid>/maps` reveals shared libraries?
- What does a segmentation fault tell you about memory protection?

## Deliverables

- updated `memory_probe.zig`
- runtime output for at least two experiments
- answers to the checkpoint questions

## Stretch Goals

- compare page-touch cost for different array sizes
- add a CSV output mode for graphing
- show how a child process inherits mappings after `fork()`

## Suggested Verification

```bash
zig build -p zig-out
./zig-out/bin/memory_probe maps
./zig-out/bin/memory_probe touch 512 sequential
./zig-out/bin/memory_probe touch 512 random
```

## Grading Focus

- accurate reasoning about virtual memory concepts
- useful experiments with clear outputs
- clear connection between observed behavior and theory


## Build it

With Zig **0.16.x** installed (0.17 removed `@cImport`, which the starters use), from the root of the repository:

```sh
zig build -p zig-out
bash scripts/smoke_test.sh
```
