Questions tagged [epoll]

epoll is a Linux 2.6 readiness notification API for sockets, pipes, and special event-, signal-, and timer descriptors which can operate both in level- and edge-triggered mode, although presently only level-triggered behaviour is in accordance with the documentation. As opposed to poll or select, epoll scales O(1) in respect to the number of descriptors and O(N) in respect realized events.

The epoll API is built around 3 functions:

  • epoll_create creates a new epoll instance and returns a file descriptor that refers to it. This descriptor can be operated on with the other epoll functions and can be added to a different epoll instance
  • epoll_ctl allows file descriptors (sockets, pipes, eventfd, timerfd, signalfd, and epoll) being added and removed to an epoll's set of monitored descriptors, as well as flags of existing descriptors being modified
  • epoll_wait will return up to maxevents queued events. If no events are available, it will return zero. If a timeout is provided and no events are available, epoll_wait will block for the duration of the timeout (a value of -1 means forever).

The conceptual idea behind the API is that applications usually have a certain set of descriptors that changes rarely if ever, but which needs to be observed for readiness many times. Also, typically a lot fewer descriptors are ready than open. epoll therefore separates copying the list of descriptors to watch from the actual watching and notifies registered listeners instead of iterating a list of descriptors.

The operation of level-triggered mode (default) is easy, since it is identical of how poll/select works. As long as the resource is ready (e.g. as long as there remains data to be read), every call to epoll_wait will return an event.

The operation of edge-triggered mode (EPOLLET flag) is more complicated, more error-prone, inconsistenly documented, and inconsistently implemented. In epoll(7), it is explained in terms of reading partial data causing the next call to epoll_wait to block until new data arrives, but not while some data remains in the buffers. It is therefore recommended to use non-blocking descriptors and reading until EAGAIN is received.
According to The Linux Programming Interface, edge-triggered mode only reports events that happened since the last call to epoll_wait.
In reality, it does a mixture of both (i.e. both reads and epoll_wait reset the status to "not ready"), and it does not work as indicated in respect of several epoll instances listening to the same socket or several threads waiting on the same epoll instance (observed under kernel 2.6.38 with timerfd and eventfd). Although epoll is supposed to signal all waiters upon arrival of an event, in edge-triggered mode it only ever signals a single waiter.

792 questions
4
votes
1 answer

Determine which signal caused EINTR?

I am running an epoll loop and sometimes my call to epoll_wait returns -1 with errno set to EINTR. Sometimes, I want this to end the epoll loop, like in the case of SIGTERM or SIGINT. But I have this code compiled with the -pg flag, so periodic…
Brian Schlenker
  • 4,966
  • 6
  • 31
  • 44
4
votes
1 answer

can I register 2 or multiple events for the same socket descriptor on epoll?

for the same socket, I want to register two events: one is EPOLLIN and another is EPOLLOUT|EPOLLET. so for input, I want it to be level trigger and for output I want it to be edge trigger. ev.data.fd=fd; ev.events=EPOLLIN; …
misteryes
  • 2,167
  • 4
  • 32
  • 58
4
votes
2 answers

How to detect empty epoll set

I'm learning to use epoll, and I wrote the following example #include #include #include #include #include #include #include int main() { int epfd; struct…
Guido
  • 2,571
  • 25
  • 37
4
votes
1 answer

Find what descriptors are registered within an epoll instance

I'm wondering whether there is a way to find out what descriptors (and the expected events) are registered for a particular epoll instance using gdb or some other inspection tool? It's fairly easy to find it out when poll or select is used since all…
Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
3
votes
1 answer

Why epoll_wait(), get returned and TCP connect time out in 5s~10s

I create a non-blocking socket, and then use the epoll to manange the socket. I do not set a timeout on epoll_wait. But I find that the epoll returned in 5s to 10s, then I check with the getsockopt on SO_ERROR, the connect find to be timed out. Why…
hyman
  • 43
  • 1
  • 5
3
votes
3 answers

Why does epoll_wait only provide huge 1ms timeout?

epoll_wait, select and poll functions all provide a timeout. However with epoll, it's at a large resolution of 1ms. Select & ppoll are the only one providing sub-millisecond timeout. That would mean doing other things at 1ms intervals at best. I…
hookenz
  • 36,432
  • 45
  • 177
  • 286
3
votes
1 answer

epoll vs select for very small number of connections

I have been using select to handle connections, recently there was a change an our socket library and select was replaced by epoll for linux platform. my application architecture is such that I make only one or at max 2 socket connections and…
user424060
  • 1,545
  • 3
  • 20
  • 29
3
votes
0 answers

My event poll model crashes when using syscall.EpollEvent

in syscall/ztypes_linux_amd64.go type EpollEvent struct { Events uint32 Fd …
alfiver
  • 31
  • 1
3
votes
1 answer

GOLANG: Why does SetDeadline/SetReadDeadline/SetWriteDeadline not work on a file when using os.File.Fd()?

I am using a combination of os.File.SetReadDeadline and os.File.ReadFull. But even if using SetReadDeadline, the deadline I have set is completely ignored and ReadFull blocks forever. Why is that? Additional info: I fire some IOCTLS towards the file…
Xerusial
  • 525
  • 1
  • 5
  • 17
3
votes
1 answer

Is it necessary to remove all file descriptors in the interest list before closing the epoll instance itself?

Provided that: I have created an epoll instance epfd by epoll_create, and registered many regular file descriptors by EPOLL_CTL_ADD. I want to close the epoll instance by close(epfd) The manual page does't say whether I must EPOLL_CTL_DEL all file…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
3
votes
4 answers

How to rebuild epoll package in electron?

I try to rebuild an electron app but I got this error regarding the epoll installation. Building module: epoll, Completed: 0gyp: name 'openssl_fips' is not defined while evaluating condition 'openssl_fips != ""' in binding.gyp while trying to load…
FaFa
  • 358
  • 2
  • 16
3
votes
1 answer

Will an epoll be woken up on EPOLLERR/EPOLLHUP if no other flags are provided?

I want to add a file descriptor to an existing epoll instance but not have it wake up yet. Specifically, I would like to make sure that EPOLLERR and EPOLLHUP are not raised. I am using EPOLLONESHOT to wake up a single thread at a time, then…
ddulaney
  • 843
  • 7
  • 19
3
votes
3 answers

How do I implement epoll timeout?

I am working on a network programming using epoll. It seems like working fine. I would like to add a timeout function so if a client doesn't sent anything for a long period time then it will just disconnect the client. How do I do this? I know that…
codereviewanskquestions
  • 13,460
  • 29
  • 98
  • 167
3
votes
1 answer

Java NIO why Selector has to work with Channel in non-blocking mode?

According to this passage, IO Multiplexing can work with file descriptors in both blocking and non-blocking mode: And non-blocking mode is only needed to cooperate with edge-triggered mode: What's more, according to this answer, Java NIO works in…
folkboat
  • 167
  • 1
  • 7
3
votes
1 answer

Why does netty provide EpollEventLoopGroup?

After my test, there is no difference in performance between Netty's NioEventLoopGroup and EpollEventLoopGroup. Why does Netty also provide EpollEventLoopGroup? When the server maintains 1000 TCP links, there is no difference between CPU and memory…