Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Appendix C Chapter 14 Solutions 933


the definition of thefd_settype with a separate symbol. We need to define
this, too.
For example, on FreeBSD 8.0, we need to define _SYS_SELECT_H_ to
prevent the inclusion of<sys/select.h>and we need to define_FD_SET
to prevent the inclusion of the definition for thefd_setdata type.


  1. Sometimes, for compatibility with older applications, <sys/types.h>
    defines the size of thefd_set, so we need to include it first, then undefine
    FD_SETSIZE.Note that some systems use__FD_SETSIZEinstead.

  2. Weneed to redefineFD_SETSIZE(or__FD_SETSIZE)tothe maximum file
    descriptor number we want to be able to use withselect.

  3. Weneed to undefine the symbols we defined in step 1.

  4. Finally, we can include<sys/select.h>.


Before we run the program, we need to configurethe system to allow us to open
as many file descriptors as we might need so that we can actually make use of
FD_SETSIZEfile descriptors.
14.4 The following table lists the functions that do similar things.

FD_ZERO sigemptyset
FD_SET sigaddset
FD_CLR sigdelset
FD_ISSET sigismember

There is not anFD_xxxfunction that corresponds tosigfillset.With signal
sets, the pointer to the set is always the first argument, and the signal number is
the second argument. With descriptor sets, the descriptor number is the first
argument, and the pointer to the set is the next argument.
14.5 FigureC.16 shows an implementation usingselect.
#include "apue.h"
#include <sys/select.h>
void
sleep_us(unsigned int nusecs)
{
struct timeval tval;
tval.tv_sec = nusecs / 1000000;
tval.tv_usec = nusecs % 1000000;
select(0, NULL, NULL, NULL, &tval);
}

Figure C.16 Implementation ofsleep_ususingselect

FigureC.17 shows an implementation usingpoll.
Free download pdf