home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / next / programm / 5877 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.7 KB  |  89 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!nwnexus!sounds!brianw
  3. From: BrianW@SoundS.WA.com (Brian Willoughby)
  4. Subject: Q: Using select() to detect another process appending to a file
  5. Message-ID: <BtuA1H.7p0@sounds.wa.com>
  6. Sender: brianw@sounds.wa.com (Brian Willoughby)
  7. Reply-To: BrianW@SoundS.WA.com
  8. Organization: SoundSoftware, Bellevue, WA, USA
  9. Date: Mon, 31 Aug 1992 08:24:53 GMT
  10. Lines: 77
  11.  
  12.  
  13. I am trying to use the select() function to detect changes appended to a  
  14. standard disk file (which isn't stdin or any other pipe for my program).  I  
  15. have everything working, except that select() seems to keep returning 1 to  
  16. indicate that the file is ready for reading, even though I have read everything  
  17. in the file up to EOF.  I am doing everything in a separate thread, hoping that  
  18. select() will block until another task appends to the file, but apparently  
  19. select() always thinks that the file is ready for reading, even if the file  
  20. position for the file descriptor in question is at EOF.
  21.  
  22. Can anyone tell me what is wrong? (perhaps select() only works on pipes which  
  23. are emptied when read and doesn't work on real files if I only want to know  
  24. about data appended beyond the current file position?)
  25.  
  26. Here is a rough sketch of my code:
  27. @interface Controller:Object
  28. {
  29.     id hashTable;
  30.     int fd;
  31.     FILE *fp;
  32.     fd_set readfds;
  33. }
  34. @end
  35. @implementation Controller
  36. -appDidInit
  37. {
  38.     hashTable = [[HashTable alloc] initKeyDesc:"i" valueDesc:"i"];
  39.     fp = fopen(file, "r");
  40.     if (fp)
  41.     {
  42.         fd = fileno(fp);
  43.         [hashTable insertKey:FWTAG_FILE value:fd];
  44.     }
  45.     [self startWatching];
  46. }
  47. -startWatching
  48. {
  49.     int fdTag, fd, nfound;
  50.     NXHashState state = [hashTable initState];
  51.  
  52.     while ([hashTable nextState:&state key:&fdTag value:&fd])
  53.         FD_SET(fd, &readfds);
  54.     nfound = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
  55.     state = [hashTable initState];
  56.     while ([hashTable nextState:&state key:&fdTag value:&fd])
  57.         if (FD_ISSET(fd, &readfds))
  58.             [self notifyFileReady:fdTag];
  59. }
  60. -notifyFileReady:(int)fdTag
  61. {
  62.     switch (fdTag)
  63.     {
  64.         case ERROR:
  65.             return;
  66.         case FWTAG_FILE:
  67.             while (cc = read(fd, buffer, BUF_SIZE - 1))
  68.             {
  69.                 buffer[cc] = 0;
  70.                 [self handle_data:buffer];
  71.             }
  72.             break;
  73.     }
  74.     [self startWatching];
  75. }
  76. @end
  77.  
  78. This does work, since the file descriptor's position is maintained and I never  
  79. process the same data twice.  But fprintf()s throughout the code reveal that  
  80. select() is never blocking, but seemingly alway indicates that the file is  
  81. ready for reading, even though the read()s that I do return EOF immediately.
  82.  
  83. Comments or clarifications welcome.
  84. Thanks,
  85. -- 
  86. Brian Willoughby    Software Design Engineer, BSEE
  87. BrianW@SoundS.WA.com    SoundSoftware
  88. NeXTmail welcome    - NO EMAIL SOLICITATION without prior permission
  89.