Practice ยท 2 of 2
The Cleanup Ladder
Implement a multi-resource pipeline with the cleanup-goto
pattern. The boilerplate declares a tiny fake-resource stack:
``c
/* resources must be acquired in order and released in REVERSE order;
the tracker records acquire/release for verification */
void tr_reset(void);
int tr_acquire(int id); /* 0 ok; -1 if id already held */
void tr_release(int id); /* no-op if not held */
int tr_holds(int id);
/* your function: acquire 1, 2, 3; fail if any acquire fails;
"work" = return 0; release in reverse order โ ALWAYS, on every path */
int pipeline(int fail_at); /* 0 = no failure; 1..3 = fail that acquire */
/* the tracker's record of events, as a string like "a1a2a3r3r2r1" */
const char *tr_log(void);
``
pipeline(fail_at) must leave NO resources held โ whatever happens.
Difficulty: intermediate
Back to lesson: Practice: Error Contract Gym