CSE 412 · xv6 Ch. 1–3 · Silberschatz Ch. 1–3, 9, 12, 13

Operating
Systems · Study Guide

The idea from the dinosaur book, the code from xv6, the output from QEMU
قبل ما تبدا سمي الله و صلي عالنبي

Welcome 👋

This interactive guide covers CSE 412 Operating Systems: the first three chapters of the xv6 book (operating-system interfaces, organization, page tables) and the dinosaur-book sections that explain them. Each part follows one rule: read the dinosaur book for the idea, then the xv6 book for the code. Every xv6 output shown was produced by running xv6 in QEMU.

  • 📖 Tutorial Mode: 7 topics in study order, each step labelled with its book and section: plain explanation, a real example, the why, and a quick check. Step-by-step walkthroughs for fork/exec/wait, pipes, redirection, a system call, the page-table walk and booting.
  • 🎴 Flash Cards: quick-recall decks of calls, flags, addresses and rules.
  • ⚡ Practice Quiz: exam-style questions, each explanation showing the working.

💡 Shortest path when your mind is blank: Tutorial 1 steps 4–5 (fork/exec/wait), Tutorial 2 step 5 (pipes), Tutorial 6 step 2 (the page-table walk). The 📋 Cheat Sheet (bottom-right) has every rule and number on one page.

📖
Tutorial Mode
Explanation-first walkthroughs of every topic with try-it checks
0 / 0
🎴
Flash Cards
Quick-recall decks of calls, flags, addresses & rules
0 / 0
🎯
Professor's Focus
Not used in this course.
⚡
Practice Quiz
Exam-style MCQs with hints, explanations & mistake tracking
Professor's Focus

Pick a revision part

Each part is a self-contained Q & A + worked-numerical deck drawn from the course books.

P1
Part 1 · Midterm Revision
Lectures 1–4 — communication model, OSI/TCP-IP, impairments, Shannon/Nyquist, thermal noise.
21 Q & A 6 numericals
P2
Part 2 · Advanced Final
Lectures 5–10 — parity & checksum, CRC circuits, HDLC, sliding window, TSI, routing.
Deep diagrams Mod-2 traces
Sheet questions only Restrict the pool to questions that came directly from the practice sheets
10
15
20
All
of 0
Practice modeShow the correct answer & explanation after each question
Shuffle questionsRandomize the order each attempt
Sheet questions only Restrict the pool to coding tasks pulled from the practice sheets
5
10
15
All
of 0
Mix question stylesInterleave fill, line builder, and scratch coding questions
Shuffle questionsRandomize the order each attempt
Shuffle cardsRandomize the order each session
📋 Cheat Sheet
Check calls, flags, addresses and rules anytime!
TUTORIAL MODEChoose a topic
Which topic do you want to walk through?

🗺️ How the two books fit together: each part starts with the dinosaur book (Silberschatz, Operating System Concepts, 10th ed.) for the idea, then the xv6 book for the code that implements it. Every step is labelled with its book and section (Dino 9.4.1 xv6 §3.1). The order below is the study order.

🌱
1 · Processes: fork, exec, wait (Dino 1.1, 1.4, 3.1, 3.3 → xv6 §1.1)
What an OS does · dual mode & the timer · process states · fork/exec/wait/exit · counting processes · zombies & orphans · walkthrough: the shell running a command
›
🔌
2 · File descriptors, pipes & files (Dino 13.1, 3.7.4, 13.3, 12.3 → xv6 §1.2–1.4)
Shared offsets (real output) · redirection · ordinary & named pipes · the shell's pipe trick · links & inodes · I/O interface · Exercise 1.1 ping-pong
›
🚪
3 · System calls & kernel structure (Dino 2.3, 1.4.2, 2.8 → xv6 §2.1–2.4)
Parameter passing · M/S/U modes & ecall · a real system call, register by register · monolithic vs microkernel · source layout
›
🧬
4 · struct proc, context switch & boot (Dino 3.1.3, 3.2.3, 2.9.2 → xv6 §2.5–2.7)
PCB vs struct proc · two stacks · MAXVA · booting step by step · the first system call · security model · Exercise 2.1 freemem
›
🧮
5 · Paging theory (Dino 9.1, 9.3, 9.4)
Base/limit & binding · pages, frames, p | d · every page/offset exercise worked · TLB & effective access time · protection · two-level tables · inverted tables
›
🌳
6 · Sv39 & xv6 address spaces (xv6 §3.1–3.3, §3.6)
PTE bit by bit · the 3-level walk (real values) · address-split explorer · satp & sfence.vma · the kernel map · counting page-table pages · a real user address space
›
📦
7 · kalloc, exec & the Ch 3 exercises (xv6 §3.4–3.9)
The free list · PGROUNDUP bit by bit · ELF loading · the user stack, checked against QEMU · Exercises 3.1 device tree, 3.3 superpages, 3.4 #! scripts
›
LINKSSources
Where everything comes from
  • 📗 xv6 book: Cox, Kaashoek & Morris, xv6: a simple, Unix-like teaching operating system (RISC-V, revision of 24 Sep 2026), Ch 1–3. MIT 6.1810.
  • 💻 Code: the MIT 6.1810 2024 lab repo, branch util. The book describes a newer upstream xv6, so names differ (kfork → fork, kexec → exec, pause → sleep): each difference is flagged where it matters.
  • 📕 Dinosaur book: Silberschatz, Galvin & Gagne, Operating System Concepts, 10th ed. Practice-exercise answers and errata: os-book.com.
  • 🧪 Every xv6 output in this guide was produced by running xv6 in QEMU, not predicted.
