From owner-freebsd-hackers@FreeBSD.ORG Sun Jun 15 20:14:16 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2C0B737B401 for ; Sun, 15 Jun 2003 20:14:16 -0700 (PDT) Received: from adsl-64-161-78-226.dsl.lsan03.pacbell.net (adsl-64-161-78-226.dsl.lsan03.pacbell.net [64.161.78.226]) by mx1.FreeBSD.org (Postfix) with SMTP id 644D543F3F for ; Sun, 15 Jun 2003 20:14:15 -0700 (PDT) (envelope-from oremanj@adsl-64-161-78-226.dsl.lsan03.pacbell.net) Received: (qmail 9048 invoked by uid 1001); 16 Jun 2003 03:16:35 -0000 Date: Sun, 15 Jun 2003 20:16:35 -0700 From: Joshua Oreman To: Matthew Hagerty Message-ID: <20030616031635.GA8974@webserver.get-linux.org> References: <3EECB43F.8010609@math.missouri.edu> <1183.10.0.81.10.1055728224.squirrel@www.mundomateo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1183.10.0.81.10.1055728224.squirrel@www.mundomateo.com> User-Agent: Mutt/1.4.1i cc: hackers@freebsd.org Subject: Re: kqueue alternative? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Jun 2003 03:14:16 -0000 On Sun, Jun 15, 2003 at 09:50:24PM -0400 or thereabouts, Matthew Hagerty seemed to write: > > Joshua Oreman wrote: > >> On Sun, 15 Jun 2003, Matthew Hagerty wrote: > >> > >>>I'm writing a little application that needs to watch a file that another > >>>process is writing to, think 'tail -F'. kqueue and kevent are going to > >>>do it for me on *BSD, but I'm also trying to support *cough* linux and > >>>other UN*X types OSes. > >>> > >>>>From what I can find on google, the linux community seems very opposed > >>>to kqueue and has not yet implemented it (they say: blah blah blah, > >>>aio_*, blah blah balh.) What alternatives do I have with OSes that > >>>don't support kqueue? I'd really hate to poll with stat(), but do I > >>>have any other choices? > >> > >> > >> I would say, use select(2). > >> Is there a reason this wouldn't work? > >> > >> -- Josh > > > > > > Either select(2) or poll(2) should work. > > > > > > > > -- > > Stephen Montgomery-Smith > > stephen@math.missouri.edu > > http://www.math.missouri.edu/~stephen > > > > Last time I did a test, it seemed that I could not read to the end of a > file that another process was writing to, without calling stat() to get > the real size? > > So, I open the file for reading, and read/process the data until EOF, at > which point I... ? call select()? What should I be watching for if > another process is doing the writing? Will my select() return if I'm > watching for a ready to read condition while another process is writing? > > While writing this response I'm thinking of a function I didn't know about > that I saw in forward.c (part of the tail command's code), clearerr(). If > I hit EOF, and another process writes to the file, will calling clearerr() > on my file pointer allow me to continue reading the new data that was just > written by the other process? Am I making sense? Are you using (FILE *) file pointers or (int) file descriptors? I recommend you use file pointers for this program; easier line input. However, select() needs a file descriptor, which you can obtain with fileno(my_file_pointer). Here's some pseudocode (assuming you want to print the data): FILE *file; char buffer[101]; fd_set readfds; file = fopen ("/path/to/my/file"); while (fgets (buffer, 100, file)) fputs (buffer, stdout); clearerr (file); FD_ZERO (&readfds); FD_SET (fileno (file), &readfds); while (1) { select (fileno (file), &readfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0); while (fgets (buffer, 100, file)) fputs (buffer, stdout); clearerr (file); } Note: it's not tested, it is very realistic pseudocode but it is pseudocode after all :-) -- Josh > > Thanks for the insight! > > Matthew > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"