-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeManager.cpp
58 lines (51 loc) · 1.3 KB
/
timeManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdint.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <signal.h>
#include "timeManager.hpp"
int cutOff=0;
using namespace std;
void TimeHandler(int sig){
if(sig==SIGXCPU){
std::cout<<"CUT OFF\n";
cutOff=1;
}
}
int checkCutOf(){
if(cutOff==1)
return 1;
return 0;
}
void initializeTime(int n){
// check if there a previous Limit
struct rlimit previousLimit;
if(getrlimit(RLIMIT_CPU,&previousLimit)!=0){
printf("setrlimit() failed with errno=%d\n", errno);
exit(-1);
}
int previousCutOff=previousLimit.rlim_cur;
int currentCutOff;
// if there is not initialize the signal and make the current
if(previousCutOff<=0){
signal(SIGXCPU,TimeHandler);
currentCutOff=n*5;
}
// else remember the time limit of the previous limit and increase with the new value
else{
currentCutOff=n*5+previousCutOff;
}
// add it as the new limit
struct rlimit newLimit;
newLimit.rlim_cur=currentCutOff;
newLimit.rlim_max=RLIM_INFINITY;
if (setrlimit(RLIMIT_CPU, &newLimit) != 0) {
printf("setrlimit() failed with errno=%d\n", errno);
exit(1);
}
// reset the cut off
cutOff=0;
}