core: add all the required files

This commit is contained in:
mkurkar
2026-11-04 16:42:12 +03:00 Unverified
commit 29324ac2ea
57 changed files with 57688 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
# Drills — the actual training
Every drill is written **from a blank file, by hand, no copy-paste**. Timer on. Report card
at the bottom. `R = red` (failed / looked) · `Y = yellow` (done but slow) · `G = green` (clean, on time).
---
## D1 — Requirement recall · 20 min · day 1
Close everything (including this file). Write the 11 ground-truth behaviors of the exam from
memory (start from the README list mentally; do NOT open it). Then compare with README
"ground truth". Every line you missed → mark it on the traps list in `cheatsheet.md`.
**Green = all 11, in order, no peeking.**
## D1.5 — Fundamentals sprint · 45 min · day 1 (optional, very recommended)
Read `fundamentals.md` §1–§8 while running the four experiments in `explore/` (they make
fds, backlog, chunking and `select` visible). Then read §10 — the "how to test each part of
your code" table — and keep `debugging.md` §5 (symptom table) open from now on.
Timebox: 45 minutes. If you're behind schedule, do **§4, §5, §7 + experiments 02, 03** only.
## D2 — Boilerplate blast · 15 min ×3 · day 1
Blank file. Type, without notes:
- includes, `t_client` struct, globals (`g_c`, `g_maxfd`, `g_server`, `g_next_id`), `fatal()`
- `main`: argc guard with the exact string, socket/bind/listen with `htonl(2130706433)`
- init all slots, then the loop: rebuild `fd_set` → `select` → `accept`-only handling
Compile clean: `cc -Wall -Wextra -Werror file.c -o test`. It doesn't have to *work* yet — just be
typed from memory. Repeat 3 times; target the last one **under 8 minutes**.
**Green = third run < 8 min, compiles clean, zero peeks.**
## D3 — Buffer brains · 10 min ×2 · day 1
Blank file. Type `str_join` + `extract_message` from `subject/main.c` (they're given in the exam —
this is fluency training). Then, from memory, type `handle_read()` that uses them:
`recv` → append → extract loop → `sprintf("client %d: %s")` → free.
Then a paper check on these cases — what is buffered, what is relayed?
| input chunks (in order) | relayed lines |
|------------------------------|----------------|
| `"ab\ncd\n"` | ? |
| `"ab"` then `"cd\n"` | ? |
| `"a\nb"` then `"c\n"` | ? |
| `"lonely"` then disconnect | ? |
**Green = code compiles + all four cases answered correctly.**
## D4 — Arrivals & departures · 25 min · day 2
Take D2's file and complete it: `accept_new` (ids, maxfd, "just arrived" to others only),
`disconnect` (left message, slot cleanup, maxfd shrink), `to_all_except`.
Test with two `nc` windows: connect/leave order, ids 0,1,2…, newcomer receives nothing on connect.
**Green = all of the above verified with `nc`.**
## D5 — Full attempt #1 · 60–90 min · day 2+
Blank file. Timer. No internet, no notes, vim only. Write the whole program. Then:
```bash
bash tester/check.sh
```
Then diff against `reference/mini_serv.c` and write your **"what I missed"** list (function by
function, be honest). That list is your study plan for the next session.
**Green = check.sh fully green.**
## D6 — Full attempt #2, faster · ≤90 min · day 3+
Same as D5 but the target is **≤ 90 min, all green**. If you red anything, do the smallest
targeted drill for the weak part and run D6 again the same day.
**Green = ≤90 min, all green.**
## D7 — Chaos hour · 30 min · day 4
Make the tester try to break you:
```bash
python3 tester/mini_serv_tester.py sandbox/mini_serv # everything
python3 tester/mini_serv_tester.py sandbox/mini_serv --quick # quick loop
```
Add your own abuse: two servers on one port (→ `Fatal error`), a client that sends 300 KB and
disconnects mid-line, 20 clients at once, `printf`-free stderr. Read every failure message
slowly — those messages are written the way the real grader thinks.
**Green = all 13 tests pass and you can explain any red to yourself.**
## D8 — Exam simulation · 2 h · day 5+
Fresh empty directory (`mkdir /tmp/opencode/examsim && cd /tmp/opencode/examsim`). Simulate
everything: only the subject (`subject/subject.en.txt`) + `subject/main.c` open in another tab
(like the exam attachment), vim, 2 hours, no other references. When done, copy your file to
`sandbox/rendu/mini_serv/mini_serv.c` and run `bash tester/check.sh`.
**Green = all green within the 2 h.** Two consecutive D8 greens = **you're ready**.
---
## Report card
| Drill | Run 1 | Run 2 | Run 3 | Notes |
|---|---|---|---|---|
| D1 | | | | |
| D2 | | | | |
| D3 | | | | |
| D4 | | | | |
| D5 | | | | |
| D6 | | | | |
| D7 | | | | |
| D8 | | | | |
## Readiness rule (repeat until true)
> Two consecutive full attempts **from a blank file, under 2 h, all green** = ready.
> Then stop drilling the full thing; do D2/D3 once a day to stay warm until exam day.
+133
View File
@@ -0,0 +1,133 @@
# Typing script — reconstruct `mini_serv` cold, stage by stage
Your situation: exam in ~3 days, never wrote a socket server, 1–2 h/day.
So the strategy is **not** "invent it" and **not** "understand every syscall".
It is: **replay this exact typing order** until your hands know it. After every stage
there is a checkpoint. If a checkpoint fails, fix that stage before moving on.
> Time budget per stage is for your *third* rep. First rep: double it. Don't rush the first one.
```mermaid
flowchart LR
A["A: top of file<br/>checkpoint: compiles clean"] --> B["B: guard + socket/bind/listen<br/>checkpoint: args error + LISTEN in ss"]
B --> C["C: loop + accept + broadcast<br/>checkpoint: two nc windows see 'just arrived'"]
C --> D["D: recv + buffer + relay<br/>checkpoint: hello relays; split line stays silent"]
D --> E["E: disconnect<br/>checkpoint: Ctrl-D gives 'just left'; partial dropped"]
E --> F["F: bash tester/check.sh<br/>checkpoint: 13/13 + GRADEME PASSED"]
```
---
## Stage 0 — setup (1 min)
- In the exam: you write in the `rendu` directory the shell shows you (ask the shell).
- Here: create `sandbox/rendu/mini_serv/mini_serv.c`.
- Open `subject/subject.en.txt` and `subject/main.c` in another tab — **in the exam you have both**, use them.
## Stage A — top of file (10 min)
Type, in this order:
1. the 7 includes (`stdlib, string, unistd, stdio, sys/socket, netinet/in, sys/select`)
2. the struct:
```c
typedef struct s_client { int fd; int id; char *buf; } t_client;
```
3. the 4 globals: `g_c[FD_SETSIZE]`, `g_maxfd`, `g_server`, `g_next_id`
4. `fatal()`:
```c
write(2, "Fatal error\n", 12); exit(1);
```
**Checkpoint A:** `cc -Wall -Wextra -Werror mini_serv.c -o mini_serv` → no output, no errors.
(If the compiler lists an unused something: that's Stage A material, not exam material — fix it now.)
## Stage B — main(), part 1: guard + socket (15 min)
1. `if (argc != 2)` → `write(2, "Wrong number of arguments\n", 26);` → `return (1);`
2. `g_server = socket(AF_INET, SOCK_STREAM, 0);` + `if (< 0) fatal();`
3. `struct sockaddr_in addr;` → `bzero(&addr, sizeof(addr));`
4. `addr.sin_family = AF_INET;`
5. `addr.sin_addr.s_addr = htonl(2130706433);` ← **the 127.0.0.1 magic number. Never forget it.**
6. `addr.sin_port = htons(atoi(argv[1]));`
7. `if (bind(...) < 0) fatal();`
8. `if (listen(g_server, 10) < 0) fatal();`
**Checkpoint B:** compiles. `./mini_serv` → prints `Wrong number of arguments` on stderr, exit 1.
`echo $?` shows 1.
## Stage C — main(), part 2: slots + loop + accept (25 min)
1. init loop: all `g_c[i].fd = -1; id = -1; buf = NULL;` then `g_maxfd = g_server;`
2. `while (1) {` and inside, **in this exact order**:
- `FD_ZERO(&readfds);`
- `FD_SET(g_server, &readfds);`
- loop `i = 0..g_maxfd`: if slot live → `FD_SET(i, &readfds);`
- `if (select(g_maxfd + 1, &readfds, NULL, NULL, NULL) < 0) continue ;`
- `if (FD_ISSET(g_server, &readfds)) accept_new();`
- loop `i = 0..g_maxfd`: if `i != g_server && slot live && FD_ISSET(i, &readfds)` → `handle_read(i);`
3. `accept_new()` (write it now, above main):
```c
fd = accept(g_server, NULL, NULL); if (fd < 0) return ;
g_c[fd].fd = fd; g_c[fd].id = g_next_id++; g_c[fd].buf = NULL;
if (fd > g_maxfd) g_maxfd = fd;
sprintf(msg, "server: client %d just arrived\n", g_c[fd].id);
to_all_except(fd, msg); /* NOT to the newcomer */
```
4. `to_all_except(fd, s)`: loop `i = 0..g_maxfd`, `if (i != fd && g_c[i].fd != -1) send(g_c[i].fd, s, strlen(s), 0);`
**Checkpoint C:** compiles. Then live test: `./mini_serv 8081` in terminal 1,
`nc 127.0.0.1 8081` in terminal 2 (nothing appears), open terminal 3 with `nc 127.0.0.1 8081`
→ **terminal 2 must show `server: client 1 just arrived`.**
(Client ids: viewer is 0, newcomer is 1 — and the newcomer sees nothing for itself. That's correct!)
## Stage D — the read path (30 min, the exam lives here)
1. `handle_read(fd)`:
```c
n = recv(fd, tmp, sizeof(tmp) - 1, 0);
if (n <= 0) { disconnect(fd); return ; }
tmp[n] = 0;
g_c[fd].buf = str_join(g_c[fd].buf, tmp); /* append FIRST */
if (!g_c[fd].buf) fatal();
while (extract_message(&g_c[fd].buf, &msg) == 1) /* then pull lines */
{
out = malloc(strlen(msg) + 64);
if (!out) { free(msg); fatal(); }
sprintf(out, "client %d: %s", g_c[fd].id, msg);
to_all_except(fd, out); /* NOT to self */
free(out); free(msg);
}
```
2. `str_join` + `extract_message`: **copy verbatim from `subject/main.c`.** You are allowed to.
Type them once by hand tonight (D3), then you'll never think about them again.
**Checkpoint D:** terminal 3 types `hello` + Enter → **terminal 2 shows `client 1: hello`.**
Terminal 3 itself shows nothing. Partial-line test: type `par` Enter? No — type `par` and
*don't* press Enter yet; terminal 2 must stay silent until you press Enter.
## Stage E — disconnect (10 min)
`disconnect(fd)`: save `id` → `close(fd)` → slot `fd = -1; id = -1` → `free(buf); buf = NULL;`
→ `sprintf(msg, "server: client %d just left\n", id);` → `to_all_except(-1, msg);`
→ shrink: `while (g_maxfd > g_server && g_c[g_maxfd].fd == -1) g_maxfd--;`
**Checkpoint E:** Ctrl-D in terminal 3 → terminal 2 shows `server: client 1 just left`.
Retype an incomplete line and Ctrl-D → **no message** with it, only `left` (buffer dropped).
## Stage F — grade yourself (5 min)
From the repo root: `bash tester/check.sh` → 13/13 + `GRADEME: PASSED`.
---
## Pace targets
| Rep | Goal | Time |
|---|---|---|
| 1st (today, guided allowed) | compiles, checkpoints C–E work with `nc` | 60–90 min |
| 2nd (blank, cheatsheet only on failure) | `bash tester/check.sh` all green | ≤ 60 min |
| 3rd (blank, exam conditions) | all green, no references at all | ≤ 45 min |
**When any stage fails, re-drill *only that stage* 3× before the next full rep.**
Partial-line handling (Stage D) is where 80% of candidates fail — over-drill it.