点击查看【music163】
这篇就不多写了,一会晚上又要回杭州了。
今天主要是在原有的架构上,把关键的两个判题机需要的函数试了一下,确保之后用起来没问题。
Linux系统函数的调用这块,Rust主要通过一些对C的FFI的依赖实现,我这边用到了**nix****libc**

系统函数

rlimit
判题机给选手程序设定的限制主要有两块,一是系统资源,二是系统调用。
rlimit的作用就是对系统资源设限,限制系统调用用的是seccomp(这个我之前就写在框架里,就不细说了)。
具体怎么通过nix去使用rlimit可以参考nix::sys::resource
rlimit的设定发生在**runner**当中,在调起选手程序之前。

  1. setrlimit(
  2. RLIMIT_AS,
  3. Some(1024 * 1024 * 1024),
  4. Some(1024 * 1024 * 1024),
  5. )
  6. .unwrap();
  7. setrlimit(RLIMIT_CPU, Some(6), Some(6)).unwrap();

以上代码展示的是为选手程序设置1个G(102410241024Byte)的虚拟内存,和6秒的执行时间。
wait4
wait4的主要作用是采集判题进程执行之后的退出状态和资源使用情况。

  1. let mut status: c_int = 0;
  2. let mut usage: rusage = get_default_rusage();
  3. unsafe {
  4. wait4(child.as_raw() as i32, &mut status, WSTOPPED, &mut usage);
  5. }

[1] WSTOPPED [2] libc::wait4
我们通过给wait4传两个可变引用来获取进程的状态和资源使用情况。
具体wait4每个参数是什么意义可以看这篇文章《进程调度之5:系统调用exit与wait4》

进展

今天主要是在测试里能打印出来监测结果了,之后就是要把这些信息整合好了再返回。

  1. Continuing execution in parent process, new child has pid: 2981
  2. I'm a new child process
  3. timeout_killer has been set
  4. 1s has passed..
  5. 2s has passed..
  6. 3s has passed..
  7. 4s has passed..
  8. killed process by killer
  9. Detected process exit
  10. 9
  11. rusage { ru_utime: timeval { tv_sec: 3, tv_usec: 139625 }, ru_stime: timeval { tv_sec: 1, tv_usec: 859778 }, ru_maxrss: 1516, ru_ixrss: 0, ru_idrss: 0, ru_isrss: 0, ru_minflt: 75, ru_majflt: 0, ru_nswap: 0, ru_inblock: 0, ru_oublock: 0, ru_msgsnd: 0, ru_msgrcv: 0, ru_nsignals: 0, ru_nvcsw: 1, ru_nivcsw: 43 }

下个阶段主要就是选手程序的I/O,以及判题核心自身的入参和返回值了。