On Thu, Mar 21, 2013 at 01:26:24PM -0400, Wietse Venema wrote:

> What does the following program print?
>
> -------
> #include <sys/types.h>
> #include <sys/sysctl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
>
> #define MAX_FILES_PER_PROC      "kern.maxfilesperproc"
>
> #define perrorexit(text) do { perror(text); exit(1); } while (0)
>
> int     main(int argc, char **argv)
> {
>     int     limit;
>     int     len = sizeof(limit);
>
>     if (sysctlbyname(MAX_FILES_PER_PROC, &limit, &len,
>                      (void *) 0, (size_t) 0) < 0)
>         perrorexit("sysctlbyname MAX_FILES_PER_PROC");
>     printf("%d\n", limit);
>     return (0);
> }
> -------

After chaning "int len" to "size_t len" and "int limit" to "long limit":

        #include <sys/types.h>
        #include <sys/sysctl.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>

        #define MAX_FILES_PER_PROC      "kern.maxfilesperproc"

        #define perrorexit(text) do { perror(text); exit(1); } while (0)

        int     main(int argc, char **argv)
        {
            long    limit;
            int     len = sizeof(limit);

            if (sysctlbyname(MAX_FILES_PER_PROC, &limit, &len,
                             (void *) 0, (size_t) 0) < 0)
                perrorexit("sysctlbyname MAX_FILES_PER_PROC");
            printf("%ld\n", limit);
            return (0);
        }

it compiles and prints "10240". After running:

        # sysctl -w kern.maxfilesperproc=10241

it prints "10241".

--
        Viktor.

Reply via email to