Re: Compiling problem on AIX 4.2.1 with gcc

From: Duane Wessels <wessels@dont-contact.us>
Date: Tue, 20 Apr 1999 15:20:30 -0600

Henrik Nordstrom writes:

>Duane Wessels wrote:
>
>> while((j=open("/dev/null", 0)) > 0) i=j;
>
>I hope this only is for testing purposes.

why, whats wrong with it?

in what cases, or for what systems does it not do the right thing?

>How about something like this?
>
>The difference between SETMAXFILES and SETMAXFILES2 is that SETMAXFILES2
>only sets the limit if no limit is known prior to the call, while
>SETMAXFILES sets the limit to at most the argument.
>
>int
>main(int argc, char *argv[]) {
> FILE *fp;
> int i=-1,j;
>#define SETMAXFILES(n) (j=(n),i=((i<0||j<i&&j>0)?j:i))
>#define SETMAXFILES2(n) (j=(n),i=((i<0)?j:i))
>#if HAVE_SETRLIMIT
> struct rlimit rl;
>#endif
>#if __CYGWIN32__
> /* getrlimit and sysconf returns bogous values on cygwin32.
> * Number of fds is virtually unlimited in cygwin (sys/param.h)
> */
> SETMAXFILES(NOFILE)
>#endif
>#if HAVE_SYSCONF && defined(_SC_OPEN_MAX)
> /* sysconf should be trustful if it exists */
> SETMAXFILES(sysconf(_SC_OPEN_MAX));
>#endif

A problem with _SC_OPEN_MAX occurs on Linux. According to Jordan
Mendelson (jordy@wserv.com):

        Linus has stated that OPEN_MAX will not change in kernel
        headers and it is unreliable to use it to check and Glibc puts
        in it's own headers which also force the configure script to
        default to 256. getrlimit() provides a reliable way to get the
        maximum file descriptors.

>#if HAVE_GETRLIMIT && defined(RLIMIT_NOFILE)
> /* getrlimit returns the currently configured limit,
> * which is not neccesary the upper limit of the system...
> */
> if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
> perror("getrlimit: RLIMIT_NOFILE");
> else if (i<0)
> SETMAXFILES2(rl.rlim_cur);
>#endif

Duane W.
Received on Tue Jul 29 2003 - 13:15:57 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:12:05 MST