0

I've created two programs in C. The first gets a number and prints the double value of it and the second prints the quadruple.
I want to execute them through PHP. I've done it using proc_open and it works fine if I execute only one of the programs each time.
I have to give a number to the first program and pass its output as input to the second program. When though I use two proc_open to create the two processes,the whole thing doesn't work. What I want to do is something like this:

$process1 = proc_open($command_exe1, $descriptorspec1, $pipes1, $cwd);
$process2 = proc_open($command_exe2, $descriptorspec2, $pipes2, $cwd);
fwrite($pipes1[0], $posted);
fwrite($pipes2[0], $pipes1[1]);
fclose($pipes1[0]);
fclose($pipes2[0]);
while(!feof($pipes1[1])) {
                $StdOut1 = stream_get_contents($pipes1[1]);
                }
        echo $StdOut1;
while(!feof($pipes2[1])) {
                $StdOut2 = stream_get_contents($pipes2[1]);
                }
        echo $StdOut2;
fclose($pipes1[1]);
fclose($pipes2[1]);

proc_close($process1);
proc_close($process2);

I know that it's a wrong way of doing it but I can't think of anything else so...any help would be welcome. Note: I'm working on Windows.

georgia
  • 45
  • 1
  • 7
  • nitpick: there's no such thing as a "c script". you've also got a typo in your second proc_open call, missing ,` after the first argument. – Marc B Jul 17 '13 at 20:41
  • I mean C codes, simple C programs. As for the ',' and the ';' at the end of the lines it's probably a mistake during the copy. – georgia Jul 17 '13 at 20:51

1 Answers1

0

If process can run separately one after another

You can try put "in-steps",

/** step 1*/
$process1 = proc_open($command_exe1, $descriptorspec1, $pipes1, $cwd)
...
while(!feof($pipes1[1])) {
                $StdOut1 = stream_get_contents($pipes1[1]);
                }
        echo $StdOut1;


/** step 2*/

$process2 = proc_open($command_exe2 $descriptorspec2, $pipes2, $cwd)
while(!feof($pipes2[1])) {

...

  • the problem is that if I want to have 2 inputs and outputs in each program that doesn't work. I mean that each code could get 2 numbers and print their double/duadruple. – georgia Jul 17 '13 at 20:54