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

Greetings again Monks,

I had posted this code before but the code was too long and I am posting shorted version of it now.

The problem I am having is the Child does not have the same values that the Parent has for the Array @ftpDirectoryListing.

Infact the value that Child prints is interger Array.

Here is the Current Output:

Child - ftpDirectoryListing: 449048769 Parent: currentListing: abc.xml xyz.xml deDuppedArray: dedup1.xml d +edup2.xml Child - ftpDirectoryListing: 449049011 Parent: currentListing: abc.xml xyz.xml deDuppedArray: dedup1.xml d +edup2.xml

The Output Should be:

Child - ftpDirectoryListing: abc.xml xyz.xml Parent: currentListing: abc.xml xyz.xml deDuppedArray: dedup1.xml d +edup2.xml Child - ftpDirectoryListing: abc.xml xyz.xml Parent: currentListing: abc.xml xyz.xml deDuppedArray: dedup1.xml d +edup2.xml

Thanks for all your help. Below is the code.

#! /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 Data::Dumper; use strict; use IPC::Shareable; my $child; my $serv_id; my $maxRcvrCount; my $stripeInstance; my $maxStripeInstance; my $user; my @ftpDirectoryListing=(); my @deDuppedArray=(); my @filesDownloaded=(); my $glue = 'data'; my %options = ( create => 0, exclusive => 1, mode => 0644, destroy => 0, ); my $deDuppedArrayHandle = tie @deDuppedArray, 'IPC::Shareable', 'data' +, \%options; 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); $deDuppedArrayHandle->shlock(); @deDuppedArray = deDupArray1(\@previousListing, \@currentListi +ng); print "Parent: currentListing: @currentListing deDuppedArra +y: @deDuppedArray\n"; $deDuppedArrayHandle->shunlock(); select (undef, undef, undef,.5); } } sub pollDaemonChild { my $user = $_[0]; while (1) { @ftpDirectoryListing = (); $deDuppedArrayHandle->shlock(); @ftpDirectoryListing = @deDuppedArray; $deDuppedArrayHandle->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 = "2"; $SIG{'ALRM'} = 'sig_alrm'; my $startingInstanceNum=1; my @kids; for (1 .. $maxStripeInstance) { $stripeInstance = $startingInstanceNum; unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; pollDaemonChild($user); exit; } push @kids, $child; # in case we care about their pids $startingInstanceNum++; } print "@kids\n"; pollDaemonParent($user); }

  • Comment on Array not holding string values using Tie and IPC::Shareable. Shortened code like asked for.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Array not holding string values using Tie and IPC::Shareable. Shortened code like asked for.
by BioLion (Curate) on Oct 22, 2009 at 10:23 UTC

    I think that maybe the problem comes from the assignment in the child @ftpDirectoryListing = @deDuppedArray, but you haven't actually called pollDaemonParent($user) yet, which assigns to @deDuppedArray. I would guess that maybe if you changed main to a different order it might solve this :

    pollDaemonParent($user); ## assign to @deDuppedArray my $startingInstanceNum=1; my @kids; for (1 .. $maxStripeInstance) { $stripeInstance = $startingInstanceNum; unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; pollDaemonChild($user); ## read from @deDuppedArray exit; } push @kids, $child; # in case we care about their pids $startingInstanceNum++; } print "@kids\n";

    I can't actually test this though... so i could be very wrong. HTH.

    Just a something something...
      I have tried your suggestion and it does not work. Does anyone else have any other suggestions. mr_p.
        Such a comment is useless.
        First, take a minute to look at why it didn't work. Did you have a typo, or something obvious? Maybe it does actually work.
        Second, when posting, say why it didn't work. What is the error message? What is incorrect about the output?

        You should at least put down some evidence that you actually tried the suggestion instead of just dismissing it.