1 / 1
1 / 1
0 ✓
0 ✗
Q1
…
Your Result
0/0
0%
0
Correct
0
Wrong
0
Skipped
Review
Paste the copied review into ChatGPT, Claude, or any LLM for a deeper walkthrough of the questions you missed.
1 / 1
FLASH CARDS
Click the card to flip · use the buttons below to track recall
PROFESSOR'S FOCUSFull Revision Guide (Parts 1 & 2)
Not used in this course.

Items drawn from the CSE 328 Midterm Revision Guide and the Advanced Final Exam Study Companion (Part 2). Theory first, then solved numericals. Click any item to expand.

Show:

📘 Theoretical Q & A

🧮 Solved Numerical Problems

CODING MODEPick a topic
Which topic do you want to practice writing code for?
1 / 1
FILL
…
25:00
Start focus timer
🍅 Focus Timer
Pomodoro / break / stopwatch — keeps counting across pages and refreshes.

🍅 Focus Timer

25:00
Focus Session
Sound chime Play a gentle tone when the timer ends

📋 Exam Reference Sheet

Processes

  • fork(): child's pid in the parent, 0 in the child, −1 on error. Copies memory, registers and fds.
  • exec(path, argv): new memory image; keeps pid, parent, fds; returns only on error.
  • wait(&st): sleeps until a child exits; returns its pid; −1 with no children. exit(st) → ZOMBIE until waited.
  • n forks in a row → 2ⁿ processes including the original.
  • States: UNUSED · USED · SLEEPING · RUNNABLE · RUNNING · ZOMBIE (Dino: new · ready · running · waiting · terminated).
  • Orphans → init (reparent). Timer tick = 0.1 s.

File descriptors & pipes

  • 0 stdin · 1 stdout · 2 stderr; NOFILE = 16 per process; open/pipe/dup take the lowest free fd.
  • fork and dup share one struct file (and its offset); two opens don't.
  • Redirect in the child: close(1); open(f, O_WRONLY|O_CREATE|O_TRUNC) (= 0x601); exec.
  • pipe(p): p[0] read, p[1] write. read → 0 only when every write end is closed. Close the ends you don't use.
  • link: nlink++ · unlink: nlink−− · freed at nlink = 0 with no open fd. cd is a shell built-in.

System calls

  • Number in a7, arguments a0–a5, ecall; result in a0.
  • Path: usys.S stub → ecall (hardware: SPP = old mode, SIE = 0, sepc, scause, jump to stvec) → uservec (save x1–x31 to trapframe, kernel satp) → usertrap (epc += 4) → syscall() → sys_* → usertrapret → userret → sret.
  • scause: 8 ecall · 12 instr. page fault · 13 load page fault · 15 store page fault.
  • SYS: 1 fork · 2 exit · 3 wait · 4 pipe · 5 read · 7 exec · 10 dup · 11 getpid · 12 sbrk · 13 sleep · 15 open · 16 write · 21 close.
  • Modes: M (3) · S (1) · U (0). sstatus.SPP = previous mode (0 = user).

Kernel structure & boot

  • xv6 = monolithic. Microkernel = services as user-mode servers + IPC.
  • ROM → _entry (sp = stack0 + (hartid+1)·4096) → start() M-mode (MPP=S, mepc=main, satp=0, delegate) → mret → main() → userinit → scheduler → forkret → initcode → exec("/init") → sh.
  • struct proc: state, pid, parent, kstack, sz, pagetable, trapframe (user regs), context (kernel regs), ofile[], cwd.
  • MAXVA = 1L << 38 = 0x4000000000.

