Files
42-exam06/exam-card.md
T

2.8 KiB

EXAM CARD — mini_serv (one screen, read right before you start)

Exact strings (bytes matter, \n included)

write(2, "Wrong number of arguments\n", 26);  return 1;   /* argc != 2  */
write(2, "Fatal error\n", 12);                 exit(1);    /* syscall/alloc failed */
sprintf(m, "server: client %d just arrived\n", id);        /* to OTHERS       */
sprintf(o, "client %d: %s", id, line);                     /* per line, OTHERS */
sprintf(m, "server: client %d just left\n", id);           /* to everyone left */

127.0.0.1 ⇔ htonl(2130706433) · only localhost · no #define · no printf in the file.

The 8 moves

  1. argc != 2 → args error · 2. socket(AF_INET, SOCK_STREAM, 0) → fatal() ·
  2. sockaddr_in: bzero, AF_INET, htonl(2130706433), htons(atoi(argv[1])) → bind → listen ·
  3. slots init fd/id/buf = -1,-1,NULL, g_maxfd = g_server ·
  4. every loop: FD_ZERO + FD_SET(g_server) + FD_SET live fds → select(g_maxfd + 1, NULL, NULL, NULL) ·
  5. listener ready → accept → id = g_next_id++ → raise g_maxfd → "arrived" to others ·
  6. client ready → recv → n<=0 disconnect / else append str_join, while extract_message → sprintf("client %d: %s") → send to others → free(msg) ·
  7. disconnect → keep id, close, clear slot, free(buf), "left" to everyone, shrink g_maxfd (≥ server fd).

The 5 traps that fail exams

  1. Not rebuilding/copying the fd_set every loop (or wrong nfds) → newest client invisible / 100% CPU.
  2. Sending arrival or messages to the sender/newcomer → extra lines → diff fails.
  3. Flushing the partial buffer at disconnect → phantom message → diff fails.
  4. One line per recv instead of a while extraction loop → lines lost/delayed.
  5. Closing an fd still in the set → select fails EBADF; always FD_CLR/remove logically before close.

My commands

bash tester/check.sh                    # compile + 13 behavioral + real grademe diff
python3 tester/mini_serv_tester.py sandbox/mini_serv --quick   # fast loop
cd sandbox && bash .system/grading/tester.sh ; ls .system/grading/passed || sed -n '1,40p' traceback

Exam-day order (first 10 minutes)

  1. Read subject twice; copy the exact strings from it, not from memory.
  2. Open the main.c attachment; plan to copy str_join + extract_message verbatim.
  3. Type Stage A→E from drills/typing-script.md; test with nc after C and after D.
  4. nc sanity: 2 windows — arrival, relay, split line, Ctrl-C → left.
  5. Re-read the subject; submit; fix; resubmit if allowed. Never sit on a working copy.

If stuck (first three checks)

  1. ss -tlnp → LISTEN? 2. nc connects? 3. trace prints: which fd got ready, recv n, buffer contents. Symptom table: debugging.md §5. Exit codes: 141 = SIGPIPE, 139 = segv, 1 = your exit(1).