94 lines
3.1 KiB
Markdown
94 lines
3.1 KiB
Markdown
# Explore — four tiny programs to make the fundamentals visible
|
|
|
|
These are **learning toys**, not exam material. Run them once, understand what you saw,
|
|
then forget the code (the exam is `mini_serv` only).
|
|
|
|
```bash
|
|
cd explore
|
|
for n in 01_fds 02_bind_listen 03_accept_recv 04_select_two; do
|
|
cc -Wall -Wextra -Werror $n.c -o $n && echo "$n built"
|
|
done
|
|
```
|
|
|
|
Open two or three terminals for the experiments that need `nc`.
|
|
|
|
---
|
|
|
|
## 01 — fds are small integers, reused
|
|
|
|
```bash
|
|
./01_fds
|
|
```
|
|
|
|
**Look for:** `stdin=0 stdout=1 stderr=2`, then a file and a socket taking `3` and `4`, then
|
|
the same numbers coming back after `close()`. This is the *whole* idea behind "the array
|
|
index is the fd".
|
|
|
|
## 02 — listen without accept: connections queue in the kernel
|
|
|
|
```bash
|
|
./02_bind_listen 8081 # terminal 1
|
|
|
|
# terminal 2:
|
|
ss -tlnp | grep 8081 # LISTEN, with backlog
|
|
nc -v 127.0.0.1 8081 # says "succeeded"!
|
|
ss -tnp | grep 8081 # ESTABLISHED — sitting in the accept queue
|
|
```
|
|
|
|
**Look for:** `nc` succeeding even though the program never called `accept()`. Then `Ctrl-C`
|
|
the server and run `nc -v` again → **Connection refused** (nobody listening). That pair of
|
|
observations covers 90% of "why does nc say X" questions.
|
|
|
|
## 03 — accept returns a NEW fd; recv gives chunks, not messages
|
|
|
|
```bash
|
|
./03_accept_recv 8081 # terminal 1
|
|
|
|
# terminal 2 (interactive):
|
|
nc 127.0.0.1 8081 # type slowly, then fast; press Enter; watch chunks
|
|
|
|
# terminal 2 (scripted split line, same connection):
|
|
send() { exec 3<>/dev/tcp/127.0.0.1/"$1"; cat >&3; exec 3>&-; }
|
|
{ printf 'par'; sleep 1; printf 'tial\n'; } | send 8081
|
|
```
|
|
|
|
**Look for:** `listening fd = 3, conversation fd = 4`; the same line arriving in 1, 2 or 3
|
|
`recv` events; and finally `recv returned 0` when the client goes away (EOF).
|
|
|
|
## 04 — select() = "wait for any of these fds"
|
|
|
|
```bash
|
|
./04_select_two 8081 # terminal 1
|
|
|
|
# type in terminal 1 -> stdin ready
|
|
# nc 127.0.0.1 8081 -> listener ready, accepted, one line read
|
|
# Ctrl-D in terminal 1 -> stdin EOF, exits
|
|
```
|
|
|
|
**Look for:** one `select()` call being woken by *two completely different sources*. That's
|
|
exactly how the exam server juggles the listening socket and every client at once.
|
|
|
|
---
|
|
|
|
## 05 — `fd_set` is a bitmap, and `select` rewrites it
|
|
|
|
```bash
|
|
cc -Wall -Wextra -Werror 05_fdset.c -o 05_fdset
|
|
./05_fdset
|
|
```
|
|
|
|
**Look for:** `FD_SETSIZE = 1024`; the 16-bit picture changing as you `FD_SET`/`FD_CLR`;
|
|
then two real pipes (one with a byte, one empty) going into a set — after `select`, **only
|
|
the readable pipe's bit survives**. That last line is the reason mini_serv rebuilds or copies
|
|
its sets every loop. (Bonus: marking a bit for an fd that isn't open makes `select` fail with
|
|
`EBADF` — bits are not fds.)
|
|
|
|
---
|
|
|
|
## After each experiment, write one sentence
|
|
|
|
What did I *see* that I didn't believe before? (e.g. "the connection exists before accept",
|
|
"one line can arrive in pieces", "fds get reused"). Those sentences are worth more than the
|
|
code. Then go back to `fundamentals.md` §4–§8 and read the matching section — it will feel
|
|
obvious now.
|