networking · beginner · ~15 min
Test a readiness bit across fds.
poll() reports readiness as a bitmask per fd. Check whether any fd is readable by testing the POLLIN bit across the array.
Implement int any_readable(const int *revents, int n, int pollin) that returns 1 if any of the first n entries has the pollin bit set (revents[i] & pollin), otherwise 0.
revents: array of per-fd result masks.n: number of entries.pollin: the POLLIN bit mask to test against.Returns 1 if at least one fd has the bit set, else 0.
any_readable({0,4,0}, 3, 4) -> 1
any_readable({2,2}, 2, 4) -> 0
pollin) do not count.An int array revents, its length n, and the POLLIN bit mask pollin.
1 if any revents[i] & pollin is non-zero, else 0.
Test the specific pollin bit with bitwise AND, not the whole value.
int any_readable(const int *revents, int n, int pollin) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.