2

I use proc_open to execute a program created by c language.

I was using file for the "stdout".

$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("file", "/tmp/example.output"),
   2 => array("file", "/tmp/example.error", "a")
);

Everything is fine when I was executing good program but problem occurred when I was executing infinite loop program like the code below :

#include "stdio.h"

int main(){
    while(1){
        printf("Example");
    }
    return 0
}

File example.output will make my hard disk full. So I need to delete the file and restart my computer. My question is how to handle something like this?

Thanks :)

Sachin D
  • 1,370
  • 7
  • 29
  • 42

1 Answers1

0

The only thing you can do is slaughter the offending process without prejudice using proc_terminate (but you can be polite and allow it to run for a while first, in effect imposing a time limit for it to complete).

For example:

$proc = proc_open(...);
sleep(20); // give the process some time to run
$status = proc_get_status($proc); // see what it's doing
if($status['running']) {
    proc_terminate($proc); // kill it forcefully
}

Don't forget to clean up any handles you still have in your hands afterwards.

Jon
  • 428,835
  • 81
  • 738
  • 806