954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to pass information to child processes

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

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

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
Moderator
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!

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 
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
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

My question is not the least bit vague. It is perfectly clear. Let's say, for example, that I had a parent process whose job it was to read a file containing URLs. And that each child process was supposed to read the contents of a given URL and save it to a disc file.
In this case, I would want the parent to create a child for each URL and pass the URL to the child which would in turn read its contents and store it in a file on disc. Perhaps, the child would return to the parent, the name of the file in which the contents of the URL are saved.

What I am looking for is the PHP mechanism by which the URL can be passed to the child and the name of the file in which the contents of the URL is stored can be returned to the parent.

Something like SendData($parentid, $childid, &dataaddress, datalen);

Is that any clearer?

Hoppy

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

The only current way to do anything of that sort is on execution of a child process with http://us.php.net/manual/en/function.pcntl-exec.php

ShawnCplus
Code Monkey
Team Colleague
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. :(

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

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
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Good idea. Maybe I will. ;)

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

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. :(

Take a look at: http://pleac.sourceforge.net/pleac_php/processmanagementetc.html
http://www.php.net/proc_open
http://www.php.net/socket_create_pair
http://www.php.net/sem

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 
hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

You're welcome.

digital-ether
Nearly a Posting Virtuoso
Moderator
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

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

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
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

hi hopalongcassidy,

i think this is usefull to you some how i think http://www.phpeasystep.com/phptu/29.html

punjabivirsa3
Newbie Poster
11 posts since Nov 2008
Reputation Points: 10
Solved Threads: 1
 

At the bottom of this thread are two buttons; one says "REPLY W/ QUOTE" and the other says "MULTIQUOTE".

Hoppy

hopalongcassidy
Junior Poster
148 posts since Oct 2007
Reputation Points: 53
Solved Threads: 13
 

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
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You