Prep: fork, wait, and exec — 51k · 52k¶
Session: Thu Dec 3, 1h45 · Exercises: 51k_fork_wait · 52k_userland · Prep time: ~40 min · Lecture: Week 15 · fork, wait, and the User Shell
A reading, not a lecture. This session's lecture page was written for a Tue Dec 1 lecture, and Dec 1 became an exercise day: read Week 15 · Essentials before class. The session opens with a 15-minute talk.
What you will build¶
First the kernel runs a tree of processes: fork copies the caller, exit parks the child as a zombie holding its status, and wait reaps it. Then exec becomes a system call and the shell leaves the kernel: run sh at the rv6$ prompt drops you into $, an unprivileged program that reaches the kernel only through ecall.
Concepts you need¶
forkreturns twice; what the child gets — Week 15 · One call, two returns, What the child gets- Zombies and reaping — Week 15 ·
exit, the zombie, andwait - The scheduler finally has two processes — Week 15 · The scheduler finally has work
execswaps the address space; only a failure returns — Week 15 · Replace the program, keep the process,execdoes not return on success- The shell is just a program — Week 15 · The shell leaves the kernel · rv6 Architecture: Two shells
- The process tree, and rv6's stand-in for
init— Week 15 · For the exam: The process tree andinit - Why fork/exec and not
spawn— Week 15 · For the exam: Whyforkandexecare two calls
Read before class¶
| What | Time |
|---|---|
| Watch the videos One Fork, Two Paths (6:36), then Exit, Then Wait (6:32) and New Program, Same Process (6:53). Each video's page links the explainer page it walks through | 20 min |
Week 15 · This week, then Thursday · 51k fork, exit, and wait through What the child gets |
5 min |
Week 15 · exit, the zombie, and wait and The scheduler finally has work |
3 min |
Week 15 · Thursday · 52k exec as a system call, all three sections |
4 min |
| Week 15 · For the exam. Not needed today; on the Final | 3 min |
| rv6 Architecture: Two shells | 1 min |
Mental model¶
Redirection, ls > out.txt, is not a feature of exec; it is two ordinary calls in the window between fork and exec:
pid = fork() slot 1: sh, Running slot 2: copy of sh, a0 = 0
child: close(1) fd 1 free in slot 2 only
open("out.txt", O_CREATE|O_WRONLY|O_TRUNC) lowest free fd -> 1
exec("ls", argv) same pid and fds; new page table, epc, sp
...ls writes fd 1, never knowing; exit(0) -> Zombie
parent: wait(&status) yields until slot 2 is a Zombie, then reaps it
fork copies the descriptor table and exec never touches it; that is the case against a single spawn.
Check yourself¶
- Two processes return from the same
forkwith identical memory. How does each know which it is?Answer
Only the return register differs: the child's saveda0is 0, the parent's is the child's pid, and zero is never a valid pid.
What "done" looks like¶
oslings run is green, then oslings submit before you leave. Not green? Submit anyway — the tests that pass earn their share — then finish it at a make-up session (office hours, on the class network) for three quarters, or anywhere else for half, and submit again.
If you finish early¶
Work Week 15 · Problem 1: Predict the output, Problem 2: A child it never forked and Problem 3: Redirect in the window on paper, then read xv6 book chapter 1 and the sleep/wakeup section of chapter 7, which rv6's polling wait omits. Then start Friday's prep page, Prep: Ship Your Commands.