Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
      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

In reply to Re^2: How can I pass an open session of Net::SSH2 to a script running in a forked subprocess? by blue_cowdawg
in thread How can I pass an open session of Net::SSH2 to a script running in a forked subprocess? by eugolb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-23 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found