c - How to look for a change in a file without polling? -



c - How to look for a change in a file without polling? -

i have beagle bone , want read gpio pin program. file either contains 1 or 0 @ given moment. in c programme have while loop runs forever sleep function maintain beingness cpu pig whenever pin low(0) , when high(1) runs the code. sense wasteful of resources. there way can see when file 1 , run code? don't polling, when beagle bone going battery powered.

use notification mechanism, illustration utilizing libev.

the basic thought is, callback called, if info available.

if not work might need utilize other apis there, illustration inotify.

example courtesy of libev docs:

// single header file required #include <ev.h> #include <stdio.h> // puts // every watcher type has own typedef'd struct // name ev_type ev_io stdin_watcher; ev_timer timeout_watcher; // watcher callbacks have similar signature // callback called when info readable on stdin static void stdin_cb (ev_p_ ev_io *w, int revents) { puts ("stdin ready"); // one-shot events, 1 must manually stop watcher // corresponding stop function. ev_io_stop (ev_a_ w); // causes nested ev_run's stop iterating ev_break (ev_a_ evbreak_all); } // callback, time time-out static void timeout_cb (ev_p_ ev_timer *w, int revents) { puts ("timeout"); // causes innermost ev_run stop iterating ev_break (ev_a_ evbreak_one); } int main (void) { // utilize default event loop unless have special needs struct ev_loop *loop = ev_default; // initialise io watcher, start // 1 watch stdin become readable ev_io_init (&stdin_watcher, stdin_cb, /*stdin_fileno*/ 0, ev_read); ev_io_start (loop, &stdin_watcher); // initialise timer watcher, start // simple non-repeating 5.5 sec timeout ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); ev_timer_start (loop, &timeout_watcher); // wait events arrive ev_run (loop, 0); // break called, exit homecoming 0; }

c beagleboneblack

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -