linux-sysprog · beginner · ~15 min
Walk a NULL-terminated pointer array.
The exec family receives arguments as a NULL-terminated array of string pointers (argv). Count how many arguments it holds.
Implement int argv_len(const char *const *argv) that returns the number of entries before the terminating NULL pointer.
argv: array of char *, terminated by a NULL pointer.The count of non-NULL entries.
argv_len({"ls", "-l", "/tmp", NULL}) -> 3
argv_len({NULL}) -> 0
argv: NULL-terminated array of string pointers.
Number of entries before the NULL terminator.
The array is always NULL-terminated.
#include <stddef.h>
int argv_len(const char *const *argv) {
/* TODO */
return 0;
}
Creating zombie processes by failing to reap child with wait/waitpid; forgetting that code after successful execvp() is never reached.
Fork failure (EAGAIN); child process crashing with signal; parent terminating before child; waitpid() with WNOHANG on running child.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.