0Pricing
C Academy · Lesson

Setting Up epoll

Create and register an epoll set.

Creating an epoll Instance

Everything starts with an epoll instance, itself a file descriptor. Create one with epoll_create1().

The argument is a flags field; pass 0 for default behavior or EPOLL_CLOEXEC to close the descriptor automatically across exec(). The returned fd is what you pass to every other epoll call.

int epfd = epoll_create1(EPOLL_CLOEXEC);
if (epfd == -1) { perror("epoll_create1"); exit(1); }

epoll_create1 vs epoll_create

The older epoll_create(int size) took a size hint, but the kernel ignores it now; the only requirement was that it be positive.

Prefer epoll_create1(): it takes flags instead of a meaningless size and lets you request EPOLL_CLOEXEC atomically, avoiding a separate fcntl() call.

/* legacy form, size arg ignored by kernel */
int epfd = epoll_create(1);

All lessons in this course

  1. Blocking vs Non-Blocking I/O
  2. Setting Up epoll
  3. The Event Loop
  4. A Simple Echo Server
← Back to C Academy