Set resource limit for your C++ program

Правка en4, от sober_phoenix, 2020-09-12 08:11:34

If you're using any build script to build and compile your C++ program then there's a high chance your system got frozen during a contest ( possibly because there were some infinite loop that you didn't notice ) and probably you had to restart your system . This gets very irritating during a contest . Although there's a workaround of this problem using timeout script in some system, it's but not always compatible with g++ commands . A better solution is to set the resource limit ( CPU time , virtual memory , stack size etc. ) in your program using a function : setrlimit . A general template is :

#include <sys/time.h>
#include <sys/resource.h>
int setrlimit(int resource, const struct rlimit *rlim);

A detailed description of all the terminologies can be found here. the setrlimit takes two arguments the first one (resource) specifies what sort of resource you want to set limit on (e.g RLIMIT_AS for the process's virtual memory (address space) , RLIMIT_DATA for the maximum size of the process's data segment etc. ) , And the second argument specifies the numeric value of the limit that you want to impose on (it's value depends on the resource parameter that you're specifying ) . Personally I like to impose limit on the CPU time (RLIMIT_CPU) and file size (RLIMIT_FSIZE).

Note : This function works on UNIX based system , And in order to ignore any of these limits in your final submitted program it's better to wrap this snippet around an #ifndef #endif conditional directive with ONLINE_JUDGE macro . That being said here's the minimal code snippet that I use :

#include<bits/stdc++.h>   
#ifndef ONLINE_JUDGE
#include<sys/resource.h>
#include <sys/time.h>
#endif
int main(){  
        #ifndef ONLINE_JUDGE
        /* setting up soft limit on resources */
        rlimit rlim , rlim_time ;
        if(getrlimit( RLIMIT_FSIZE , &rlim) || getrlimit(RLIMIT_CPU , &rlim_time) )  
            return 1 ; 
        rlim.rlim_cur = MAX_FILE_SIZE ; 
        rlim_time.rlim_cur = CPU_TIME ; 
        if(setrlimit( RLIMIT_FSIZE , &rlim ) || setrlimit(RLIMIT_CPU , &rlim_time))
            return 2 ; 
        #endif
}
Теги #c++, #space+time optimized, sublime text 3, sublime text

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en9 Английский sober_phoenix 2020-09-12 09:23:24 1 Tiny change: 'Personally I like to' -> 'Personally, I like to'
en8 Английский sober_phoenix 2020-09-12 09:21:15 8 Tiny change: 'e system, it's but not alway' -> 'e system, but it's not alway'
en7 Английский sober_phoenix 2020-09-12 08:19:07 0 (published)
en6 Английский sober_phoenix 2020-09-12 08:18:24 117
en5 Английский sober_phoenix 2020-09-12 08:14:09 3 Tiny change: 'to impose on (it's val' -> 'to impose (it's val'
en4 Английский sober_phoenix 2020-09-12 08:11:34 6 Tiny change: 'pile your program Then there'' -> 'pile your C++ program then there''
en3 Английский sober_phoenix 2020-09-12 08:10:48 19
en2 Английский sober_phoenix 2020-09-12 08:09:20 11 code snippets updated
en1 Английский sober_phoenix 2020-09-12 08:08:01 2338 Initial revision (saved to drafts)