networking · intermediate · ~15 min
The dispatch-loop pattern; mask matching for the 'readable' flags.
Define the bit constants and a struct just like epoll's:
#define EPOLLIN 0x001
#define EPOLLOUT 0x004
#define EPOLLERR 0x008
#define EPOLLHUP 0x010
typedef struct { int events; int fd; } evt_t;
Implement int filter_readable(const evt_t *in, int n, int *out_fds, int max).
The function copies into out_fds[] every in[i].fd whose in[i].events has
EPOLLIN OR EPOLLHUP OR EPOLLERR set (these are the three flags that
indicate "you should read"). Returns the count copied (<= max).
When epoll_wait fills your events[] buffer, you walk it once and dispatch each fd. Getting the dispatch loop right — including masks for HUP/ERR — is the difference between a working server and a stuck one.
Array of {events, fd}.
Count + filled out_fds.
Single pass; respect max.
#define EPOLLIN 0x001
#define EPOLLOUT 0x004
#define EPOLLERR 0x008
#define EPOLLHUP 0x010
typedef struct { int events; int fd; } evt_t;
int filter_readable(const evt_t *in, int n, int *out_fds, int max) { /* TODO */ return 0; }
Treating only EPOLLIN as 'readable' — HUP and ERR also need attention.
Empty input, all-set, none-set, max smaller than matches.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.