networking · beginner · ~15 min

Any readable fd?

Test a readiness bit across fds.

Challenge

poll() reports readiness as a bitmask per fd. Check whether any fd is readable by testing the POLLIN bit across the array.

Task

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.

Input

  • revents: array of per-fd result masks.
  • n: number of entries.
  • pollin: the POLLIN bit mask to test against.

Output

Returns 1 if at least one fd has the bit set, else 0.

Example

any_readable({0,4,0}, 3, 4)   ->   1
any_readable({2,2},   2, 4)   ->   0

Edge cases

  • Other bits set (without pollin) do not count.

Input format

An int array revents, its length n, and the POLLIN bit mask pollin.

Output format

1 if any revents[i] & pollin is non-zero, else 0.

Constraints

Test the specific pollin bit with bitwise AND, not the whole value.

Starter code

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.