Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Access value of a variable after exit 0 in fork

by NetWallah (Abbot)
on Dec 27, 2012 at 07:33 UTC ( #1010462=note: print w/ replies, xml ) Need Help??


in reply to Access value of a variable after exit 0 in fork

The simplest would be to return a small numeric value via the exit code of the sub-process, read that in the parent process, and translate that in you main code to set $trigger to "ON".

The problem you currently have is that $trigger in the child process is in a different memory space, and modifying that in the child has no effect on the parent's instance of $trigger.

             "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius


Comment on Re: Access value of a variable after exit 0 in fork
Re^2: Access value of a variable after exit 0 in fork
by Anonymous Monk on Dec 27, 2012 at 08:53 UTC
    Can you please elaborate via some code? Sorry to give you trouble,but just want to understand thoroughly.
      See perlipc and waitpid
      if( my $pid = fork() ){ print "parent ($$) is waiting on child $pid\n"; my $kid ; do { $kid = waitpid(-1, WNOHANG); } while $kid > 0; my $exit = $? >> 8; die qq{Kid ($kid) exited with $? / $exit \n}; } else { print "I am the kid $$\n"; sleep 1; #~ exit 666; ## too big exit 12; } __END__ parent (1724) is waiting on child -880 I am the kid -880 Kid (-880) exited with 3072 / 12

      There are abstractions to help you manage this like Parallel::ForkManager, Proc::Fork, forks, threads ....

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2015-07-02 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (34 votes), past polls