SPUG: Forked Children talking back to the parent?
Richard Anderson
starfire at zipcon.net
Wed Jan 17 11:19:06 CST 2001
It might be simpler to use shared memory and a semaphore as opposed to
managing 10 sockets.
Richard.Anderson at unixscripts.com RayCosoft, LLC
Perl/Java/Oracle/Unix software engineering www.unixscripts.com
www.zipcon.net/~starfire/home Seattle, WA, USA
----- Original Message -----
From: "Richard Wood" <wildwood_players at yahoo.com>
To: "John Cokos" <jcokos at ccs.net>; <spug-list at pm.org>
Sent: Wednesday, January 17, 2001 8:28 AM
Subject: Re: SPUG: Forked Children talking back to the parent?
> John,
>
> I do something very similar to this, and I still have
> a dilemma with it. What I do is to find out how many
> processes I need to fork, then I set up bidirectional
> socket-pairs to use between the parent and children,
> then I fork the children. The parent then goes into a
> loop of sending messages to every child followed by a
> loop of waiting for a response from every child prior
> to going on to the next message. The parent is in an
> outer loop of looking for and processing scripts which
> may or may not have commands for every child process.
> If a child process is not supposed to receive a
> command, I just send it a DUMMY command which it echos
> back.
>
> The children are in a loop waiting for messages from
> the parent upon which they take action and report back
> to the parent.
>
> The part I haven't worked out is what to do if a child
> hangs. Hopefully, other responses to your email will
> give me some ideas on that.
>
> This is related to your situation since you add
> processes along the way. Well, maybe this will help
> and maybe not.
>
> ####################################################
> # estab_socket();
> ####################################################
>
> sub estab_socket {
> my $site = shift;
> my $pSoxR = shift;
> my $cSoxR = shift;
>
> my $Parent = "Parent"."$site";
> my $Child = "Child"."$site";
>
> socketpair($Child, $Parent, AF_UNIX, SOCK_STREAM,
>
> PF_UNSPEC)
> or die "socketpair: $!";
>
> $Parent->autoflush(1);
> $Child->autoflush(1);
>
> $$pSoxR[$site] = \*$Parent;
> $$cSoxR[$site] = \*$Child;
> }
>
> Regards,
>
> Rich Wood
>
>
>
> --- John Cokos <jcokos at ccs.net> wrote:
> > Working on a long running perl script, that I think
> > would be better suited to using fork() than running
> > on it's own.
> >
> > Basically, the pseudocode for it is something like
> > this:
> >
> >
> >
> >
>
code:-----------------------------------------------------------------------
-------my
> > @to_do_list
> >
> > push @to_do_list, $something;
> >
> > foreach my $item ( @to_do_list ) {
> >
> > .. do some work.
> > if ( I find something else to do) {
> > push @to_do_list, $this_new_thing;
> > }
> >
> > }
> >
> --------------------------------------------------------------------------
----
> >
> >
> > So, you can see that it's an array that can be added
> > to in mid-stream, and continually grows. Depending
> > on the amount of things
> > "found to do" in the inside loop, it can take hours
> > and hours to run, as that array grows larger.
> >
> > What I'm wanting to do, is fork off 10 child
> > processes, and have each of them doing the same
> > loop, each aware of the master array's
> > contents, and each able to push things onto it, so
> > that the other children, when they're in the loop
> > see the new todo items...
> >
> > Can the children intercommunicate?
> >
> > If not, can they at least return a value back to the
> > parent?
> > In this case, we could have it fork 10, give them a
> > task, get back the new todo items found, and die.
> > Then, fork 10 new kids, etc
> > ....
> >
> > Thanks, John
> > ========================================
> > John Cokos, President / CEO: iWeb Inc.
> > http://www.iwebsys.com
> > jcokos at ccs.net
> > ========================================
> >
> >
> >
> > - - - - - - - - - - - - - - - - - - - - - - - - - -
> > - - - - - - - - - - -
> > POST TO: spug-list at pm.org PROBLEMS:
> > owner-spug-list at pm.org
> > Subscriptions; Email to majordomo at pm.org:
> > ACTION LIST EMAIL
> > Replace ACTION by subscribe or unsubscribe, EMAIL
> > by your Email-address
> > For daily traffic, use spug-list for LIST ; for
> > weekly, spug-list-digest
> > Seattle Perl Users Group (SPUG) Home Page:
> > http://www.halcyon.com/spug/
> >
> >
>
>
> =====
> Richard O. Wood
> Wildwood IT Consultants, Inc.
> wildwood_players at yahoo.com
> 206.605.2539
> http://resumes.dice.com/richardowood
>
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
> Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
> For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
> Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
More information about the spug-list
mailing list