setrlimit always fails

Corinna Vinschen corinna-cygwin@cygwin.com
Fri Feb 12 09:11:06 GMT 2021


On Feb 11 10:23, Patrick Chkoreff wrote:
> 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?

Most of setrlimit is not supported.  There's a bit of RLIMIT_CORE
and RLIMIT_NOFILE support but it's basically fake because Windows
doesn't (or didn't in some cases) have support for setting limits
for the own and child processes.


Corinna


More information about the Cygwin mailing list