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:
CPU time limit in seconds. When the process reaches the soft limit, it is sent a SIGXCPU signal. The default action for this signal is to terminate the process. However, the signal can be caught, and the handler can return control to the main program.
Probably you do not receive any output because your output stream is not flushed (output std::endl to 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 survive execl() 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With