networking · beginner · ~15 min

Count ready descriptors

Scan poll results for ready fds.

Challenge

After a poll() call, each watched fd has a revents value where 0 means "nothing happened". Count how many fds are ready.

Task

Implement int count_ready(const int *revents, int n) that returns how many of the first n entries in revents are non-zero.

Input

  • revents: array of per-fd result masks.
  • n: number of entries.

Output

Returns the count of non-zero entries.

Example

count_ready({0,1,0,4,0}, 5)   ->   2
count_ready({0,0}, 2)         ->   0

Edge cases

  • All-zero array gives 0.

Input format

An int array revents and its length n.

Output format

The number of non-zero entries in revents[0..n-1].

Constraints

A non-zero revents value means that fd is ready.

Starter code

int count_ready(const int *revents, int n) {
    /* TODO */
    return 0;
}

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