I have a program (code below) that forks off two processes (will be more). One of the processes checks some data and ads it to a hash depending on the outcome of the check. I would like the other process to have access to that hash. The second process works the data put in the hash and removes it based upon it's outcome.

I originally thought that maybe if I passed the hash by reference it would work, but that does not. Can someone offer me a lead on how to do this.

TIA, Chad.

code below

#!/usr/bin/perl -w use strict; my %hash; my $href = \%hash; my @list = qw/1 2 3 4 5 6/; my $pid = fork; if ($pid) { #parent print "$$: Parent Pid\n"; my $new_child = fork; unless($new_child) { #child 1 while (1){ hash_check($href); sleep 3; } exit; } } else { #child 2 for (@list){ pinger($href,$_); } } 1 while waiter(); sub hash_check{ my $hash = shift; print "~~~~~~~~~~~~~~~~~~\n"; for (keys(%$hash)){ print "Key is $_\n"; } print "~~~~~~~~~~~~~~~~~~\n"; } sub pinger{ my $hash = shift; my $value = shift; #print "$$: pinger\n"; if ($value < 4){ add_to_hash($hash, $value); } } sub waiter{ my $waited_pid = wait; # print $waited_pid,"\n"; } sub add_to_hash{ my $hash = shift; my $failed_addr = shift; $$hash{$failed_addr} += 1; } sub del_from_hash{ my $hash = shift; my $pinged_ip = shift; delete $$hash{$pinged_ip}; }

In reply to communication between forked processes by gnu@perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.