setrlimit always fails

Patrick Chkoreff pc@fexl.com
Thu Feb 11 15:23:02 GMT 2021


I'm trying to use setrlimit to impose limits on various resources such
as CPU time and memory.  The call to setrlimit always fails.  I've
distilled this into the following example test.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>

int main(void)
    {
    struct rlimit rlim;
    rlim.rlim_cur = 1;
    rlim.rlim_max = 1;
    if (setrlimit(RLIMIT_CPU,&rlim) < 0)
        {
        perror("setrlimit");
        exit(1);
        }
    printf("HEY\n");
    while (1)
        {
        }
    printf("BYE\n");
    return 0;
    }

$ gcc test.c
$ ./a.exe
setrlimit: Invalid argument


I have found that the only way to make the setrlimit call succeed is to
use RLIM_INFINITY:

    rlim.rlim_cur = RLIM_INFINITY;
    rlim.rlim_max = RLIM_INFINITY;

But then of course it does not achieve the desired result of timing out
after 1 second.

Any ideas?


-- Patrick


More information about the Cygwin mailing list