chore(judger): add runp::double_to_itimerval

This commit is contained in:
Baoshuo Ren 2022-11-08 15:24:21 +08:00
parent 1e63154574
commit d7aea4cbbc
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 20 additions and 11 deletions

View File

@ -1,3 +1,4 @@
#include <cmath>
#include <string>
#include <vector>
#include <map>
@ -7,6 +8,7 @@
#include <filesystem>
#include <exception>
#include <stdexcept>
#include <sys/time.h>
#define UOJ_GCC "/usr/bin/gcc-11"
#define UOJ_GPLUSPLUS "/usr/bin/g++-11"
@ -388,6 +390,23 @@ namespace runp {
}
}
};
itimerval double_to_itimerval(const double &tl) {
struct itimerval val;
long tl_sec = (long)tl;
long tl_usec = (long)((tl - floor(tl)) * 1000 + 100) * 1000;
if (tl_usec >= 1'000'000l) {
tl_sec++;
tl_usec -= 1'000'000l;
}
val.it_value = {tl_sec, tl_usec};
val.it_interval = {0, 100 * 1000};
return val;
}
}
namespace runp::interaction {

View File

@ -202,15 +202,7 @@ void set_limit(int r, int rcur, int rmax = -1) {
}
void set_user_cpu_time_limit(double tl) {
struct itimerval val;
long tl_sec = (long)tl;
long tl_usec = (long)((tl - floor(tl)) * 1000 + 100) * 1000;
if (tl_usec >= 1'000'000l) {
tl_sec++;
tl_usec -= 1'000'000l;
}
val.it_value = {tl_sec, tl_usec};
val.it_interval = {0, 100 * 1000};
itimerval val = runp::double_to_itimerval(tl);
setitimer(ITIMER_VIRTUAL, &val, NULL);
}

View File

@ -1,6 +1,5 @@
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@ -15,7 +14,6 @@
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/user.h>
#include <sys/time.h>
#include <sys/prctl.h>
#include <fcntl.h>
#include <argp.h>