Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

using tie on two arrays on IPC::Shareable makes array1 and array2 both same even though array2 is not updated.

by mr_p (Scribe)
on Oct 06, 2009 at 17:02 UTC ( #799539=perlquestion: print w/ replies, xml ) Need Help??
mr_p has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w # sharetest - test shared variables across forks use lib "/home/samir/shmem/lib/lib"; use lib "/home/samir/shmem/lib/lib64"; use lib "/home/samir/shmem/lib/lib/perl5/site_perl/"; use IPC::Shareable; my @buffer; my @array1; my @array2; my $glue = 'data'; my %options = ( create => 'yes', exclusive => 0, mode => 0644, destroy => 'yes', ); $handle = tie @array1, 'IPC::Shareable', $glue, { destroy => 1 }; $handle2 = tie @array2, 'IPC::Shareable', $glue, { destroy => 1 }; $SIG{INT} = sub { die "$$ dying\n" }; for (1 .. 10) { unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; squabble(); exit; } push @kids, $child; # in case we care about their pids } while (1) { print "array1: is @array1\n"; print "array2: is @array2\n"; sleep 1; } die "Not reached"; sub squabble { my $i = 0; while (1) { $handle->shlock(); $i++; @array1=(); push (@array1, "$i"); $handle->shunlock(); # $handle2->shlock(); # @array2=(); # push (@array2, "$i++"); # $handle2->shunlock(); } }

problem is array1 and array2 have same results eventhough array2 is not update.

...

I'm trying to use two arrays to one for the parent to update and the other for childerns to update.

been trying for a week now.

download

Comment on using tie on two arrays on IPC::Shareable makes array1 and array2 both same even though array2 is not updated.
Select or Download Code
Re: using tie on two arrays on IPC::Shareable makes array1 and array2 both same even though array2 is not updated.
by ikegami (Pope) on Oct 06, 2009 at 17:47 UTC
    You instructed IPC::Shareable to connect both arrays to the same shared structure ('data').

      If I use antything other then 'data' like 'kala' I guess message as Could not create shared memory segment:

      This is the exact Error message that I get.

      Name "main::kids" used only once: possible typo at ./sharetest1.pl line 32. Name "main::handle2" used only once: possible typo at ./sharetest1.pl line 22. IPC::Shareable::SharedMem: shmget: No such file or directory at /nss/nfx/shmem/lib/lib/perl5/site_perl//5.8.5/IPC/Shareable.pm line 566 Could not create shared memory segment: at ./sharetest1.pl line 21

        create defaults to false. You forget to actually pass the options to IPC::Shareable.
        #!/usr/bin/perl -w use strict; use warnings; use IPC::Shareable qw( ); my @array1; my @array2; my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my $handle1 = tie @array1, 'IPC::Shareable', 'data', \%options; my $handle2 = tie @array2, 'IPC::Shareable', 'kala', \%options;

        You also seem to forgot to connect to the share in the children. It's not documented to be inheritable.

        You might have a problem where the child destroys the share when it exits. That's why forked children should use POSIX::_exit. An alternative is to set destroy => 0 and call  IPC::Shareable->clean_up; before exiting the parent.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-08-28 01:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls