home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gumby!wupost!bcserv!pat
- From: pat@bcserv.wustl.edu (Niemeyer (Pat))
- Newsgroups: comp.unix.questions
- Subject: What's wrong with select() ?
- Date: 19 Nov 92 21:55:44 GMT
- Organization: Washington University in Saint Louis, MO USA
- Lines: 122
- Message-ID: <pat.722210144@bcserv>
- NNTP-Posting-Host: bcserv.wustl.edu
-
-
- I'm totally confused.
-
- I find that I cannot get the select() system call to
- correctly identify when there are characters waiting
- on a file descriptor that I have opened with 'open()'.
-
- It *does* seem to work for the fd associated with stdin.
-
- The attached piece of code illustrates the difficulty.
-
- It simply reads chars, at 1/2 second intervals, from either
- either stdin or the device given as it's argument.
- Before each read, it shows the status that select() returns as
- to whether there are chars to be read.
- (1 = pending, 0 = buffer empty)
-
- The idea is this:
-
- run it on your tty, (first as stdin, then as an argument to be opened)
-
- type a blurb of garbage, (and hit return if not in cbreak mode)
-
- and watch as it reads off character by character.
-
- What I find is that it works perfectly for the first case (run from stdin)
- but totally fails for the second (where it opens your tty).
-
- I have tried many variations on this:
- I have used a real serial port as opposed to a ptty.
- and I have verified that the termio settings don't effect the problem
- as far as I can tell with the information reported by 'stty -a'.
-
- Any help would be greatly appreciated.
-
- /*----------------------------------------------*/
- #include <sys/types.h>
- #include <sys/time.h>
- #include <stdio.h>
- #include <fcntl.h>
-
- char getch();
- int rdchk();
-
- main(argc, argv)
- int argc; char *argv[];
- {
- int iofd;
-
- setbuf(stdout, NULL);
-
- if ( argc == 2 )
- if ( (iofd = open(argv[1], O_RDWR)) == -1)
- {
- printf("openerror\n");
- exit(1);
- }
- else
- printf("Opened %s\n", argv[1]);
- else
- {
- iofd=fileno(stdin);
- printf("Opened stdin\n");
- }
-
- while(1)
- {
- usleep(500000);
- rdchk(iofd);
- getch(iofd);
- }
- }
-
- char getch(iofd)
- int iofd;
- {
- int got;
- char ch;
-
- if ( (got = read(iofd, &ch, 1) ) == -1 )
- {
- printf("read error\n");
- return(-1);
- }
-
- if (got > 0)
- {
- printf("(%c)", ch);
- return(ch);
- }
- else
- {
- printf("timeout");
- return(-1);
- }
- }
-
- /* return true if a read will not block */
- int rdchk( fd )
- int fd;
- {
- int got;
- fd_set fdset;
- struct timeval timeout;
-
- FD_ZERO (&fdset);
- FD_SET (fd, &fdset);
- memset(&timeout, 0, sizeof(timeout));
-
- got = select (1, &fdset, NULL, NULL, &timeout);
- printf("select returned %u\n\r", got);
- return( got > 0 );
- }
-
- /*----------------------------------------------*/
-
- /* Pat@bcserv.wustl.edu */
-
- --
-
-
- Pat
-