php - 如何对 `exec()` 的调用施加时间限制?

标签 php linux

我正在尝试用 PHP 编写在线法官。我的代码目前看起来像:

exec("gcc /var/www/qwerty.c -o /var/www/binary",$output,$returnval);
print_r($output);
exec("cat /var/www/qwe.txt | /var/www/binary",$output,$returnval);
print_r($output);

但是,我希望 exec 产生的每个进程最多有 1 秒的运行时间。我不知道该怎么做。 set_time_limit() 不工作

最佳答案

我会使用 proc_按照@Adam Wright 的建议运行,但是类 Linux 环境的快速替代方法是在命令之前使用 GNU timeout 命令:

// If it took more than 1 second, $returnval will got the exit code 124
exec('timeout 1 gcc /var/www/qwerty.c -o /var/www/binary', $output, $returnval);

关于php - 如何对 `exec()` 的调用施加时间限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9128938/

相关文章:

c++ - 依赖于同一个静态链接库的可执行文件和共享库

php - 使用 Symfony DomCrawler 选择文本节点

php - Doctrine findBy 或语句

php - 在 Mac 上通过 PECL 安装 PHP imagick

php - Zendframework 2 使用静态方法调用服务

linux - mod-mono-server4 和 FileNotFoundException

linux - 更改登录 shell 时出现问题

Java - 即使文件不可读,file.length() 也会返回

linux - 如何在ubuntu中手动选择音频输出设备?

php - 如何使用php将多行数据插入Mysql DB