linux-sysprog · intermediate · ~30 min

Map poll() events to per-fd readiness flags

Bit-masked event interpretation.

Challenge

Given parallel arrays events[n] and revents[n], populate out_ready[n] such that out_ready[i] = 1 iff revents[i] & (POLLIN | POLLERR | POLLHUP) != 0, else 0. Use these constants:

#define POLLIN  0x0001
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010

Implement void poll_check_ready(short *revents, int n, int *out_ready). (events is unused for this exercise — we only care which revents bits indicate the fd is 'something happened'.)

Why this matters

poll() is the modern, portable cousin of select. Knowing how to translate the event flags is the inner loop of any event-driven server.

Input format

revents array of length n.

Output format

0/1 array.

Constraints

O(n).

Starter code

#define POLLIN  0x0001
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
void poll_check_ready(short *revents, int n, int *out_ready) { /* TODO */ }

Common mistakes

Treating POLLOUT as a 'something happened' bit (it is, but for write-readiness, not read-side); using == instead of & for the bit check; missing POLLHUP (a closed peer often only signals via POLLHUP).

Edge cases to handle

All-zero revents -> all-zero out_ready. POLLOUT alone -> not ready by this rule.

Complexity

O(n).

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