我目前正在开发一个在线 C/C++/汇编编译器,我偶然发现了一个名为 libsandbox 的好软件。这使我能够运行在线编写的代码,对其进行编译并拦截系统调用(如果已发出)。
首先,我是 Linux 环境的新手。我已经下载了 tar.gz,解压缩,配置并安装它。这运行没有任何错误,但现在我很难运行它。我应该如何在此沙盒中运行 C/C++ 程序?我必须向它提供 .c/.cpp 文件吗?编译后的可执行文件?
这可能是一个非常愚蠢的问题。我在互联网上搜索了如何执行此操作,并阅读了包含的自述文件,但他们没有给我任何线索。
提前致谢!
最佳答案
沙箱仅适用于 linux。您必须首先使用库函数实际创建沙箱,然后告诉沙箱运行您的程序。
此 python 示例展示了如何从 python 执行此操作。 “#targeted program”行显示您将在何处指定实际应用程序的名称。
def main(args):
# sandbox configuration
cookbook = {
'args': args[1:], # targeted program
'stdin': sys.stdin, # input to targeted program
'stdout': sys.stdout, # output from targeted program
'stderr': sys.stderr, # error from targeted program
'quota': dict(wallclock = 30000,# 30 sec
cpu = 2000, # 2 sec
memory = 8388608, # 8 MB
disk = 1048576)} # 1 MB
# create a sandbox instance and execute till end
msb = MiniSandbox(**cookbook)
msb.run()
# verbose statistics
sys.stderr.write("result: %(result)s\ncpu: %(cpu)dms\nmem: %(mem)dkB\n" % \
msb.probe())
return os.EX_OK
我建议转到 libsandbox 下载页面并在那里获取完整的 sample2.py 文件,然后使用 python 脚本运行沙箱。这比让 C++ 或 C 程序为您做这件事更容易。
所以...
编写您的 C 或 C++ 程序。不要将它链接到 LIBSANDBOX。
确保您安装了 python。
从 libsandbox 页面运行示例 python 脚本。
python 脚本将为您加载 libsandbox。然后它将运行您在沙箱中构建的程序。
简单。
关于c++ - 运行 libsandbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10861211/