ev_io

Marc Lehmann schmorp at schmorp.de
Sat Dec 6 13:28:21 CET 2008


On Sat, Dec 06, 2008 at 02:33:02AM +0200, Graham Leggett <minfrin at sharp.fm> wrote:
> I set up a callback on a file that must run when the file is readable,  

A file is always readable.

Since a file is trivially always readable, most event mechanisms don't go to
the expense of actually generating events for files.

> 	ev_io_init(&t->io.io, watch_tail_reader_cb, t->handle, EV_READ);
> 	ev_io_start(t->s->e->loop, &t->io.io);
>
> The callback is called,
   
Sometimes, yes, but you cannot depend on it.

The solution is to simply read from the file, and not first try to use an
I/O watcher.

> and in the callback, the file is read, until I reach the end of the
> file, at which point my attempts to read return size 0, which I
> understand to mean EOF.

You cna verify your understanding by reading the documentation of "read"
or whatever else you are using to read, indeed.

> The problem is, despite the EOF point in the file having been reach,  
> socket remains readable, and the event still fires off in a tight loop,  
> eating up 100% CPU.

How did a socket come into play? You were talking about a file
earlier. Files are not sockets, so you have to decide what you are reading
from.

The event still fires at EOF, because you can still read from the socket
(you read the eof condition).

> I am trying to tail the file, so telling the event to stop watching the  
> file for reads means that I will miss any writes a third party makes to  
> the file.

You can either poll regularly, or use an ev_stat watcher.

> Am I understanding this correctly, or does a file report itself as  
> repeatedly readable when the pointer reaches the end of the file?

No, it doesn't report itself so, it simply *is* readable.

But keep in mind that files are always readable, and are not supported in
ev_io.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      pcg at goof.com
      -=====/_/_//_/\_,_/ /_/\_\



More information about the libev mailing list