Not to terminate ev_loop

Gabriel Kerneis kerneis at pps.jussieu.fr
Sun Jan 8 09:36:59 CET 2012


On Sun, Jan 08, 2012 at 11:51:51AM +0400, Sandeep wrote:
> Could you please help me how to modify this in such a way that, call
> "stdin_cb" everytime there is input (but not simply in infinite loop), if
> there is no input just keep waiting without exiting.
> 
> static void
> stdin_cb (EV_P_ struct ev_io *w, int revents)
>   {
>     puts ("stdin ready");
>   }

The infinite-loop issue stems from the fact that you do not actually read
what the user entered on stdin, so that data stays available, and stdin_cb
keeps being triggered.  You have to "flush" stdin to make clear to the kernel
that you got what the user entered and react to new input:

    char buf[SIZE];
    while(read(0, buf, SIZE) > 0);

You might also want to google "edge-triggered" vs. "level-triggered" (or read
the epoll(7) man page about it).

Best,
-- 
Gabriel



More information about the libev mailing list