943,632 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2514
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 18th, 2008
0

Re: How to pass information to child processes

Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Nov 18th, 2008
0

Re: How to pass information to child processes

You're welcome.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 20th, 2008
0

Re: How to pass information to child processes

Thanks to digital-ether, I found this piece of code:

PHP Syntax (Toggle Plain Text)
  1. // PHP supports fork/exec/wait but not pipe. However, it does
  2. // support socketpair, which can do everything pipes can as well
  3. // as bidirectional communication. The original recipes have been
  4. // modified here to use socketpair only.
  5.  
  6. // -----------------------------
  7.  
  8. // pipe1 - use socketpair and fork so parent can send to child
  9. $sockets = array();
  10. if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) {
  11. die(socket_strerror(socket_last_error()));
  12. }
  13. list($reader, $writer) = $sockets;
  14.  
  15. $pid = pcntl_fork();
  16. if ($pid == -1) {
  17. die('cannot fork');
  18. } elseif ($pid) {
  19. socket_close($reader);
  20. $line = sprintf("Parent Pid %d is sending this\n", getmypid());
  21. if (!socket_write($writer, $line, strlen($line))) {
  22. socket_close($writer);
  23. die(socket_strerror(socket_last_error()));
  24. }
  25. socket_close($writer);
  26. pcntl_waitpid($pid, $status);
  27. } else {
  28. socket_close($writer);
  29. $line = socket_read($reader, 1024, PHP_NORMAL_READ);
  30. printf("Child Pid %d just read this: `%s'\n", getmypid(), rtrim($line));
  31. socket_close($reader); // this will happen anyway
  32. exit(0);
  33. }
  34.  
  35. 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
Last edited by hopalongcassidy; Nov 20th, 2008 at 3:27 pm. Reason: Solved
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Nov 20th, 2008
0

Re: How to pass information to child processes

Thanks to digital-ether, I found this piece of code:

PHP Syntax (Toggle Plain Text)
  1. // PHP supports fork/exec/wait but not pipe. However, it does
  2. // support socketpair, which can do everything pipes can as well
  3. // as bidirectional communication. The original recipes have been
  4. // modified here to use socketpair only.
  5.  
  6. // -----------------------------
  7.  
  8. // pipe1 - use socketpair and fork so parent can send to child
  9. $sockets = array();
  10. if (!socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets)) {
  11. die(socket_strerror(socket_last_error()));
  12. }
  13. list($reader, $writer) = $sockets;
  14.  
  15. $pid = pcntl_fork();
  16. if ($pid == -1) {
  17. die('cannot fork');
  18. } elseif ($pid) {
  19. socket_close($reader);
  20. $line = sprintf("Parent Pid %d is sending this\n", getmypid());
  21. if (!socket_write($writer, $line, strlen($line))) {
  22. socket_close($writer);
  23. die(socket_strerror(socket_last_error()));
  24. }
  25. socket_close($writer);
  26. pcntl_waitpid($pid, $status);
  27. } else {
  28. socket_close($writer);
  29. $line = socket_read($reader, 1024, PHP_NORMAL_READ);
  30. printf("Child Pid %d just read this: `%s'\n", getmypid(), rtrim($line));
  31. socket_close($reader); // this will happen anyway
  32. exit(0);
  33. }
  34.  
  35. 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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 21st, 2008
0

Re: How to pass information to child processes

hi hopalongcassidy,

i think this is usefull to you some how i think http://www.phpeasystep.com/phptu/29.html
Reputation Points: 10
Solved Threads: 1
Newbie Poster
punjabivirsa3 is offline Offline
11 posts
since Nov 2008
Nov 21st, 2008
0

Re: How to pass information to child processes

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

Hoppy
Reputation Points: 53
Solved Threads: 13
Junior Poster
hopalongcassidy is offline Offline
148 posts
since Oct 2007
Nov 22nd, 2008
0

Re: How to pass information to child processes

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
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Connection problem
Next Thread in PHP Forum Timeline: checkbox array storage and retrieval





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC