Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Global Variables Not Updated in Child Process

by anshumangoyal (Scribe)
on Nov 16, 2012 at 05:20 UTC ( [id://1004124]=perlquestion: print w/replies, xml ) Need Help??

anshumangoyal has asked for the wisdom of the Perl Monks concerning the following question:

I am having a program where a few Global Variables are defined. From the same program/script i am forking a child process which is in form of another PM file. In another PM file I am calling the main program to use its global variables. Problem is these Global Variables are not getting updated when changed from Child Process (.PM) file. Is this a constraint with Child Processes or there is any other way to do it.
  • Comment on Global Variables Not Updated in Child Process

Replies are listed 'Best First'.
Re: Global Variables Not Updated in Child Process
by philipbailey (Curate) on Nov 16, 2012 at 08:20 UTC

    When you fork a process, then parent and child are initially more or less exact copies, including the values of variables, except for the return code of fork(). But the key word there is copies. Those two processes are now independent and variables, "global" or not, are not connected in any way.

    That's fortunate, as for Unix-like operating systems, and probably others, all processes are ultimately children (or children of children etc.) of one startup process. If data were always shared between parent and child, all processes would have some common state.

    As to how you can solve your problem, the best approach depends on the details of what you are doing. You could share data, for example, through disc files, or by various IPC mechanisms, e.g. sockets, pipes. See perlipc.

Re: Global Variables Not Updated in Child Process
by choroba (Cardinal) on Nov 16, 2012 at 08:16 UTC
    Normally, a child process cannot change its parent's variables. But see forks::shared.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I tried Fork::Shared and Dat::Dumper, but ther issue is they are not able to save Type Globs. It returned error that it is not able to save Type Globs. Any other solution that you have?
Re: Global Variables Not Updated in Child Process
by afoken (Chancellor) on Nov 18, 2012 at 17:19 UTC

    You seem to have a problem understanding the concept behind fork(). fork creates an exact copy of the current process, assigns it a new process ID, and makes both processes run independantly. Some things are shared, like file handles, but most (like memory) are not. (Actually, modern operating systems initially share most memory of both processes and use a copy-on-write mechanism to avoid expensive copy operations as long as possible.)

    What you expect here is that your child will also have a hangover after only you got drunk.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      So, is there anything I can use instead of forking? I want to run some processes in parallel and capture the results from them. My Prime focus is not forking but process some thing in a child process and get the values updated in parent.
        My Prime focus is not forking but process some thing in a child process and get the values updated in parent.

        So you want your son to learn French and your daugther to learn German, and you expect that you automatically speak both French and German as soon as both have finished learning. Does that work in real life? Nope. And it also won't work with fork()ed processes.

        Please read fork-exec, fork, and perhaps also exec.

        Generally, processes can't update other processes data. You need some kind of inter-process communication, or shared memory. Shared memory won't work well with Perl, because perl tends to move data around when its size changes. See perlipc for details on both ways.

        Another option, with its own set of traps, is using threads. Threads give you lightweight "processes" inside a single process, and threads can actually share variables. See perlthrtut for details. Of course, this works only if all external programs you are using now are merged into a single big program (possibly by moving each program into a single module).

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found