I haven’t heard about the eventfd feature in Linux until I came across it in our code base and began to wonder what it is. This article about eventfd gives some good introduction about it. In a gyst:
- This is similar to pipe that two related processes can signal to each other and synchronize themselves.
- Unlike pipe(), this has only one file descriptor. One can do both read() and write() on the file descriptor.
- As a bonus, it can also act like a mutex/semaphore. When one read()s the fd, the value of the eventfd object becomes zero. Any subsequent read()s blocks the reader until some writer comes along and write()s some value in it. If the value being written/read is 1, it is a mutex, otherwise it is a semaphore.
- One advantage of using this as a mutex/semaphore is that there is no explicit need for locking.
- pipe() is a bit confusing with two file descriptors – one process has to close one end and the other process has to close other end etc. eventfd is pretty simple.
- Though pipe() can be used to transfer data between processes, but eventfd() cannot be used for that. In a sense, pipe() should be left for piping data and eventfd should be kept for signalling purposes. Separation of functions.