Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

getting values from child process

by myuser (Initiate)
on Nov 10, 2010 at 10:14 UTC ( #870542=perlquestion: print w/ replies, xml ) Need Help??
myuser has asked for the wisdom of the Perl Monks concerning the following question:

From a process XX, i launch 3 or 4 other processes which are the child of the XX process.

In each child process i does different things, and finally when each returns i have to find out which child returned by checking the Environment variable i manipulated in the child. That is i have a variable PROJECTNAME in all child process, and each ones value varies as PROJECT1, PROJECT2... Based up on that i do different task in the parent.

That is what i asked how to get the environment variable from the child process to the parent process. Hope it is clear.

Note: As many people felt the explanation given in my previous question is not clear, I am giving better explanation here of my earlier question here.

Comment on getting values from child process
Re: getting values from child process
by Corion (Pope) on Nov 10, 2010 at 10:22 UTC

    Using the environment won't work.

    My recommendation is to simply use files to transfer the information.

    For alternative approaches, see perlipc and the discussion in your previous thread. But none of the solutions beats using files written by the children and polling for these files in the parent for simplicity. Threads::Queue comes close, but only if you can write your children as threads instead of separate processes.

    Depending on the size of information, you can even get by by appending to a single file and using File::Tail in the parent.

      No file method wont work. If i have not conveyed the scenario properly, here it is, when a child returns to the parent ( after execution completion ) i have to check the environment variable PROJECTNAME, and according to that i have to process in the parent.

        When the child exits, the child environment is lost.

        No file method wont work.
        Yes file method will work. :) The parent knows the pid of the child that just exited from the return value of wait. If each child writes the value of the PROJECTNAME "environment variable" just before it exits (you still haven't explained why it has to be an environment variable) to a file (pid.tmp, say), the parent can read the value of PROJECTNAME from the pid.tmp file.

        You still haven't explained the bigger picture, what is the type of problem you are trying to solve, and why running multiple sub processes is a good way to solve it. And do you really need environment variables? Or do you just need a decent IPC mechanism?

Re: getting values from child process
by cdarke (Prior) on Nov 10, 2010 at 11:09 UTC
    i have to find out which child returned by checking the Environment variable i manipulated in the child

    So why not use wait or waitpid? It depends on how you launch the child processes, but you should be able to pick up the exit code from the child, which has a range of 0-255, usually in $?. See perlvar.
Re: getting values from child process
by JavaFan (Canon) on Nov 10, 2010 at 16:36 UTC
    That is what i asked how to get the environment variable from the child process to the parent process.
    I think it's possible on VMS, but on Unix and Windows, it isn't possible. Environment variables are private to the process. On process creation, the new process gets a copy of the environment variables of the parent, but short of peeking directly into memory, there's no easy way to get to the value of an environment variable of another process. The non-easy ways all have to do with modifying your kernel.

    I'd use exit values or pipes to solve your problem.

Re: getting values from child process
by sundialsvc4 (Monsignor) on Nov 10, 2010 at 18:57 UTC

    As a blanket statement, I would advise that you don’t eer expect the parent of a process (or, any other process) to have “specialized knowledge” about another process, nor to magically rendezvous with one.

    If you need to obtain information from the child, have the child give it to the parent by some sharing-friendly mailbox or queue.

    There are a lot of things (like this) that “look good in testing” and “seem to work okay,” that will have you fall flat on your “*” in production.   :-}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-08-30 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls