duanju8431 2012-08-19 18:40
浏览 59
已采纳

使用超时处理程序从PHP调用Java

Problem: I am running java files via PHP that a user submits. It is possible for a java file to cause an infinite loop. How can I handle this in the php execution process?

Here is my code:

$proc = proc_open($javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);
// Check status here logic ?
proc_close($proc);

It currently waits for the process to finish, but I want it to timeout after 30 seconds or a minute. I tried set_time_limit(x), but that does not terminate java.exe.

My question is, is it possible for me to check the status of java.exe process after X seconds, then terminate if it is still running? Or, do I need to use a timeout functionality in java (i.e. have a main class that executes the user's submitted classes on a thread)

  • 写回答

1条回答 默认 最新

  • duanaiguang1960 2012-08-19 21:01
    关注

    Yes, it's possible. I don't think a java process is any different than any other process in this regard. See these links for unix exec with timeout and windows exec with timeout.

    I didn't write this code, but here it is copy-pasted in case the original disappears from the Internet:

    For unix:

    <?php
      function PsExecute($command, $timeout = 60, $sleep = 2) {
            // First, execute the process, get the process ID
    
            $pid = PsExec($command);
    
            if( $pid === false )
                return false;
    
            $cur = 0;
            // Second, loop for $timeout seconds checking if process is running
            while( $cur < $timeout ) {
                sleep($sleep);
                $cur += $sleep;
                // If process is no longer running, return true;
    
               echo "
     ---- $cur ------ 
    ";
    
                if( !PsExists($pid) )
                    return true; // Process must have exited, success!
            }
    
            // If process is still running after timeout, kill the process and return false
            PsKill($pid);
            return false;
        }
    
        function PsExec($commandJob) {
    
            $command = $commandJob.' > /dev/null 2>&1 & echo $!';
            exec($command ,$op);
            $pid = (int)$op[0];
    
            if($pid!="") return $pid;
    
            return false;
        }
    
        function PsExists($pid) {
    
            exec("ps ax | grep $pid 2>&1", $output);
    
            while( list(,$row) = each($output) ) {
    
                    $row_array = explode(" ", $row);
                    $check_pid = $row_array[0];
    
                    if($pid == $check_pid) {
                            return true;
                    }
    
            }
    
            return false;
        }
    
        function PsKill($pid) {
            exec("kill -9 $pid", $output);
        }
    

    For windows:

    <?php
    // pstools.inc.php
    
        function PsExecute($command, $timeout = 60, $sleep = 2) {
            // First, execute the process, get the process ID
            $pid = PsExec($command);
    
            if( $pid === false )
                return false;
    
            $cur = 0;
            // Second, loop for $timeout seconds checking if process is running
            while( $cur < $timeout ) {
                sleep($sleep);
                $cur += $sleep;
                // If process is no longer running, return true;
                if( !PsExists($pid) )
                    return true; // Process must have exited, success!
            }
    
            // If process is still running after timeout, kill the process and return false
            PsKill($pid);
            return false;
        }
    
        function PsExec($command) {
            exec( dirname(__FILE__). "\\psexec.exe -s -d $command  2>&1", $output);
    
            while( list(,$row) = each($output) ) {
                $found = stripos($row, 'with process ID ');
                if( $found )
                    return substr($row, $found, strlen($row)-$found-strlen('with process ID ')-1); // chop off last character '.' from line
            }
    
            return false;
        }
    
        function PsExists($pid) {
            exec( dirname(__FILE__). "\\pslist.exe $pid 2>&1", $output);
    
            while( list(,$row) = each($output) ) {
                $found = stristr($row, "process $pid was not found");
                if( $found !== false )
                    return false;
            }
    
            return true;
        }
    
        function PsKill($pid) {
            exec( dirname(__FILE__). "\\pskill.exe $pid", $output);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料