How to pass information to child processes

Thread Solved

Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: How to pass information to child processes

 
0
  #11
Nov 18th, 2008
Thank you digital-ether, this looks like information I can use.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,085
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to pass information to child processes

 
0
  #12
Nov 18th, 2008
You're welcome.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: How to pass information to child processes

 
0
  #13
Nov 20th, 2008
Thanks to digital-ether, I found this piece of code:

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,085
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to pass information to child processes

 
0
  #14
Nov 20th, 2008
Originally Posted by hopalongcassidy View Post
Thanks to digital-ether, I found this piece of code:

  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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: punjabivirsa3 is an unknown quantity at this point 
Solved Threads: 1
punjabivirsa3 punjabivirsa3 is offline Offline
Newbie Poster

Re: How to pass information to child processes

 
0
  #15
Nov 21st, 2008
hi hopalongcassidy,

i think this is usefull to you some how i think http://www.phpeasystep.com/phptu/29.html
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 147
Reputation: hopalongcassidy is an unknown quantity at this point 
Solved Threads: 13
hopalongcassidy's Avatar
hopalongcassidy hopalongcassidy is offline Offline
Junior Poster

Re: How to pass information to child processes

 
0
  #16
Nov 21st, 2008
At the bottom of this thread are two buttons; one says "REPLY W/ QUOTE" and the other says "MULTIQUOTE".

Hoppy
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,085
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: How to pass information to child processes

 
0
  #17
Nov 22nd, 2008
Originally Posted by hopalongcassidy View Post
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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 1788 | Replies: 16
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC