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

Recommended Answers

All 16 Replies

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?

Of course not. If I knew how to pass info from one process to another, I would not have asked the question!

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?

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

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

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.

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

You're welcome.

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

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.

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

Hoppy

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.