[comp.unix.programmer] Understanding select...

dcj@AUSTIN.LOCKHEED.COM (David Jacobson rimux) (04/13/91)

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@austin.lockheed.com
____________________________________|_______________________________________
`Nothing of what I say has anything to do with anybody in any way.'

theo@integow.uucp (Theo Hardendood) (04/18/91)

From article <533@shrike.AUSTIN.LOCKHEED.COM>, by dcj@AUSTIN.LOCKHEED.COM (David Jacobson rimux):
>   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?
> 

Yes (although I read somewhere that not all devices support select calls)

> 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?
> 

... (some stuff deleted)

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

This must be:

     maxfdp1 = fd + 1;

Hope this helps!

-- 
    UUCP: ..!uunet!mcsun!hp4nl!integow!theo  or  theo@integow.uucp
Theo Hardendood, Sr. software engineer, Integrity software consultants, 
         Pelmolenlaan 2, 3447 GW  Woerden, The Netherlands.
               tel +31 3480 30131, fax +31 3480 30182

goehring@mentor.cc.purdue.edu (Scott Goehring) (04/24/91)

In article <1580@integow.uucp> theo@integow.uucp (Theo Hardendood) writes:

   From article <533@shrike.AUSTIN.LOCKHEED.COM>, by
   dcj@AUSTIN.LOCKHEED.COM (David Jacobson rimux): 
   >   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?

   Yes (although I read somewhere that not all devices support select
   calls)

correction:  all devices *support* select.  some, however, always
report that the device is ready (disks, for example), thereby
supporting select in a less-than-useful fashion.
--
		  Help stamp out vi in our lifetime!
	Scott Goehring			goehring@mentor.cc.purdue.edu
					goehring@gnu.ai.mit.edu
	   "Then go cast an astral spell on yourself, ok?"