27

I would like to know what the difference is between both i/o watchers inotify and epoll?

inotify

  • inotify_init(void) creates inotify instance to read events from
  • inotify_add_watch(int fd, const char * path, int mask) returns a watch fd around the file node behind the path
  • inotify_rm_watch(int fd, int wd) stops watching for events on fd

epoll

  • epoll_create(void) creates epoll object
  • epoll_ctl(int epfd, int op, int fd, struct epoll_event * event) sets up events to watch
  • epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout); blocks until event happens

So there seems to be a different approach on file watching. Inotify tries to let the user decide when to collect events while epoll blocks until something happens.

Is this correct? What are other differences?

ᄂ ᄀ
  • 5,214
  • 6
  • 38
  • 54
bodokaiser
  • 13,492
  • 20
  • 90
  • 133

1 Answers1

32

The biggest difference is that epoll can be used for ANY fd. This means it's good for watching all types of ways to communicate data. Sockets, IPC, files, printers.. anything. inotify is for filesystems only.

However, because inotify is specific to filesystems, you can receive notifications on a wide array of filesystem-specific attributes, such as file attributes and the file being read. These things are not possible via epoll.

In fact, inotify returns a file descriptor - which means you can use epoll to determine which inotify FD's you should call read on. So the two go hand in hand to some extent.

http://en.wikipedia.org/wiki/Inotify

xaxxon
  • 17,640
  • 4
  • 42
  • 70
  • thank you this answers my question. Byside epoll is linux only poll works everywhere (I think) – bodokaiser Jun 20 '13 at 07:47
  • 1
    I think it's cool that you can use epoll (or poll or select) to find out when to read any of the FDs created by inotify if you have multiple ones set up. I could see someone doing this to watch some base directory and then setting up conditional watches based on what changes in that base directory. – xaxxon Jun 20 '13 at 07:50
  • 1
    For reference, support for epoll has been added to FreeBSD's Linux compatibility layer as of version 11. ([ref](https://wiki.freebsd.org/WhatsNew/FreeBSD11#Other_changes)) – ghoti May 10 '16 at 15:44
  • 2
    That's not exactly correct as epoll doesn't actually work for file fd's inspite of that it potentially could. See "Epoll on regular files" for details – Sergey Mashkov Apr 10 '18 at 09:03