From d7aea4cbbc77f5c2b1a9a2a062a3e6acae78f894 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 8 Nov 2022 15:24:21 +0800 Subject: [PATCH] chore(judger): add runp::double_to_itimerval --- judger/uoj_judger/include/uoj_run.h | 19 +++++++++++++++++++ judger/uoj_judger/run/run_program.cpp | 10 +--------- judger/uoj_judger/run/run_program_sandbox.h | 2 -- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/judger/uoj_judger/include/uoj_run.h b/judger/uoj_judger/include/uoj_run.h index 25783b0..2316656 100644 --- a/judger/uoj_judger/include/uoj_run.h +++ b/judger/uoj_judger/include/uoj_run.h @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,6 +8,7 @@ #include #include #include +#include #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 { diff --git a/judger/uoj_judger/run/run_program.cpp b/judger/uoj_judger/run/run_program.cpp index 59309a1..567f97b 100644 --- a/judger/uoj_judger/run/run_program.cpp +++ b/judger/uoj_judger/run/run_program.cpp @@ -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); } diff --git a/judger/uoj_judger/run/run_program_sandbox.h b/judger/uoj_judger/run/run_program_sandbox.h index 7f47639..ff9ea6a 100644 --- a/judger/uoj_judger/run/run_program_sandbox.h +++ b/judger/uoj_judger/run/run_program_sandbox.h @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -15,7 +14,6 @@ #include #include #include -#include #include #include #include