in reply to Re: IPC and communication between Parent and Child Process
in thread IPC and communication between Parent and Child Process

thanks for the reply, field supposed to holds the latest value in share memory, $newvalue.

what confused me is that when child process is dead, parent will fork a new one, at that time I do see field of the object in parent holds the latest value of $newvalue since this section get executed:

if ($pid = fork) { #parent $this->{'field'} = $newvalue; $num_children++; print 'parent'.$$.'|'.$this->{'field'}."\n"; return; }
so I expect the forked new child should also have that value.from my understanding of the fork, it will get a copy of the parent's var, filehandle,etc. right ? However, new child does not get the lastest $newvalue but get the value when parent first startup (parent never exit since this is a daemon which monitors child). so did I misunderstand the meaning of fork or something else ? btw, in the child section, I removed the tie $newvalue... so that $newvalue will always be the one in share memory.

Replies are listed 'Best First'.
Re^3: IPC and communication between Parent and Child Process
by shmem (Chancellor) on Oct 26, 2006 at 11:10 UTC
    so I expect the forked new child should also have that value.
    No, it doesn't. While your assumptions about fork are correct (i.e. the child is a full copy of the parent) - the assignment $this->{'field'} = $newvalue is done after the fork happened, so it's visible at the next fork.

    At startup you get 2 child processes, one of them reports 0, the other reports 1.

    Why's that? One child has the value '1' in the 'field' member of the object, from the object constructor new(). The parent then (after having forked) assigns $newvalue - '0' - to the 'field' member, so at the next fork the value for 'field' is '0'.

    Since the 'field' member isn't a shared variable, it can't be set in the parent from the child.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}