use strict; use threads; use threads::shared; use Thread::Semaphore; use POSIX; my @clientList = qw(client1 client2 client3 client4 client5 client6 client7 client8 client9 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 = 2; 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; #print " query fired $query on dmserver $dmserver index $i\n"; my @clientList_temp = @clientList; for(my $index = $i; $index < @queryPath;$index = $index + @dmServers) { print "process $$ query at $index is ",$queryPath[$index],"\n"; # $shared[$i] = $i; # sleep rand(10); for(my $a;$a <= $maxnoofThreads;$a++) { my $sema = Thread::Semaphore->new(); my @temp = splice(@clientList_temp,0,ceil(@clientList/$maxnoofThreads)); my $thr = thread->new(\&worker,$sema,\@temp,$queryPath[$index]); #if( defined %sharedHash) # { # my $thrnew = thread->new(\&compare); # $sema->up; # } } foreach my $thr (threads->list) { # Don't join the main thread or ourselves if ($thr->tid && !threads::equal($thr, threads->self)) { ##########Not Sure ####### foreach my $client (keys %sharedHash) { print " client $client\n"; my $thrnew = thread->new(\&compare,$client); $sema->up; } $thr->join; } } } #$shared[$i] = undef; } sub worker { my $sema = shift; my $myClientArrayRef = shift; my $query = shift; for(my $i;$i < @$myClientArrayRef;$i++) { print " $$myClientArrayRef[$i] client \n"; sleep 5; my $thrid = threads->tid; $sharedHash{$$myClientArrayRef[$i]} = $thrid; shift @$myClientArrayRef; $sema->down; } } sub compare { my $client = shift; print " hash value ",$sharedHash{$client},"\n"; }