My problem is that I have a PHP application that needs to (in Windowsese) multi-task. I am relatively new to PHP and equally new to Linux. I need to know how to pass information from a parent process to a child process and vice versa. If somebody could please provide an example, it would be very much appreciated.
Hoppy
Do you have some code you're currently working with? Could you post this if possible?
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Of course not. If I knew how to pass info from one process to another, I would not have asked the question!
The reason to post code was to clarify your question. It was incredibly vague. In what context are you passing information between processes?
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
I read the URL you provided and especially the user contributed notes that followed. It appears to me that the folks that designed the process control portion of PHP (pcntl) were rank amateurs with little real world experience.
Using pcntl_exec after forking really misses the mark. It has no way to pass anything back to the parent, not even a return code. :(
Excellent, then write your own extension to PHP to implement process control the way it should be handled. It's open source, don't complain unless you have something meaningful to contribute as a retort.
ShawnCplus
Code Monkey
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Thanks to digital-ether, I found this piece of code:
// PHP supports fork/exec/wait but not pipe. However, it does
// support socketpair, which can do everything pipes can as well
// as bidirectional communication. The original recipes have been
// modified here to use socketpair only.
// -----------------------------
// pipe1 - use socketpair and fork so parent can send to child
$sockets = array();
if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) {
die(socket_strerror(socket_last_error()));
}
list($reader, $writer) = $sockets;
$pid = pcntl_fork();
if ($pid == -1) {
die('cannot fork');
} elseif ($pid) {
socket_close($reader);
$line = sprintf("Parent Pid %d is sending this\n", getmypid());
if (!socket_write($writer, $line, strlen($line))) {
socket_close($writer);
die(socket_strerror(socket_last_error()));
}
socket_close($writer);
pcntl_waitpid($pid, $status);
} else {
socket_close($writer);
$line = socket_read($reader, 1024, PHP_NORMAL_READ);
printf("Child Pid %d just read this: `%s'\n", getmypid(), rtrim($line));
socket_close($reader); // this will happen anyway
exit(0);
}
This is so simple, a child could do it. To communicate in both directions, you can simply not close the pipe that sends data from the child to the parent until the child is finished (after the pcntl_waitpid).
Thanks again, digital-ether. :) By the way, how do I mark this thread as solved?
Hoppy
Glad that worked for you.
Right at the bottom of the thread, beside the "reply" button you should see the link to mark the thread as solved.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
At the bottom of this thread are two buttons; one says "REPLY W/ QUOTE" and the other says "MULTIQUOTE".
Hoppy
Do a search for "Mark as Solved" on the page and see if you find it.
ie: Ctr+F
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101