home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13584 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.6 KB  |  133 lines

  1. Path: sparky!uunet!gumby!wupost!bcserv!pat
  2. From: pat@bcserv.wustl.edu (Niemeyer (Pat))
  3. Newsgroups: comp.unix.questions
  4. Subject: What's wrong with select() ?
  5. Date: 19 Nov 92 21:55:44 GMT
  6. Organization: Washington University in Saint Louis, MO USA
  7. Lines: 122
  8. Message-ID: <pat.722210144@bcserv>
  9. NNTP-Posting-Host: bcserv.wustl.edu
  10.  
  11.  
  12. I'm totally confused.
  13.  
  14. I find that I cannot get the select() system call to 
  15. correctly identify when there are characters waiting
  16. on a file descriptor that I have opened with 'open()'.
  17.  
  18. It *does* seem to work for the fd associated with stdin.
  19.  
  20. The attached piece of code illustrates the difficulty.
  21.  
  22. It simply reads chars, at 1/2 second intervals, from either
  23. either stdin or the device given as it's argument.
  24. Before each read, it shows the status that select() returns as
  25. to whether there are chars to be read.
  26. (1 = pending, 0 = buffer empty)
  27.  
  28. The idea is this:  
  29.  
  30.     run it on your tty, (first as stdin, then as an argument to be opened)
  31.  
  32.     type a blurb of garbage, (and hit return if not in cbreak mode)
  33.  
  34.     and watch as it reads off character by character.
  35.  
  36. What I find is that it works perfectly for the first case (run from stdin)
  37. but totally fails for the second (where it opens your tty).
  38.  
  39. I have tried many variations on this:
  40. I have used a real serial port as opposed to a ptty.
  41. and I have verified that the termio settings don't effect the problem 
  42. as far as I can tell with the information reported by 'stty -a'.
  43.  
  44. Any help would be greatly appreciated.
  45.  
  46. /*----------------------------------------------*/
  47. #include <sys/types.h>
  48. #include <sys/time.h>
  49. #include <stdio.h>
  50. #include <fcntl.h>
  51.  
  52. char getch();
  53. int rdchk();
  54.  
  55. main(argc, argv)
  56. int argc; char *argv[];
  57.     {
  58.     int iofd;
  59.  
  60.     setbuf(stdout, NULL);
  61.  
  62.     if ( argc == 2 )
  63.         if ( (iofd = open(argv[1], O_RDWR)) == -1) 
  64.             {
  65.             printf("openerror\n"); 
  66.             exit(1);
  67.             }
  68.         else
  69.             printf("Opened %s\n", argv[1]);
  70.     else
  71.         {
  72.         iofd=fileno(stdin);
  73.         printf("Opened stdin\n");
  74.         }
  75.  
  76.     while(1) 
  77.         {
  78.         usleep(500000);
  79.         rdchk(iofd);
  80.         getch(iofd);
  81.         }
  82.     }
  83.  
  84. char getch(iofd)
  85. int iofd;
  86.     {
  87.     int got;
  88.     char ch;
  89.  
  90.     if ( (got = read(iofd, &ch, 1) ) == -1 )
  91.         {
  92.         printf("read error\n");
  93.         return(-1);
  94.         }
  95.  
  96.     if (got > 0) 
  97.         {
  98.         printf("(%c)", ch); 
  99.         return(ch);
  100.         } 
  101.     else 
  102.         {
  103.         printf("timeout");
  104.         return(-1);
  105.         }
  106.     }
  107.  
  108. /* return true if a read will not block */
  109. int rdchk( fd )
  110. int fd;
  111.     {
  112.     int got;
  113.     fd_set fdset;
  114.     struct timeval timeout;
  115.  
  116.     FD_ZERO (&fdset);
  117.     FD_SET (fd, &fdset);
  118.     memset(&timeout, 0, sizeof(timeout));
  119.  
  120.     got = select (1, &fdset, NULL, NULL, &timeout);
  121.     printf("select returned %u\n\r", got);
  122.     return( got > 0 );
  123.     }
  124.  
  125. /*----------------------------------------------*/
  126.  
  127. /* Pat@bcserv.wustl.edu */
  128.  
  129. -- 
  130.  
  131.  
  132. Pat
  133.