Perhaps others might
know better but to the best of my knowledge the fork command doesn't
work that way. The child and parent
are two completely separate processes that don't
share any resources. What you are looking for is something more
like threads where each thread can access shared resources. I'm not sure if Perl handles
threading.
However,
I did something similar to what you are trying to do but I had to do it using file
handles. Here is a snippet of how I handled
this.
#### Parent
if ($put_pid =
open(PUT, "-|")) {
do {
$term = waitpid($put_pid,&WNOHANG);
chomp ($tmp = <PUT>);
print "$tmp \n" if (defined $tmp && $tmp ne '')
} while ($term <= 0);
close PUT;
do {
$term = waitpid($put_pid,&WNOHANG);
chomp ($tmp = <PUT>);
print "$tmp \n" if (defined $tmp && $tmp ne '')
} while ($term <= 0);
close PUT;
}
#### Child
} elsif (defined $put_pid) {
STDOUT->autoflush(1); # must have this or you won't get anything until the child terminates
#### Child
} elsif (defined $put_pid) {
STDOUT->autoflush(1); # must have this or you won't get anything until the child terminates
foreach (1..12) {
print " $_\n"; # must have \n at the end of each print you want to go though. This is what autoflush keys on.
sleep 1;
print " $_\n"; # must have \n at the end of each print you want to go though. This is what autoflush keys on.
sleep 1;
}
exit
0;
}
}
Matt
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Arijit Das
Sent: Wednesday, August 24, 2005 9:42 AM
To: activeperl@listserv.ActiveState.com; perl-unix-users@listserv.ActiveState.com
Subject: [Perl-unix-users] Sharing Variables among Processes...
Its about sharing a Perl Variable among parent and child processes.
Do you know of any design pattern or technique by which this can
be achieved?
What I want is this...
$shared_var = 10;
if ($pid = fork) {
sleep 10;
print "$shared_var\n"; # Should show the child's
changes...
} elsif (defined $pid) {
$shared_var = 12;
exit 0;
}
I want the change in the shared variable to be reflected to the parent
process immediately...basically, same variable in memory should be changed.
Thanks,
Arijit
Start your day with Yahoo! - make it your home page
_______________________________________________ Perl-Unix-Users mailing list Perl-Unix-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs