mirror of
https://github.com/renbaoshuo/S2OJ.git
synced 2024-11-08 14:18:40 +00:00
fix(judger): 完善浮点数时限功能
ref: 986c7aa4ed
Co-authored-by: Kaifeng Lyu <vfleaking@163.com>
This commit is contained in:
parent
d7aea4cbbc
commit
2e53a2733f
@ -22,6 +22,11 @@
|
||||
std::string escapeshellarg(int arg) {
|
||||
return std::to_string(arg);
|
||||
}
|
||||
std::string escapeshellarg(double arg) {
|
||||
std::ostringstream sout;
|
||||
sout << std::setprecision(15) << arg;
|
||||
return sout.str();
|
||||
}
|
||||
std::string escapeshellarg(const std::string &arg) {
|
||||
std::string res = "'";
|
||||
for (char c : arg) {
|
||||
@ -391,21 +396,30 @@ namespace runp {
|
||||
}
|
||||
};
|
||||
|
||||
itimerval double_to_itimerval(const double &tl) {
|
||||
struct itimerval val;
|
||||
/**
|
||||
* @brief convert a time t to timeval. Assume t <= 1000. Accurate to ms.
|
||||
*
|
||||
* @param t time in double
|
||||
* @return timeval
|
||||
*/
|
||||
timeval double_to_timeval(const double &t) {
|
||||
long tl = round(t * 1000);
|
||||
long tl_sec = tl / 1000;
|
||||
long tl_usec = tl % 1000 * 1000;
|
||||
return {tl_sec, tl_usec};
|
||||
}
|
||||
|
||||
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;
|
||||
/**
|
||||
* @brief convert a time t to timespec. Assume t <= 1000. Accurate to ms.
|
||||
*
|
||||
* @param t time in double
|
||||
* @return timespec
|
||||
*/
|
||||
timespec double_to_timespec(const double &t) {
|
||||
long tl = round(t * 1000);
|
||||
long tl_sec = tl / 1000;
|
||||
long tl_nsec = tl % 1000 * 1'000'000;
|
||||
return {tl_sec, tl_nsec};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,10 +51,10 @@ error_t run_program_argp_parse_opt (int key, char *arg, struct argp_state *state
|
||||
|
||||
switch (key) {
|
||||
case 'T':
|
||||
config->limits.time = stod(arg);
|
||||
config->limits.time = round(stod(arg) * 1000) / 1000;
|
||||
break;
|
||||
case 'R':
|
||||
config->limits.real_time = stod(arg);
|
||||
config->limits.real_time = round(stod(arg) * 1000) / 1000;
|
||||
break;
|
||||
case 'M':
|
||||
config->limits.memory = atoi(arg);
|
||||
@ -202,7 +202,16 @@ void set_limit(int r, int rcur, int rmax = -1) {
|
||||
}
|
||||
|
||||
void set_user_cpu_time_limit(double tl) {
|
||||
itimerval val = runp::double_to_itimerval(tl);
|
||||
itimerval val;
|
||||
val.it_value = runp::double_to_timeval(tl);
|
||||
val.it_interval = {0, 100'000};
|
||||
|
||||
val.it_value.tv_usec += 100'000;
|
||||
if (val.it_value.tv_usec >= 1'000'000) {
|
||||
val.it_value.tv_sec++;
|
||||
val.it_value.tv_usec -= 1'000'000;
|
||||
}
|
||||
|
||||
setitimer(ITIMER_VIRTUAL, &val, NULL);
|
||||
}
|
||||
|
||||
@ -302,7 +311,7 @@ struct rusage *ruse0p = NULL;
|
||||
bool has_real_TLE() {
|
||||
struct timeval elapsed;
|
||||
timersub(&end_time, &start_time, &elapsed);
|
||||
return elapsed.tv_sec >= run_program_config.limits.real_time;
|
||||
return elapsed.tv_sec + elapsed.tv_usec / 1'000'000. >= run_program_config.limits.real_time;
|
||||
}
|
||||
|
||||
int rp_children_pos(pid_t pid) {
|
||||
@ -628,9 +637,12 @@ void dispatch_event(run_event&& e) {
|
||||
if (rp_timer_pid == -1) {
|
||||
runp::result(runp::RS_JGF, "error code: FKFAL2").dump_and_exit(); // fork failed
|
||||
} else if (rp_timer_pid == 0) {
|
||||
struct timespec ts;
|
||||
ts.tv_sec = run_program_config.limits.real_time;
|
||||
ts.tv_nsec = 100 * 1000000;
|
||||
struct timespec ts = runp::double_to_timespec(run_program_config.limits.real_time);
|
||||
ts.tv_nsec += 100'000'000;
|
||||
if (ts.tv_nsec >= 1'000'000'000) {
|
||||
ts.tv_sec += 1;
|
||||
ts.tv_nsec -= 1'000'000'000;
|
||||
}
|
||||
nanosleep(&ts, NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user