0

I am executing javaw with an infinite looping java file using this:

$descriptorspec = array(
                                0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
                                1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
                                2 => array("pipe", "w") // stderr is a file to write to
                        );

 // e.x. $javaCmd = "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 > /dev/null 2>&1 < h.in"
$proc = proc_open($javaCmd, $descriptorspec, $pipes);//, $cwd, $env);
 stream_set_blocking($pipes[0], 0) ;

 $status = proc_get_status($proc);  
 var_dump($status);
  $timeOut = 5;
  $currentSecond = 0;

  while( $currentSecond < $timeOut ) {
        echo '<br/>';
        sleep(1);
        $currentSecond = $currentSecond +1;
        if( ! $status["running"] )
        {
            echo 'process exited before timing out'; // Process must have exited, success!
            return;
        }
        $status = proc_get_status($proc);
        var_dump($status);
  } // end while
  if($currentSecond  == $timeOut)
  {
      // kill KILL KILL!
      exec("taskkill /PID ".$status['pid']);
  }

On the first call to proc_get_status, running attribute returns true. On the second call (one second later) to proc_get_status, running returns false. The application javaw.exe is still running, however ( I call proc_get_status in a while loop that will eventually timeout.)

My goal is to taskkill the program after the timeout has expired. See a similar question here. I am running on Win7 64 bit, PHP 5.3

Var dump on $status: (Note; I tried applying stream_set_blocking($pipes[0], 0) ;, same issue)

Before entering timeout loop:

    array(8) { 
["command"]=> string(157) "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 /dev/null 2>&1 < h.in" 
["pid"]=> int(3264) 
["running"]=> bool(true) 
["signaled"]=> bool(false) 
["stopped"]=> bool(false) 
["exitcode"]=> int(-1) 
["termsig"]=> int(0) 
["stopsig"]=> int(0) 
} 

After first iteration/sleep(1):

    array(8) { 
["command"]=> string(157) "java -cp "../Fully Diversified Sequences of Sets" SuperEasyProblemSolution2 /dev/null 2>&1 < h.in" 
["pid"]=> int(3264) 
["running"]=> bool(false) 
["signaled"]=> bool(false) 
["stopped"]=> bool(false) 
["exitcode"]=> int(1) 
["termsig"]=> int(0) 
["stopsig"]=> int(0) 
} 
    process exited before timing out

After testing, it appears that $status['pid'] is different than the pid for javaw.exe under Windows' Resource Monitor.

Community
  • 1
  • 1
Gaʀʀʏ
  • 4,372
  • 3
  • 39
  • 59

3 Answers3

1

If the Java app is running as a daemon, then it would have a different PID.

Does the Java app attempt to output anything? If so - then you aren't reading the pipe so the script will eventually fill up the buffer and hang (which might cause Java to stop running)

If you don't care about the output then send it to /dev/null to get rid of it:

/cmd/to/run > /dev/null 2>&1

If you choose to read the data from the pipe, the read function will block if there is no data - to get around that you might want to use:

stream_set_blocking($pipes[0], 0)

This will turn off blocking mode (the read function will return FALSE immediately if there is no data waiting).

vqdave
  • 2,361
  • 1
  • 18
  • 36
msEmmaMays
  • 1,073
  • 7
  • 7
  • I do not care about the output- I only want to make sure it runs within a certain time limit. I tried using the `> /dev/null` and `stream_set_blocking($pipes[0], 0)`, but neither worked. I am running on windows, and it is possible it is running as a daemon. Would that make me SOL? – Gaʀʀʏ Aug 30 '12 at 20:47
  • What if you put your code in a .bat file and called that from the command line – msEmmaMays Aug 30 '12 at 21:55
  • I use linux so I'm guessing how windows handles it - but perhaps it's returning the status of the redirect pipe (< h.in) instead of the javaw process. (it's done running the "< h.in" redirect part so running=false) – msEmmaMays Aug 30 '12 at 22:02
  • Im not sure what you mean by putting the code in the .bat file --> the "javaw -cp ..." command? I tried that- it executed normally but completed, and javaw continued running... It is possible that it finishes reading in the < h.in file to supply to java, however, in my test java file, i simply have a while(true), not reading any inputs. Perhaps I need both stdin and stdout pipes to block ? Unfortunately, I need to include an "in" file every time, as I am running test cases against a user solution. – Gaʀʀʏ Aug 30 '12 at 22:06
  • Gonna try `if (file_exists("/proc/$pid")) { // process still running }` as an alternative to `proc_get_status` – Gaʀʀʏ Aug 30 '12 at 22:31
  • That won't work - /proc/ is only available on linux - If you are using windows that might be a big source of your problems. Every example you find will be for linux. ----------- Wait I just figured it out - see my new answer – msEmmaMays Aug 30 '12 at 22:45
  • My bad, I meant, I am going to try the /proc/$pid, on the server (linux). Seems to be getting me somewhere – Gaʀʀʏ Aug 30 '12 at 22:59
  • see my new answer on the question- you should be using 'java' not 'javaw' – msEmmaMays Aug 30 '12 at 23:01
  • Using the file_exists method worked! I will award you the bounty, but I am going to post another answer with the alternative method I used – Gaʀʀʏ Aug 30 '12 at 23:04
1

Although we couldnt pinpoint the issue with proc_get_status returning the incorrect boolean, we were able to find a good work-around (for Unix), using

if (file_exists("/proc/".$status["pid"])) { // process still running }

to check if the process was still running or not.

Here is the full code excerpt:

$proc = proc_open($javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);

$status = proc_get_status($proc);

if($status["pid"] === false)
{
    // Process did not execute correctly
}
else
{
        $timeOut = 0;
        $forceKill = true;

        while($timeOut < $timeLimit)
        {
            $timeOut++;
            sleep(1);
            echo 'Timeout is at '.$timeOut.' and timelimit: '.$timeLimit.'<br/>';

            if (file_exists("/proc/".$status["pid"])) {
            // process still running
            }
            else
            {
                echo 'Finished running after '.$timeOut.' seconds<br/>';
                $forceKill = false;
                break;
            }

        }
        if($forceKill == true)
        {
            echo ' Manual killing pid '.$status['pid'];
            exec("sudo kill ".$status['pid']);
            $runTime = $timeLimit;
        }


}
Gaʀʀʏ
  • 4,372
  • 3
  • 39
  • 59
0

Since you mentioned you are doing this on windows I realized what is happening-

javaw (with a W) starts it as a windows app and returns immediately (which is why running=false)

you need to use java (without the w) to run it in console.

java = console application
javaw = windows application with no console
msEmmaMays
  • 1,073
  • 7
  • 7
  • I experienced the issue with both java, and javaw (I was first using java, then tried javaw, and Im back on java). I am having some success checking the /proc/$pid, so I may end up giving up on a windows solution and stick to unix. – Gaʀʀʏ Aug 30 '12 at 23:01