Re: fork() and returning hash / strings to the parent



zentara wrote:
> On Thu, 30 Jun 2005 10:32:07 +0200, KoekemTN@xxxxxxxxxxxx (Tielman
> Koekemoer \) wrote:
>
>>I have some subroutines that return information. Since the each
>>subroutine takes a snapshot of data on networked servers that should
>>be compared, I want the subroutines to execute in the shortest time
>>possible - I'd like to get all the subroutines running at the same
>>time. I thought of using fork() but I have not seen any code that
>>shows me how a forked process returns data to the parent process. And
>
>>from what I've read in the Camel book, "everything else (except file
>
>>descriptors) is copied" to the child process when fork()ed, so passing
>>the reference ta a hash will not work. But what if the child was
>>generating hashes and wanted to pass it to the parent?
>>
>>Q: How can I pass info (hashes / strings) from a child process to a
>>parent when the child is done processing?
>
>
> When you want to fork and return information, it is a good place to
> use threads and threads:shared.
>
> If you are stuck with forking, you can
> use pipes (child writes to pipe, parent reads it)
> use sockets
> use shared memory segments (probably the fastest, but trickier)
> write to a database
> write to a temp file
>
[snip threads discussion]

If you are stuck with forking and need to do some of the above I would
switch to POE and let it handle the setting up of pipes and and the I/O.
Using its filters you can return arbitrarily structured data through
references (freeze/thaw model) or just simple strings (among others, or
write your own). POE::Wheel::Run will handle the fork, and you can pass
a subroutine for it to fork/exec just as you mentioned. POE has a
learning curve, a relatively steep one at that, but it will save you
time in the end.

http://poe.perl.org

Good luck,

http://danconia.org
.



Relevant Pages

  • Re: COBOLs Influence on C
    ... which is easily avoided by calling via literal. ... If the child ... there will be logic changes to the parent anyway. ... are handled in subroutines which need to be recompiled when laws or ...
    (comp.lang.cobol)
  • fork() and returning hash / strings to the parent - take2
    ... I have some subroutines that return information. ... shows me how a forked process returns data to the parent process. ... descriptors) is copied" to the child process when forked, ... sub build_hash; ...
    (perl.beginners)
  • fork() and returning hash / strings to the parent
    ... I have some subroutines that return information. ... shows me how a forked process returns data to the parent process. ... descriptors) is copied" to the child process when forked, ... sub build_hash; ...
    (perl.beginners)
  • Re: fork off several children processes
    ... executing along with all childrenX() subroutines, all in parallel, ... So should I have only *one* subroutine under the parent section, ... If you want to run N processes in parallel, you need to have N-1 forks. ...
    (comp.lang.perl.misc)
  • Re: pipe hangs
    ... > several child processes. ... > returns data to the parent. ... for the pipes created for them, but all the parent fds, including the ...
    (comp.unix.programmer)