http://www.perlmonks.org?node_id=999567


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

I'd be interested in seeing your script as I have tried a basic SSH client with Net::SSH2 and have gotten into (unresolved) problems with input/output - see Net::SSH2 command output polling.

Because fork creates a copy of the environment, you can't "share"* variables between the parent / child without some form of inter-process communication. This I am not an expert on so will watch the forthcoming answers with interest.

* Update follows to reflect blue_cowdawg comments in Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?:

The same variable in parent and child are actually copies, not the same variable in memory, so when either the parent or child modifies its copy, the other (child or parent) is not aware of the update and the same variable NAME is now 'out of sync' (no longer the same VALUE) between the parent and child.

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

Replies are listed 'Best First'.
Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?
by blue_cowdawg (Monsignor) on Oct 17, 2012 at 16:16 UTC
        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
Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess?
by eugolb (Initiate) on Oct 17, 2012 at 16:01 UTC
    Thanks for your response. The point is I don't want to use the open session within the forked subprocess, which of course has duplicated ssh, channel and so forth. I should run (using exec command) a new script, which knows nothing about open session