Tuesday 23 February 2016

Update the program in Figure 2.16 to avoid the needless processing that occurs when sysconf returns LONG_MAX as the limit for OPEN_MAX.

If OPEN_MAX is indeterminite or ridiculously large (i.e., equal to LONG_MAX), we can use geTRlimit to get the per process maximum for open file descriptors. Since the per process limit can be modified, we can't cache the value obtained from the previous call


#include "apue.h"
#include <limits.h>
#include <sys/resource.h>

#define OPEN_MAX_GUESS 256

long
open_max(void)
{
    long openmax;
    struct rlimit rl;

    if ((openmax = sysconf(_SC_OPEN_MAX)) < 0 ||
      openmax == LONG_MAX) {
        if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
            err_sys("can't get file limit");
        if (rl.rlim_max == RLIM_INFINITY)
            openmax = OPEN_MAX_GUESS;
        else
            openmax = rl.rlim_max;
    }
    return(openmax);

}

No comments:

Post a Comment