Find the answer to your Linux question:
Results 1 to 4 of 4
Hello In the course of trying to understand some software, I've been reading the Manpage for select(2). It gives the following example: Code: #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2010
    Posts
    16

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


    Hello

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

    Code:
      #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_ZERO(&rfds);
               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)
                   perror("select()");
               else if (retval)
                   printf("Data is available now.\n");
                   /* FD_ISSET(0, &rfds) will be true. */
               else
                   printf("No data within five seconds.\n");
    
               exit(EXIT_SUCCESS);
           }


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

    Code:
    #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
    Location
    Tokyo, Japan
    Posts
    260
    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:
    Code:
    % 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
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,755
    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
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    16
    Thank you for your reassuring replies.

    ramin.honary:
    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
  •