Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2010

    [SOLVED] Another problem rears its ugly head[er]


    In the course of trying to understand some software, I've been reading the Manpage for select(2).
    It gives the following example:

      #include <stdio.h>
           #include <stdlib.h>
           #include <sys/time.h>
           #include <sys/types.h>
           #include <unistd.h>
           int       main(void)
               fd_set rfds;
               struct timeval tv;
               int retval;
               /* Watch stdin (fd 0) to see when it has input. */
               FD_SET(0, &rfds);
               /* Wait up to five seconds. */
               tv.tv_sec = 5;
               tv.tv_usec = 0;
               retval = select(1, &rfds, NULL, NULL, &tv);
               /* Don't rely on the value of tv now! */
               if (retval == -1)
               else if (retval)
                   printf("Data is available now.\n");
                   /* FD_ISSET(0, &rfds) will be true. */
                   printf("No data within five seconds.\n");

    However, experimentation appears to show that it will work with just the following headers.

    #include <stdio.h>
    #include <stdlib.h>
    None the less, I have to assume that the Manpage is correct and that I am misleading myself in some way.

    Am I, and if so: how; please?

  2. #2
    Linux User
    Join Date
    Nov 2008
    Tokyo, Japan
    The manual page I am using says you need only to include <sys/select.h>, but in the past, using older versions of the POSIX standard, you would have also needed to include the <sys/time.h> <sys/types.h> and <unistd.h> .

    When you include <stdlib.h>, it now includes everything you need in this example except for printf which is included in <stdio.h>. The <stdlib.h> library recursively includes <sys/types.h> which recursively includes both <time.h> and <sys/select.h>.

    See for yourself:
    % grep sys/types.h /usr/include/stdlib.h
    /usr/include/stdlib.h: # include <sys/types.h>	/* we need int32_t... */
    % grep sys/select.h /usr/include/sys/types.h
    /usr/include/sys/types.h: # include <sys/select.h>

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Including headers you don't need usually isn't a problem, so follow the man pages for this. My guess is that stdlib.h also includes the sys/time.h and sys/types.h headers, or something else it includes does. All Linux system headers are guarded so they will not be actually included more than once, even if you include them multiple times in your source and header files.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. $spacer_open
  5. #4
    Just Joined!
    Join Date
    Mar 2010
    Thank you for your reassuring replies.

    The grep idea is really good.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts