MMUs & Virtualization
Algorithms & Replacement
Filesystems
Memory Management
C/Linux/Evil
100

What benefits does virtual memory provide? Name two and explain.

Memory can be more easily protected, isolated, and managed since processes cannot directly work with physical memory.

100

How does First Fit replacement work? Name one benefit and one drawback.

The OS finds the first block of memory that is large enough. First Fit is fast because it stops once a block has been found. A drawback is that it can cause excess external fragmentation (fragments of memory too small to be useful).

100

What are the typical sizes of a block on a physical drive vs filesystem block?

Physical drives usually have 512 byte blocks, while filesystems tend to use 4096 byte blocks.

100

What gets allocated to the heap? What about the stack?

Functions like malloc and free work in the heap, where data can persist beyond the scope of a function. Local variables and function calls live in the stack, which gets cleared when the program reaches the end of their scope.

100

What function returns malloc'd memory to the heap?

free()

200

What is the MMU and what does it do?

It is the Memory Management Unit. It translates virtual addresses into physical addresses.

200

How does Best Fit replacement work? Name one benefit and one drawback.

The OS finds the block of memory closest to the requested size. Best Fit tries to minimize wasted memory. The drawback is that it can be inefficient because it must search the entire list.

200

How do inodes keep track of how many directory entries point to a file? How does this allow file blocks to be freed?

Inodes have a reference count that keeps track of the number of hard links. When it reaches 0, the file blocks can be freed.

200

What is swap space? Why do we need it?

Swap space lives on the disk and is where memory overflows to when RAM is fully used. It is also used to optimize memory usage (e.g. swapping out untouched memory). Without swap, when RAM is fully used, the OS must pick processes to kill in order to free up memory, which causes apps to hang or crash suddenly.

200

See Code Snippet 1

buf is never freed!

Rule of thumb: every malloc should have a corresponding free

300

What is the TLB and what does it store?

It's the Translation Lookaside Buffer. It is a small, fast cache within the MMU that stores recent virtual page to physical frame mappings.

300

What is a soft TLB miss vs a hard TLB miss?

A soft TLB miss occurs when a translation is missing from the TLB but valid in the page table. A hard TLB miss occurs when the translation is invalid, triggering a page fault to retrieve data from disk.

300

If larger filesystem blocks allow for more bandwidth and less inode overhead, why don’t we just use gigantic block sizes? Explain.

Overly large filesystem blocks may leads to excess internal fragmentation, where small files waste space by only needing and using a small amount of the block.

300

What are two times we would want to use the heap instead of the stack?

When data needs to outlive the function that created it, when its size isn’t known until runtime, or when it’s too big for the stack.

300

See Code Snippet 2

Yes, it enters the if statement. Although p is freed, p is not modified and still contains an address (which now points to an invalid region of memory).

400

On a system with a 32-bit virtual address space and 4KB block size (hint: 2^12 = 4096), how many bits are left for the page table index?

20 bits.

A 4KB block size needs 12 bits for the offset. 32-12=20, leaving 20 bits for the virtual page index.

400

How does the Clock page replacement algorithm work?

Pages sit in a circular list with a “hand” pointing at the oldest one. On a page fault, the OS checks that page’s R bit. If R is 0, the page is evicted. If R is 1, the OS clears it to 0, moves the hand forward, and checks the next page.

400

Why do filesystems keep a journal?

Operations like deleting a file takes several steps. A crash part way through leaves the filesystem inconsistent. With a journal, the filesystem writes down what it’s about to do before doing it. After a crash, it replays the journal to finish the job.

400

Why is segmentation less popular than paging?

Segments are different sizes. As they’re loaded and removed, memory fills with gaps between them that are too small to use (external fragmentation). Fixing that requires costly compaction, and each segment must fit in one contiguous chunk of RAM. Pages are all the same size, so any free frame works for any page and there’s no external fragmentation. A process’ memory can also be spread across RAM, and only the pages it’s using need to be loaded.

400

See Code Snippet 3

24 bytes, even though the fields only add up to 10. a at 0, 7 bytes of padding so b sits on an 8-byte boundary at 8, c at 16, then 7 bytes of tail padding so the size is a multiple of 8. Put the two chars next to each other (b, a, c) and it drops to 16.

500

Name at least 3 things stored in a page table entry, and explain why we need them.

Frame number, present/absent bit, protection bits (read/write/execute), modified (dirty) bit, referenced bit, caching-disabled bit.

Present/absent: is the page in RAM? 0 means a page fault.

Modified: the page was written to, so it must be written back to disk before eviction; clean pages can just be dropped.

Referenced: the page was recently accessed, which page replacement algorithms (NRU, Clock, aging) use to pick a victim.

500

How does NFU with aging pick a page to evict? Why is it better than plain NFU?

Each page has a counter. Every clock tick, each counter is shifted right one bit, and the page’s R bit is added as the leftmost bit. The page with the lowest counter is evicted. Plain NFU just adds up references forever, so a page used heavily long ago can never be evicted. Aging lets old references fade away.

500

Why should you eject a USB before removing it?

Writing to disk is slow, so the OS collects many small writes in a buffer in RAM and writes them out together. This means data you “saved” may still be sitting in that buffer and not yet on the drive. Ejecting it forces this write.

500

What is a process’s working set? What happens when the working sets of all running processes don’t fit in RAM?

The working set is the set of pages a process is actively using. If they don’t all fit, processes keep evicting each other’s pages. This is thrashing: constant page faults, the disk always busy, and almost no real work getting done. The fix is more RAM or fewer processes running at once.

500

FINAL JEOPARDY!

With 64-bit addresses and 4 KB pages, a single flat page table would need 2^52 entries per process. What could we do about this?

Create a multilevel page table or an inverted page table.