150 lines
8.9 KiB
Markdown
150 lines
8.9 KiB
Markdown
# Exam Rank 06 — `mini_serv` training gym
|
||
|
||
**Goal: walk into Exam Rank 06, get `mini_serv`, submit once, pass. Cold recall — no guessing.**
|
||
|
||
---
|
||
|
||
## What the exam actually is (confirmed)
|
||
|
||
- One file: `mini_serv.c`. A **`select()`-based multi-client chat server** on `127.0.0.1:<port>`.
|
||
- The exam gives you a `main.c` attachment with socket boilerplate + two helpers
|
||
(`str_join`, `extract_message`). You are expected to reuse those helpers.
|
||
- Grading is scripted, automated, **byte-exact**: one listener client is connected, then a
|
||
series of short-lived clients connect / send / disconnect, and the output the listener
|
||
received is `diff`-ed against an expected trace. (The real test is in `tester/grademe/`.)
|
||
- Window: ~2–4 h. No internet. Only the allowed functions from the subject.
|
||
|
||
> ⚠️ **Campus caveat.** In the updated exam format some students get **`mini_db`** (C++98
|
||
> key-value server) instead — sometimes as "level 2" of the same exam. Ask a senior at your
|
||
> campus exactly what exam 06 serves there. 80% of this training (select loop, line
|
||
> buffering, broadcast, clean disconnect) transfers to mini_db; if your campus uses it,
|
||
> tell me and I'll extend this gym.
|
||
|
||
## What exactly gets checked (ground truth from the graders)
|
||
|
||
1. No argument → stderr `Wrong number of arguments\n`, exit status 1.
|
||
2. Bind failure → stderr `Fatal error\n`, exit status 1.
|
||
3. Client N connects → **every other** connected client gets `server: client N just arrived\n`.
|
||
The newcomer gets **nothing** for itself.
|
||
4. Client N sends data → every **other** client gets `client N: ` + every complete `\n`-terminated
|
||
line — one prefix per line, even if one packet contained several lines.
|
||
5. An incomplete line (no `\n` yet) is held in a per-client buffer… and **dropped if the
|
||
client disconnects** — never flushed.
|
||
6. `\x04` (Ctrl-D byte) is plain data, not EOF. Only `recv() <= 0` means disconnect.
|
||
7. Client disconnects → everyone else gets `server: client N just left\n`.
|
||
8. Ids increment forever (`0, 1, 2, …`), never reused.
|
||
9. It must survive a **364 KB single line** and a **10 KB line with no newline** (both are in the test).
|
||
10. No memory / fd leaks. Non-blocking: only call `recv`/`send` after `select` said "ready".
|
||
11. Only listen on `127.0.0.1` (`htonl(2130706433)`). No `#define` in the file.
|
||
|
||
## Files in this gym
|
||
|
||
| Path | What it is |
|
||
|---|---|
|
||
| `subject/subject.en.txt` | The exam subject (verbatim, from the official attachment) |
|
||
| `subject/main.c` | The helper file the exam gives you |
|
||
| `cheatsheet.md` | **The 8 moves + the 15 traps.** Read once, re-read after every attempt |
|
||
| `walkthrough.md` | Code block → line-by-line → **real captured behavior** for every station (with probes) |
|
||
| `exam-pack.md` | **The desert-island file:** exam subject + `main.c` verbatim, full stage plan, debug ladder, checklist, man-page excerpts |
|
||
| `diagrams.md` | 15 diagrams from scratch: processes, fd table, address, door, state, select loop, accept, pipeline, disconnect, ids, ownership, errors, session |
|
||
| `exam-answer/mini_serv.c` | The simplest passing implementation (proven 13/13 + Grademe) |
|
||
| `exam-answer/explained.md` | Every variable, function, expression and ownership rule in that file |
|
||
| `subject/main.annotated.c` | The exam's `main.c`, annotated: keep the helpers, rewrite `main()` |
|
||
| `exam-card.md` | One screen for the 5 minutes before you enter |
|
||
| `fundamentals.md` | What fds, sockets, TCP streams and `select()` really are — with runnable experiments |
|
||
| `libc-reference.md` | Every syscall/libc call used by `mini_serv`, individually: args, returns, errno, exam pattern, traps |
|
||
| `man-pages.md` | Curated offline man-page index + reading paths + "power moves" |
|
||
| `debugging.md` | Debugging toolkit (`gdb`/`valgrind`/`ss`), the symptom→cause table, a 60-second triage checklist |
|
||
| `explore/` | Four tiny toy programs that make the fundamentals visible |
|
||
| `drills/drills.md` | D1…D8 training program, scoring, 3-day crash plan |
|
||
| `templates/mini_serv.skeleton.c` | Fill-in skeleton (compiles, TODOs where the meat is) |
|
||
| `reference/mini_serv.c` | A known-good solution. **Only for diffing AFTER a timed attempt** |
|
||
| `reference/variant_friend.c` | Community variant (sets + one-event-per-wakeup style), validated by this gym's graders |
|
||
| `tester/mini_serv_tester.py` | Fast offline behavioral tester (13 checks, edge cases) |
|
||
| `tester/grademe/` | The **real** exam-style test suite (exact diff) |
|
||
| `tester/check.sh` | One command: strict compile + behavioral tester + real grademe diff |
|
||
| `sandbox/` | Your exam simulation. You write `sandbox/rendu/mini_serv/mini_serv.c` |
|
||
|
||
## Commands
|
||
|
||
```bash
|
||
# check your attempt (compile with -Wall -Wextra -Werror + behavior + real grademe diff):
|
||
bash tester/check.sh
|
||
|
||
# fast behavioral suite only (needs a compiled binary):
|
||
cc -Wall -Wextra -Werror sandbox/rendu/mini_serv/mini_serv.c -o sandbox/mini_serv
|
||
python3 tester/mini_serv_tester.py sandbox/mini_serv
|
||
|
||
# real grademe runner only:
|
||
cd sandbox && bash .system/grading/tester.sh
|
||
# pass -> sandbox/.system/grading/passed exists
|
||
# fail -> sandbox/traceback contains expected-vs-yours
|
||
|
||
# manual testing with netcat (3 terminals):
|
||
cc -Wall -Wextra -Werror sandbox/rendu/mini_serv/mini_serv.c -o sandbox/mini_serv
|
||
./sandbox/mini_serv 8081 # terminal 1
|
||
nc 127.0.0.1 8081 # terminal 2 (listener)
|
||
nc 127.0.0.1 8081 # terminal 3 (type "hello" + Enter; terminal 2 shows "client 1: hello")
|
||
# Ctrl-D in terminal 3 → terminal 2 shows "server: client 1 just left"
|
||
```
|
||
|
||
## The plan
|
||
|
||
### 7-day plan (default)
|
||
|
||
| Day | Work |
|
||
|---|---|
|
||
| 1 | Read the subject + `cheatsheet.md`. Drill **D1** (requirement recall), **D2** (boilerplate blast ×3), **D3** (buffer brains ×2) |
|
||
| 2 | **D4** (join/leave + ids), then **D5**: first full from-blank attempt under timer → `check.sh` → diff vs `reference/` |
|
||
| 3 | Fix the "what I missed" list. **D5 again**. Target: all green |
|
||
| 4 | **D6**: full attempt ≤ 90 min, all green. Then **D7** chaos scenarios |
|
||
| 5 | **D5/D6 again** — two clean runs in a row? |
|
||
| 6 | **D8**: exam sim — fresh dir, vim, 2 h, no notes, then grade yourself |
|
||
| 7 | Light repetition (D2/D3 from memory), read cheatsheet traps once, **rest**. |
|
||
|
||
### 3-day crash plan (exam in ≤3 days, ~2 h/day)
|
||
|
||
| Block | Work |
|
||
|---|---|
|
||
| Day 1 · 20 min | Read `subject/subject.en.txt` + cheatsheet "8 moves". Then D1 (write the ground truths from memory) |
|
||
| Day 1 · 80 min | `drills/typing-script.md` Stage A→F, live `nc` checkpoints. Guided reading allowed this first rep |
|
||
| Day 1 · 20 min | D2 rep 2 (blank) + Stage D case table from memory |
|
||
| Day 2 · 15 min | Warm-up: Stage C from blank, < 15 min |
|
||
| Day 2 · 75 min | D5: full attempt from blank, timer on. Then `check.sh` + diff vs `reference/` + write "what I missed" |
|
||
| Day 2 · 30 min | Fix the missed bits: re-drill only failing stages ×3 |
|
||
| Day 3 · 30 min | D5/D6 again from blank, target ≤ 60 min, all green |
|
||
| Day 3 · 30 min | D7 chaos (`check.sh`, then `tester/mini_serv_tester.py --quick` loops) |
|
||
| Day 3 · 45 min | D8-lite: fresh dir, only subject + `main.c` open, type the whole file, then grade |
|
||
| Day 3 · 15 min | Read cheatsheet traps once. Stop. Sleep. |
|
||
|
||
> If Day 3's cold run is not green, take the exam only if you must — a failed attempt is
|
||
> information, not the end. But if you can wait one more day and get one more green, wait.
|
||
|
||
### Readiness rule
|
||
|
||
> **Two consecutive full attempts written from a blank file, all green, under 2 h = ready.**
|
||
> Not "I understand it". Not "I did it once". **Twice, in a row, cold.**
|
||
|
||
## Rules of the gym
|
||
|
||
- During any timed attempt: no internet, no notes, no `reference/`, vim only — same as the exam.
|
||
- After the attempt: `check.sh` → then **diff your file against `reference/`** line by line and
|
||
write down every difference you missed (that list is your real study material).
|
||
- Never copy-paste during practice. Typing the patterns is the point.
|
||
- `reference/` is for post-attempt study and for keeping the test harness honest. It is not
|
||
an exam asset. Exam day you write your own code.
|
||
|
||
## Exam-day playbook
|
||
|
||
1. Read the subject **twice**. Copy the exact strings (`"Wrong number of arguments\n"`,
|
||
`"Fatal error\n"`, `"server: client %d just arrived\n"`, …) into your file as you go.
|
||
2. Found the `main.c` attachment? **Open it in a split / second tab.** Copy `str_join` +
|
||
`extract_message` verbatim — they're free wins.
|
||
3. Build in this order: args check → socket/bind/listen → fd_set loop → accept + arrivals →
|
||
recv + buffer + line extract + relay → disconnect + departures. Test with a second
|
||
terminal **after each layer** (`nc`), don't wait for the end.
|
||
4. Leave 30 min at the end for `nc` sanity + re-reading the subject + submission.
|
||
5. Submit early, submit again after fixes if the exam shell allows. A submitted partial
|
||
program beats a perfect unsent one.
|
||
6. Panic costs more than bugs. You have drilled this. Type what you drilled.
|