in reply to IPC and communication between Parent and Child Process
#!/usr/bin/perl -w #ipcprocesses.pm. ... sub new { my $class = shift; my $this = { 'field' => 1 }; bless ($this,$class); tie $newvalue,'IPC::Shareable','kali'; return ($this); } ... sub DO_newchild { my $this = shift; ... if ($pid = fork) { #parent $this->{'field'} = $newvalue; ... } else{ my $newvalue; tie $newvalue,"IPC::Shareable",'kali';
The $newvalue in DO_newchild masks the outer $newvalue.
If you want the 'field' member to hold a tied variable, tie the value:
sub new { my $class = shift; #my $this = { 'field' => 1 }; my $this = { }; bless ($this,$class); tie $this->{'field'},'IPC::Shareable','kali'; $this->{'field'} = 1; return ($this); } ... sub DO_newchild { my $this = shift; if ($pid = fork) { #parent # $this->{'field'} = $newvalue; $num_children++; print 'parent '.$$.' | '.$this->{'field'}."\n"; return; } else{ #my $newvalue; #tie $newvalue,"IPC::Shareable",'kali'; #Children can not return from this subroutine $SIG{INT} = 'DEFAULT'; #make SIGINT kill us as it did befo +re while(1) { sleep 2; print 'child '.$$.' | '.$this->{'field'}."\n"; #$newvalue = time(); $this->{'field'} = time; } } exit; }
That's what you do if you want the 'field' object to be updatable by the parent and child processes, visible to each.
--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}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: IPC and communication between Parent and Child Process
by hengha (Initiate) on Oct 26, 2006 at 06:42 UTC | |
by shmem (Chancellor) on Oct 26, 2006 at 11:10 UTC |