Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?

by blue_cowdawg (Prior)
on Oct 17, 2012 at 16:16 UTC ( #999573=note: print w/ replies, xml ) Need Help??


in reply to Re: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?
in thread How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?

      Because fork creates a copy of the environment, you can't "share" variables between the parent / child

Not 100% sure what you meant by that, but to review:

When you execute fork a copy of not only the environment but any variables pass from the parent to the child. If the child or parent modify their copy then the copies are out of sync.

| hand waving here my $a=1; my $b=2; if ( fork() ){ # becomes 2 for the parent $a++; } else { # becomes 3 for the child $a=3; } | etc.

A strategy to change this behavior can be to leverage off of signals.

SIGUSR1 30 user defined signal 1 SIGUSR2 31 user defined signal 2
Be careful here though as your mileage may vary wildly depending on the OS you are running on. The above signals exist on a Windows 7 box running Cygwin/X.
SIGUSR1 30,10,16 Term User-defined signal 1 SIGUSR2 31,12,17 Term User-defined signal 2
Those exist on CentOS. While there's commonality between those two I cannot vouch for other environments. The basic strategy is thus:
# handwaving if ( fork() ){ # this is the parent $SIG{USR1)=\&parentHandler; } else { # child $SIG{USR1} =\&childHandler; }
where parentHanlder and childHandler are mirror images of one another and take action appropriatly when the signal arrives.

Another strategy is to use the socketpair() system call. Read up on this strategy in peripc for more details. This allows for bidirectional communications between a child and parent very nicely.

Food for thought I hope...


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-04-24 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (557 votes), past polls