قبل ما تبدا سمي الله و صلي عالنبي
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.
P2
Part 2 · Advanced Final
Lectures 5–10 — parity & checksum, CRC circuits, HDLC, sliding window, TSI, routing.
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!
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.
cdis 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.vmabefore 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, underkmem.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