3

我正在开发一个在线判断类型系统,其中可能有 100 个左右不受信任的可执行文件将同时运行并评估相同的输入数据。

我希望每个可执行文件都限制在预定义资源池的 CPU、内存、磁盘空间等的相等份额。例如,如果资源池设置为机器 CPU 的 3/4、3 GB 内存、300 GB 磁盘,并且正在运行 2 个可执行文件,则每个将获得 3/8 的 CPU、1.5 GB 内存、150 GB的磁盘。如果另一个人加入,资源将被重新调整为三个相等的切片。这是为了防止恶意或有缺陷的可执行文件从他人那里窃取资源,并为每个人提供平等的资源。

理想情况下,我还希望可执行文件不局限于单一语言(例如,让用户使用他们喜欢的任何语言进行开发——C、C++、Java、Python 等)。

使用整个 VM 或类似 OpenVZ 的东西似乎有点过头了。是否有更轻量级的替代方案,基本上为每个可执行文件使用单独的进程,同时限制其资源,禁用网络访问、进程生成等?我正在寻找一个轻量级解决方案的部分原因是有相当多的输入数据——宁愿不必将其复制到每个可执行文件,而是让它们从共享内存中读取。

4

1 回答 1

0

也许为每个进程创建不同的用户ID,然后通过“ulimit”来限制它们就足够了。Debian 下 bash 的 ulimit 手册页:

ulimit [-HSTabcdefilmnpqrstuvx [limit]]

在允许这种控制的系统上,提供对 shell 可用资源和由它启动的进程的控制。-H 和 -S 选项指定为给定资源设置硬限制或软限制。硬限制一旦设置就不能由非 root 用户增加;软限制可以增加到硬限制的值。如果既没有指定 -H 也没有指定 -S,则同时设置软限制和硬限制。limit 的值可以是为资源指定的单位中的数字,也可以是特殊值 hard、soft 或 unlimited 之一,它们分别代表当前的硬限制、当前的软限制和无限制。如果省略limit,则打印资源软限制的当前值,除非给出-H 选项。当指定多个资源时,限制名称和单位将打印在值之前。其他选项解释如下:

          -a     All current limits are reported
          -b     The maximum socket buffer size
          -c     The maximum size of core files created
          -d     The maximum size of a process's data segment
          -e     The maximum scheduling priority ("nice")
          -f     The maximum size of files written by the shell and its children
          -i     The maximum number of pending signals
          -l     The maximum size that may be locked into memory
          -m     The **maximum resident set size** (many systems do not honor this limit)
          -n     The maximum number of open file descriptors (most systems do not allow this value to be set)
          -p     The pipe size in 512-byte blocks (this may not be set)
          -q     The maximum number of bytes in POSIX message queues
          -r     The maximum real-time scheduling priority
          -s     The maximum stack size
          -t     The maximum amount of cpu time in seconds
          -u     The maximum number of processes available to a single user
          -v     The maximum amount of virtual memory available to the shell and, on some systems, to its children
          -x     The maximum number of file locks
          -T     The **maximum number of threads**

I think that limiting the threads to 1 you can get a fair distribution of CPU computation time.

Limit also the maximum RSS.

于 2013-04-10T22:00:11.067 回答