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

Global variable acts like local variable why?

by madtoperl (Hermit)
on Mar 06, 2007 at 14:39 UTC ( [id://603423]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

In the below code,the $extflag i am setting to 1 inside parent process,but in the child process the same flag is still available as 0 and the while loop is getting executed infinite loop,So even the $extflag is a global variable it is acting like a local variable.

Can Anyone please let me know how can i avoid the while loop getting executed infiniteThe code is given below.
$extflag=0; print"Flag outside loop=> $extflag\n"; if(defined($pid = fork)) { if ( $pid ) { print"PARENT PROCESS\n"; $extflag=1; print"Flag inside paretn=> $extflag\n"; waitpid ($pid,0); } else { print"CHILD PROCESS\n"; sleep 10; while(!$extflag){ print"Flag inside while child > $extflag\n"; } print"Flag outide while in cheild=> $extflag\n"; } }
Regards,
madtoperl

Replies are listed 'Best First'.
Re: Global variable acts like local variable why?
by kyle (Abbot) on Mar 06, 2007 at 14:48 UTC

    The child gets a copy of the parent's variables at the time of the fork, and from then on those copies are separate. If you're trying to get your child to exit, you probably want to look at kill. More generally, have a look at perlipc for any time you want to communicate between processes.

Re: Global variable acts like local variable why?
by davorg (Chancellor) on Mar 06, 2007 at 14:49 UTC

    When the child process starts up, the value of $extflag is 0. The child process therefore inherits a copy of $extflag with the value 0. The copy of $extflag in the child is completely separate to the copy in the parent so changing the copy in the parent after the fork has no effect on the child.

Re: Global variable acts like local variable why?
by johngg (Canon) on Mar 06, 2007 at 14:50 UTC
    AFAIK, once you have forked, the child is a copy of the parent and is independant of it. In other words, if you change a variable in the parent it will have no effect on the varaible of the same name in the child. They are separate processes.

    Cheers,

    JohnGG

Re: Global variable acts like local variable why?
by Moron (Curate) on Mar 06, 2007 at 15:39 UTC
    $extflag is set to one in the parent process, but not in the child process. Therefore it stays 0 in the child process and the loop is never terminated. What I don't understand is why have a loop there at all - a simple if (or an unless ($extflag)) would be sufficient to limit the contents of the block to the child process(es). Also, try to keep your indentation consistent with {} nesting level - it might make it easier to understand (and might even be the real problem ;)

    -M

    Free your mind

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found