🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.next.programmer
2 messages
2 total messages Started by Brian Willoughby Mon, 31 Aug 1992 08:24
Q: Using select() to detect another process appending to a file
#922
Author: 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
Re: Q: Using select() to detect another process appending to a file
#927
Author: Louis A. Mamakos
Date: Mon, 31 Aug 1992 14:57
9 lines
937 bytes
In article <BtuA1H.7p0@sounds.wa.com> BrianW@SoundS.WA.com writes:
>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?)

You are correct here; select() only works on pipes, tty-like devices
and sockets.  It doesn't do anything useful on plain files.

louie
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads