how to use exceptfds argument in select system call

Hi,

Can any one tell me how to use the fourth argument of select system call.I saw example  "port forwarding" on the net,but it was too complex for me to understand.Can any one explain me about the usage of exceptfds argument of select system call with simple example.

Thanks.

Arguments 2, 3, and 4 are all the same thing - an fd_set (which is a datatype defined in sys/select.h. Arg #2 is the fd_set for fd's you have open for reading. #3 is for writing,
#4 is for error streams like stderr. An fd_set is a "list" of file descriptor numbers that select is supposed to check for you. select() clears the values (meaning you have to reset the values in each of the fd_set objects), and on success it sets only those fd values that you can : read(#2) write(#3) errorstream(#4).

Use the following macros to set, clear, or test an fd_set
FD_ZERO - initialize
FD_SET - turn on an fd value setting
FD_CLR - turn off an fd value setting in the set
FD_ISSET - test to see if a value is on or off.

 #define MASK(f)     (1 << (f))
           #define NTTYS 4

                int tty[NTTYS];
                int ttymask[NTTYS];
                int readmask = 0;
                int readfds;
                int nfound, i;
                struct timeval timeout;

                   /* First open each terminal for reading and put the
                    * file descriptors into array tty[NTTYS].  The code
                    * for opening the terminals is not shown here.
                    */

                for (i=0; i < NTTYS; i++) {
                   ttymask = MASK(tty);
                   readmask |= ttymask;
                }

                timeout.tv_sec  = 5;
                timeout.tv_usec = 0;
                readfds = readmask;

                /* select on NTTYS+3 file descriptors if stdin, stdout
                 * and stderr are also open
                 */
                if ((nfound = select (NTTYS+3, &readfds, 0, 0, &timeout)) == -1)
                   perror ("select failed");
                else if (nfound == 0)
                   printf ("select timed out \n");
                else for (i=0; i < NTTYS; i++)
                   if (ttymask & readfds)
                      /* Read from tty.  The code for reading
                       * is not shown here.
                       */
                else printf ("tty[%d] is not ready for reading \n",i);