linux-sysprog · intermediate · ~30 min
Bit-masked event interpretation.
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'.)
poll() is the modern, portable cousin of select. Knowing how to translate the event flags is the inner loop of any event-driven server.
revents array of length n.
0/1 array.
O(n).
#define POLLIN 0x0001
#define POLLOUT 0x0004
#define POLLERR 0x0008
#define POLLHUP 0x0010
void poll_check_ready(short *revents, int n, int *out_ready) { /* TODO */ }
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).
All-zero revents -> all-zero out_ready. POLLOUT alone -> not ready by this rule.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.