eventfd

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.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s