Paging (Dino Ch 9)

  • Space 2ᵐ, page 2ⁿ: p = high m−n bits, d = low n bits. Physical = f × page size + d.
  • Logical bits = log₂(pages) + log₂(page size); physical bits = log₂(frames) + log₂(page size).
  • EAT = h·(hit) + (1−h)·(miss); miss = page-table access + data. Say whether TLB time counts.
  • Internal fragmentation ≤ 1 page (avg ½); no external fragmentation.
  • Two-level 32-bit, 4 KB: 10 | 10 | 12. Flat table: 2²⁰ entries × 4 B = 4 MB.
  • Single-level entries = 2^(virtual bits − offset bits); inverted entries = number of frames.
  • No ASIDs → flush the TLB on every switch. Valid–invalid bit, PTLR, shared reentrant pages.

Sv39

  • VA: L2 = 38–30 · L1 = 29–21 · L0 = 20–12 · offset = 11–0. PX(l, va) = (va >> (12 + 9l)) & 0x1FF.
  • 512 PTEs × 8 B = one 4096-byte page per table. A level-0 table covers 2 MB; level-1, 1 GB.
  • PTE: V0 R1 W2 X3 U4 G5 A6 D7 · RSW 8–9 · PPN 10–53. PTE2PA = (pte>>10)<<12, PA2PTE = (pa>>12)<<10.
  • V with R=W=X=0 → next table; any of R/W/X → leaf.
  • MAKE_SATP = (8L<<60) | (root>>12); sfence.vma before and after writing satp.

xv6 address spaces

  • Kernel: direct map VA = PA, KERNBASE 0x80000000 – PHYSTOP 0x88000000; text R-X, data RW-; UART 0x10000000, VIRTIO 0x10001000, PLIC 0x0C000000.
  • TRAMPOLINE = MAXVA − 4096 = 0x3FFFFFF000 (R-X, no U); TRAPFRAME = 0x3FFFFFE000; KSTACK(p) = TRAMPOLINE − (p+1)·8192.
  • User (util branch): text 0x0 (R-X-U) · data 0x1000 (RW-U) · guard 0x2000 (U cleared) · stack 0x3000–0x4FFF · heap 0x5000↑.

kalloc

  • Free list of 4096-byte pages, struct run { next } inside each free page, under kmem.lock.
  • kinit → freerange(end, PHYSTOP) with PGROUNDUP = (a+4095) & ~4095. kfree fills with 1s; kalloc fills with 5s.

exec

  • namei → ELF magic 0x7F 'E' 'L' 'F' → proc_pagetable → for each LOAD: checks, uvmalloc(vaddr+memsz), loadseg(filesz).
  • Checks: memsz ≥ filesz; vaddr + memsz doesn't overflow; vaddr % 4096 = 0.
  • Stack: PGROUNDUP(sz), USERSTACK+1 pages, uvmclear the lowest (guard); push strings, then argv[]; sp aligned down to 16.
  • argc → a0 (return value); argv → trapframe a1; epc = elf.entry. Commit (switch pagetable, free old) last.

Final answers (each one is worked step by step in the tutorials)

  • Dino 3.1: PARENT: value = 5
  • Dino 3.2: 8 processes (7 children)
  • Dino 3.11: 16 processes
  • Dino 3.13: A = 0, B = 2603, C = 2603, D = 2600
  • Dino 3.16: X: CHILD: 0 −1 −4 −9 −16 · Y: PARENT: 0 1 2 3 4
  • Dino 3.12: LINE J only if execlp fails
  • Dino 1.6: privileged: a, c, e, f, h
  • Dino 9.4: 16 logical bits, 15 physical
  • Dino 9.7: 3/13 · 41/111 · 210/161 · 634/784 · 1953/129
  • Dino 9.8: 1,024 · 32 · 64 KB
  • Dino 9.9: 20 bits, 18 bits
  • Dino 9.10: 2²⁰ · 2¹⁷
  • Dino 9.21: 20/725 · 160/410 · 118/525 · 16093/83 · 26614/451
  • Dino 9.22: 4,096 · 256 · 1 MB
  • Dino 9.23: 23 bits, 21 bits
  • Dino 9.24: 2¹⁹ · 2¹⁷
  • Dino 9.25: 100 ns · 64.5 ns
  • Dino 9.28: 19986 → page 4, offset 3602
  • xv6 1.1: 10,000 exchanges in 43 ticks ≈ 2,325 per second (QEMU, 1 CPU)
  • xv6 2.1: kfreemem() walks kmem.freelist; 32,592 free pages at the prompt
  • xv6 3.1: memory@ node reg = base 0x80000000, size 128 MB (256 MB with -m 256M)
  • xv6 3.3: kvminit 166 → 103 pages; usertests pass
  • xv6 3.4: myprog arg1 → /echo myprog arg1