Okay, I guess the question I have is, why do you need to fork before creating threads? Are you running this on win32? some nix flavor? If its win32, then the threads and actually forks, and using threads aren't really saving you much of anything

Note, in the code you've posted, you have a scope problem:
if ( $thr->tid && !threads::equal( $thr, th +reads->self ) + ) { ##########Not Sure ####### foreach my $client ( keys %sharedHash ) { print " client $client\n"; my $thrnew = thread->new( \&compare, $client ); $sema->up; }

$sema is not defined in this context, its local to the previous loop. there are numerous other bugs, but i'll leave that to you to find ;)

Again, I would refer you the the thread primer http://perlmonks.org/?node_id=691785 and the like http://perlmonks.org/?node_id=704138, but I've made a stab at doing what I _think_ you are looking to have done

#!/usr/bin/perl use strict; use threads; use threads::shared; use Time::HiRes qw (usleep); use POSIX; my @clientList = qw(client1 client2 client3 client4 client5 client6 client7 client8 c +lient9 client10 client11 client12); my @dmServers = qw(A B C); my @queryPath = qw(AA BB CC DD EE FF); my @shared; my $queryIndex = 0; my $maxnoofThreads = 3; my %sharedHash : shared; for ( my $i = 0 ; $i < @dmServers ; $i++ ) { my $cpid = fork(); die unless defined $cpid; if ( !$cpid ) { # This is the child # my $wait = int rand 10; # sleep $wait; callChild( $queryPath[ $queryIndex++ ], $dmServers[$i], $i ); #print "Child $$ exiting after seconds\n"; exit; } } # Just parent code, after this while ( ( my $cpid = wait() ) != -1 ) { #print "Waited for child $cpid\n"; #print " length of array shared",length(@shared); } #print "Parent Exiting\n"; sub callChild { my $query = shift; my $dmserver = shift; my $i = shift; my %shash; #print " query fired $query on dmserver $dmserver index $i\n"; my @clientList_temp = @clientList; for ( my $index = $i ; $index < @queryPath ; $index = $index + @dm +Servers ) { print "process $$ query at $index is ", $queryPath[$index], "\ +n"; #prepare all of the threads for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { share $shash{$$}{$a}{'data'}; $shash{$$}{$a}{'data'} = undef; share $shash{$$}{$a}{'go'}; $shash{$$}{$a}{'go'} = 0; share $shash{$$}{$a}{'done'}; $shash{$$}{$a}{'done'} = 0; my @temp = splice( @clientList_temp, 0, ceil( @clientList / $maxnoofThreads ) ); $shash{$$}{$a}{'thread'} = threads->new( \&worker, $a, \@temp, $queryPath[$index], +$$ ); print "pid: $$ tid is: ", $shash{$$}{$a}{'thread'}->tid,"\ +n"; } #launch all of the threads for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { print "launching pid:$$ tid: $a\n"; $shash{$$}{$a}{'go'} = 1; } #now wait on them to finish for ( my $a = 1 ; $a <= $maxnoofThreads ; $a++ ) { $shash{$$}{$a}{'thread'}->join; } } sub worker { my $thr_num = shift; my $myClientArrayRef = shift; my $query = shift; my $pid = shift; WORKER_LOOP: while (1) { #wait for $go_control if ( $shash{$pid}{$thr_num}{'go'} ) { #and $go) { for ( my $i=0 ; $i < @$myClientArrayRef ; $i++ ) { print " $$myClientArrayRef[$i] client \n"; $shash{$pid}{$thr_num}{'data'} = "$$myClientArrayR +ef[$i]"; sleep 1; shift @$myClientArrayRef; } $shash{$pid}{$a}{'done'} = 1; #print "done\n"; last WORKER_LOOP; } else { usleep 5000; #essential sleep or will flog cpu waiting + on while loop } # sleep until awakened } #end WORKER_LOOP return; } sub compare { my $client = shift; print " hash value ", $sharedHash{$client}, "\n"; } }
Note that the control structure of what I've done is _extremely_ sloppy and I would not reccommend it.

note that i don't quite understand why you need to thread your compares, but, I guess if that is also a time consuming operation, it could make sense

also note that I made your worker sub and compare sub local to the sub that is running in the fork. I did this for my own readability, as well as to remind myself that they are really running in the scope of the forked process and not the main program

In that vein, in my shared hash, I did not need the pid as a key in the hash, I just put it there for clarity, each forked process will be local unto itself, only copying the values, etc that are available at the time the process is forked (at least the way I understand the world

anyhow, hope this is helpful


In reply to Re^3: Problem in Inter Process Communication by JoeKamel
in thread Problem in Inter Process Communication by libvenus

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.