devdev
devdev

Reputation: 211

linux, inotify - how to subscribe?

Can you please explain me the inotify mechanism? I’ve searched in the web about it, but what I understood is that if I want to see the changes in a file I have to do polling with the “read” function. Is there any callback function which will notify me when the file has a change without polling?

Thanks,

Upvotes: 5

Views: 1417

Answers (4)

Tigerbot
Tigerbot

Reputation: 11

You can use the ev_io part of the libev library. This will allow you to set a callback for when the inotify fd is readable.

The documentation for libev can be found here: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod

Upvotes: 1

Duck
Duck

Reputation: 27542

The inotify calls employ file descriptors. Rather than polling you can use the returned FDs in the select() family of calls. It's not exactly a callback function but reduces the overhead you are concerned with.

Upvotes: 1

bew
bew

Reputation: 641

I'm speculating that you are using some GUI library that handle events for you.

The better GUI libraries have a way to watch file descriptors. In GTK, it is gtk_input_add_full; in Qt, QSocketNotifier might work for you.

Upvotes: 2

BЈовић
BЈовић

Reputation: 64203

No, inotify requires polling.

Registering a callback using signal (as suggested), is IMO worse then doing regular polling.

Upvotes: 0

Related Questions