while using proc_open(), my time stamp to terminate the function is not working properly

36 Views Asked by At

I am having issues with my php code. I am executing c++rom fffff code using proc_open() in php.

//from another function I'm calling this function with parameters execute("code.exe", $STDIN, 0.02, 268435456 * 2);
//my c++ code is already executed to .exe file.

function execute($cmd, $stdin, $timeout, $memoryout)
{
    $descriptorspec = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']];
    $timer = microtime(true);
    $TL = false;
    $ML = 0;
    $stdout = '';

    $process = proc_open($cmd, $descriptorspec, $pipes);


    fwrite($pipes[0], $stdin);
    fclose($pipes[0]);

    while(!feof($pipes[1]))
    {
        if($timer + $timeout < microtime(true))
        {
            $TL = true;
            break;
        }

        $ML = max($ML, memory_get_usage());

        $stdout .= fread($pipes[1], 1);// . "<br/>";

        if($timer + $timeout < microtime(true))
        {
            $TL = true;
            break;
        }
    }

    $status = proc_get_status($process);

    if(!$status['running'])
        if($status['exitcode'] !== 0) $stdout = 'code:RE';
    if($TL) $stdout = 'code:TL';
    if($ML > $memoryout) $stdout = 'code:ML';

    $pid = proc_get_status($process)['pid'];
    strstr(PHP_OS, 'WIN') ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid");

    fclose($pipes[1]);
    proc_close($process);

    return $stdout;
}

So, when my c++ code is correctly written, there is no problem, but when i've written something like "while(1);" in my c++ code, my php code does not terminate. while(!feof($pipes[1])) cycle loops forever and I don't know what to do.

I've tried to use getrusage() instead of time stamp but it was worse.

0

There are 0 best solutions below