linux-sysprog · intermediate · ~15 min

Lowest free file descriptor

Model the fd-allocation rule.

Challenge

The kernel hands out the lowest available fd number. Implement int lowest_free_fd(const int *open_fds, int n) returning the smallest non-negative integer not present in the array.

Starter code

int lowest_free_fd(const int *open_fds, int n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.