Understanding select...

David Jacobson rimux dcj at AUSTIN.LOCKHEED.COM
Sat Apr 13 07:08:40 AEST 1991


HI! ;-)

  I'm rather a novice at Unix I/O.  I've been RTFMing about the
select command and believe I'm misunderstanding it.

  I would like to use the select command to sit on a serial device
object descriptor (fd) and come off the select when any data has come
in.

Q1:  Is this possible?

  I'd then proceed to read (and write) to (and from) the object
descriptor.  Unfortunately, my program sits on my select and never comes
off.

Q2:  Do you see the error in my concept of select or in my code below?

Any help would be greatly appreciated.  I'll post the answer for those
that are interested.  A simplified example of my attempt follows:

=== Start Source ===================================================

... all the necessary includes

main(argc, argv)
int argc;
char *argv[];
{
   static int maxfdp1;
   fd_set readmask;
   boolean nfound = FALSE;
   static struct timeval timeout;

   /**  open serial port file descriptor  **/
   if ((fd = open(STUDeviceName, O_RDWR|O_NOCTTY)) == -1) {
      fprintf(stderr,"Could not open %s.\n",STUDeviceName);
      exit(1);
      }

   FD_ZERO(&readmask);
   FD_SET(fd, &readmask);
   maxfdp1 = fd++;

   /**  Watch for IO on port  **/
   nfound =
     select(maxfdp1, &readmask, (fd_set *) 0, (fd_set *) 0, NULL);
   if (nfound < 0)
      {
      fprintf(stderr,"Select error.\n");
      exit(1);
      }

... start reading/writing from/to fd

} /* end main() */

=== End Source =====================================================

____________________________________ _______________________________________
             .                      |  David C. Jacobson
=========___/ \___    Lockheed      |  (512) 386-4267
   =======`/ . \'  Austin Division  |  INTERNET:
       ===/'   `\       (LAD)       |    dcj at austin.lockheed.com
____________________________________|_______________________________________
`Nothing of what I say has anything to do with anybody in any way.'



More information about the Comp.unix.programmer mailing list