I have this code:
pid_t vPid=fork();
int vStat;
switch(vPid){
case -1: perror("fork");
exit(1);
case 0:
//proces fiu
if(chdir("/var/code/p1")==0){
system("make clean");
system("make");
//limit on data
struct rlimit vLimD;
vLimD.rlim_cur = 10000000;//10Mb
vLimD.rlim_max = 10000000; //10Mb
setrlimit(RLIMIT_DATA, &vLimD);
//limit on cpu time
struct rlimit vLimCPU;
vLimCPU.rlim_cur = 10;//10 sec
vLimCPU.rlim_max = 10;//10 sec
cout<<"limits return "<<setrlimit(RLIMIT_CPU, &vLimCPU);
execl("/var/code/p1/p1","",NULL);
}
else {exit(1);}
break;
default:
while(wait(&vStat)!=vPid);
break;
}
The process /var/code/p1/p1 runs for 40 seconds, and I want to limit this process to run for just 10 seconds with vLimitCPU(setrlimit), and after 10 sec do something, but is not print nothing like “limits return value” (the first setrlimit returns 0)
The man page
setrlimit(2)states:Probably you do not receive any output because your output stream is not flushed (output
std::endlto solve this problem). The program may be killed by the signal before it can flush its output buffers. It may also well be that the buffers don’t surviveexecl()in the first place.Some more advice:
Then you should implement said signal handler and your program will
continue from there. Implement it to see whether the signal is
caught.
Note that you will also need a fallback solution for the case that
the process unexpectedly terminates before CPU time is up.
Note also that if I take it correctly, you also need to account for
the CPU time already used by your process when setting the limit.