🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #922

Q: Using select() to detect another process appending to a file

#922
From: Brian Willoughby
Date: Mon, 31 Aug 1992 08:24
77 lines
2816 bytes
I am trying to use the select() function to detect changes appended to a  
standard disk file (which isn't stdin or any other pipe for my program).  I  
have everything working, except that select() seems to keep returning 1 to  
indicate that the file is ready for reading, even though I have read everything  
in the file up to EOF.  I am doing everything in a separate thread, hoping that  
select() will block until another task appends to the file, but apparently  
select() always thinks that the file is ready for reading, even if the file  
position for the file descriptor in question is at EOF.

Can anyone tell me what is wrong? (perhaps select() only works on pipes which  
are emptied when read and doesn't work on real files if I only want to know  
about data appended beyond the current file position?)

Here is a rough sketch of my code:
@interface Controller:Object
{
	id hashTable;
	int fd;
	FILE *fp;
	fd_set readfds;
}
@end
@implementation Controller
-appDidInit
{
	hashTable = [[HashTable alloc] initKeyDesc:"i" valueDesc:"i"];
	fp = fopen(file, "r");
	if (fp)
	{
		fd = fileno(fp);
		[hashTable insertKey:FWTAG_FILE value:fd];
	}
	[self startWatching];
}
-startWatching
{
	int fdTag, fd, nfound;
	NXHashState state = [hashTable initState];

	while ([hashTable nextState:&state key:&fdTag value:&fd])
		FD_SET(fd, &readfds);
	nfound = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
	state = [hashTable initState];
	while ([hashTable nextState:&state key:&fdTag value:&fd])
		if (FD_ISSET(fd, &readfds))
			[self notifyFileReady:fdTag];
}
-notifyFileReady:(int)fdTag
{
	switch (fdTag)
	{
		case ERROR:
			return;
		case FWTAG_FILE:
			while (cc = read(fd, buffer, BUF_SIZE - 1))
			{
				buffer[cc] = 0;
				[self handle_data:buffer];
			}
			break;
	}
	[self startWatching];
}
@end

This does work, since the file descriptor's position is maintained and I never  
process the same data twice.  But fprintf()s throughout the code reveal that  
select() is never blocking, but seemingly alway indicates that the file is  
ready for reading, even though the read()s that I do return EOF immediately.

Comments or clarifications welcome.
Thanks,
-- 
Brian Willoughby	Software Design Engineer, BSEE
BrianW@SoundS.WA.com	SoundSoftware
NeXTmail welcome	- NO EMAIL SOLICITATION without prior permission
Technical Details
Message-ID: <BtuA1H.7p0@sounds.wa.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!nwnexus!sounds!brianw

Show Full Headers
Complete Article Headers
Newsgroups: comp.sys.next.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!nwnexus!sounds!brianw
From: BrianW@SoundS.WA.com (Brian Willoughby)
Subject: Q: Using select() to detect another process appending to a file
Message-ID: <BtuA1H.7p0@sounds.wa.com>
Sender: brianw@sounds.wa.com (Brian Willoughby)
Reply-To: BrianW@SoundS.WA.com
Organization: SoundSoftware, Bellevue, WA, USA
Date: Mon, 31 Aug 1992 08:24:53 GMT
Lines: 77