in reply to Re^2: Problem in Inter Process Communication
in thread Problem in Inter Process Communication
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 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Problem in Inter Process Communication
by libvenus (Sexton) on Aug 21, 2008 at 06:29 UTC | |
by BrowserUk (Patriarch) on Aug 21, 2008 at 09:29 UTC | |
by libvenus (Sexton) on Aug 21, 2008 at 10:53 UTC | |
by BrowserUk (Patriarch) on Aug 21, 2008 at 11:29 UTC | |
by libvenus (Sexton) on Aug 22, 2008 at 03:13 UTC | |
| |
by JoeKamel (Acolyte) on Aug 21, 2008 at 11:34 UTC | |
by BrowserUk (Patriarch) on Aug 21, 2008 at 11:54 UTC | |
|