note 102239 added to function.proc-open
| From: | mattis at xait dot no | Date: | Thu, 03 Feb 2011 15:41:23 +0000 |
| Subject: | note 102239 added to function.proc-open | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176648@lists.php.net to get a copy of this message | ||
If you are, like me, tired of the buggy way proc_open handles streams and exit codes; this example
demonstrate the power of pcntl, posix and some simple output redirection:
<?php
$outpipe = '/tmp/outpipe';
$inpipe = '/tmp/inpipe';
posix_mkfifo($inpipe, 0600);
posix_mkfifo($outpipe, 0600);
$pid = pcntl_fork();
//parent
if($pid) {
$in = fopen($inpipe, 'w');
fwrite($in, "A message for the inpipe reader\n");
fclose($in);
$out = fopen($outpipe, 'r');
while(!feof($out)) {
echo "From out pipe: " . fgets($out) . PHP_EOL;
}
fclose($out);
pcntl_waitpid($pid, $status);
if(pcntl_wifexited($status)) {
echo "Reliable exit code: " . pcntl_wexitstatus($status) . PHP_EOL;
}
unlink($outpipe);
unlink($inpipe);
}
//child
else {
//parent
if($pid = pcntl_fork()) {
pcntl_exec('/bin/sh', array('-c', "printf 'A message for the outpipe
reader' > $outpipe 2>&1 && exit 12"));
}
//child
else {
pcntl_exec('/bin/sh', array('-c', "printf 'From in pipe: '; cat
$inpipe"));
}
}
?>
Output:
From in pipe: A message for the inpipe reader
From out pipe: A message for the outpipe reader
Reliable exit code: 12
----
Server IP: 85.19.212.230
Probable Submitter: 212.33.136.212
----
Manual Page -- http://www.php.net/manual/en/function.proc-open.php
Edit -- https://master.php.net/note/edit/102239
Del: integrated -- https://master.php.net/note/delete/102239/integrated
Del: useless -- https://master.php.net/note/delete/102239/useless
Del: bad code -- https://master.php.net/note/delete/102239/bad+code
Del: spam -- https://master.php.net/note/delete/102239/spam
Del: non-english -- https://master.php.net/note/delete/102239/non-english
Del: in docs -- https://master.php.net/note/delete/102239/in+docs
Del: other reasons-- https://master.php.net/note/delete/102239
Reject -- https://master.php.net/note/reject/102239
Search -- https://master.php.net/manage/user-notes.php