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

Shared Memory using IPC::Shareable - Can't use an undefined value as an ARRAY reference

by mr_p (Scribe)
on Feb 17, 2010 at 16:42 UTC ( [id://823756]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am working on a program that forks 3 childern. The first child acts as a perent to the next two.

Job for the first Child:

- Update a Variable called dirListing.

- Checks filesDownloaded array and takes action based on filename.

Job for two childern:

- Check dirListing array content and take action based on the filename.

- If action is taken then added filename to filesDownloaded array.

when I run the program I keep getting the following error.

Can't use an undefined value as an ARRAY reference at /shmem/lib/lib/perl5/site_perl//5.8.5/IPC/Shareable.pm line 446. IPC::Shareable::FETCHSIZE('IPC::Shareable=HASH(0x79baf0)') called at ./test_nfxftpd line 63

Please help, I have watsed so much time with this, Thanks in advance.

#! /usr/bin/perl -w use strict; use lib "/nss/nfx/shmem/lib/lib"; use lib "/nss/nfx/shmem/lib/lib64"; use lib "/nss/nfx/shmem/lib/lib/perl5/site_perl/"; use File::Basename; use Getopt::Long; use File::Copy; use strict; #use IPC::Shareable; use IPC::Shareable qw(); my $child; my $serv_id; my $maxRcvrCount; my $stripeInstance; my $maxStripeInstance; my $user; my @ftpDirectoryListing=(); my @dirListing=(); my @filesDownloaded=(); my $glue = 'data'; my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my $dirListingHandle = tie @dirListing, 'IPC::Shareable', 'data', \%op +tions; my $filesDownloadedHandle = tie @filesDownloaded, 'IPC::Shareable', 'k +ala', \%options; sub pollDaemonParent { my $user = $_[0]; my @previousListing=(); my @currentListing=(); while (1) { @previousListing = @currentListing; @currentListing = getListing($user); $dirListingHandle->shlock(); @dirListing = deDupArray1(\@previousListing, \@currentListing) +; print "Parent: currentListing: @currentListing dirListing: +@dirListing\n"; $dirListingHandle->shunlock(); deleteRemoteFiles1(); select (undef, undef, undef,.5); } } sub deleteRemoteFiles1 { # TODO: Get list and delete from server. once deleted $filesDownloadedHandle->shlock(); if ($#filesDownloaded >= 0) { print "\nParent: deleteListing: @filesDownloaded\n"; } foreach (@filesDownloaded) { deleteFile($_); } @filesDownloaded=(); $filesDownloadedHandle->shunlock(); } sub pollDaemonChild { my $user = $_[0]; print "ChildStarted: $stripeInstance\n"; while (1) { @ftpDirectoryListing = (); $dirListingHandle->shlock(); @ftpDirectoryListing = @dirListing; $dirListingHandle->shunlock(); if ($#ftpDirectoryListing >= 0) { print "Child - @ftpDirectoryListing: @ftpDirectoryListing\ +n"; } select (undef, undef, undef,1); } } sub getListing { my @ftpListing1 = (); push (@ftpListing1, "abc.xml"); push (@ftpListing1, "xyz.xml"); return @ftpListing1; } sub deDupArray1 { my @difference = ("dedup1.xml", "dedup2.xml"); return @difference; } sub addToDeleteList { $|++; $filesDownloadedHandle->shlock(); push (@filesDownloaded, "$_[0]"); $filesDownloadedHandle->shunlock(); } # main { $user = "IAmUser"; $serv_id = "1"; $maxRcvrCount = "5"; $stripeInstance = "1" ; $maxStripeInstance = "3"; $SIG{'ALRM'} = 'sig_alrm'; my $startingInstanceNum=1; my @kids; for (1 .. $maxStripeInstance) { $stripeInstance = $startingInstanceNum; print "stripeInstance: $startingInstanceNum\n"; unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; if($stripeInstance == 1 ) { pollDaemonParent($user); } else { pollDaemonChild($user); } exit; } push @kids, $child; # in case we care about their pids $startingInstanceNum++; } while (1) { sleep 1; } }
  • Comment on Shared Memory using IPC::Shareable - Can't use an undefined value as an ARRAY reference
  • Download Code

Replies are listed 'Best First'.
Re: Shared Memory using IPC::Shareable - Can't use an undefined value as an ARRAY reference
by zentara (Archbishop) on Feb 18, 2010 at 13:15 UTC
    You might want to try this script, which was worked out by andThenThereWasPERL in forking with Storable and IPC::ShareLite. It may give you a way to get it working with Parallel::ForkManager, or at least clue you in as to why your code is failing.
    #!/usr/bin/perl use strict; use Parallel::ForkManager; use IPC::Shareable; my $glue = $$; my %options = ( create => 1, exclusive => 0, mode => 0644, destroy => 1, ); my %final_parent_hash; my $parent_share = tie %final_parent_hash, 'IPC::Shareable', $glue, { +%options } or die "parent : tie failed\n"; my $fork_manager = new Parallel::ForkManager(5); foreach my $child ( 1 .. 10 ) { my $pid = $fork_manager->start($child) and next; my %options = ( create => 0, exclusive => 0, mode => 0644, destroy => 0, ); my %child_hash; my $child_share = tie %child_hash, 'IPC::Shareable', $glue, { %opt +ions } or die "client: tie failed\n"; for my $id (1 .. 20) { my $key = $child . '-' . $id; $child_share->shlock; $final_parent_hash{$key} = qq{|Kid $child pushed $id}; $child_share->shunlock; } $fork_manager->finish($child); } print "Waiting for Children...\n"; $fork_manager->wait_all_children; my $sleep_ctr = 10; while ($sleep_ctr) { if ((keys %final_parent_hash) == 200) { last; } sleep 1; $sleep_ctr--; } foreach my $child ( 1 .. 10 ) { for my $id (1 .. 20) { my $key = $child . '-' . $id; if (! exists $final_parent_hash{$key} ) { print "Missing data for Kid $child , data $id\n"; } else { print "$key = $final_parent_hash{$key}\n"; } } } IPC::Shareable->clean_up_all;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: Shared Memory using IPC::Shareable - Can't use an undefined value as an ARRAY reference
by rowdog (Curate) on Feb 18, 2010 at 01:57 UTC

    You tie @dirListing and then go on to fork. I'm not sure that tied variables survive a fork and, in any case, they're not safe to access from more than one child. fork before tie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-18 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found