perluser09 has asked for the wisdom of the Perl Monks concerning the following question:
But, when I try to redirect the output of the script to a file and run the perl script as below, Thread 1 seems to do all the jobs and Thread 2 is missing completely.C:\Users\perluser09\Desktop\Perl>perl lock.pl ... Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.47 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.46 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.45 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.40 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.44 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.39 Thread 2: Doing SNMP GET for 2.1.1.1:1.3.1.8.38 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.43 ..... Perl exited with active threads 0 running and unjoined 2 finished and unjoined 0 running and detached
What I discovered is that when I do not do any redirection, the two threads are created almost one after another, but when I use redirection, the second thread is created only after the first thread has completed the while loop, which means performed the entire operation. I would be grateful if someone would point out the reason for such behavior. The full code for the script is:C:\Users\perluser09\Desktop\Perl>perl lock.pl > threadinfo.txt .... Thread 1: size of the array is 199 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.200 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.199 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.198 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.197 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.196 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.195 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.194 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.193 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.192 Thread 1: Doing SNMP GET for 2.1.1.1:1.3.1.8.191 ...... Perl exited with active threads: 0 running and unjoined 2 finished and unjoined 0 running and detached
#create the array with the ip:OID use warnings; use strict; use Thread qw(async); use threads::shared; share (my @array); @array = qw/2.1.1.1:1.3.1.8.1 2.1.1.1:1.3.1.8.2 2.1.1.1:1.3.1.8.3 +2.1.1.1:1.3.1.8.4 2.1.1.1:1.3.1.8.5 2.1.1.1:1.3.1.8.6 2.1.1.1:1.3.1.8 +.7....sequence continues upto....2.1.1.1:1.3.1.8.199 2.1.1.1:1.3.1.8. +200/; my $thr1 = async { while($#array > 0) { print "Thread 1: size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 1: Doing SNMP GET for $_\n" if (defined( +$_)); } } }; my $thr2 = async { while($#array > 0) { print "Thread 2: Size of the array is $#array\n"; my @my_oids; { lock (@array); # Block until we get access to $a for(my $i=0;$i<10;$i++) { push (@my_oids, (pop @array)); } } foreach(@my_oids) { print "Thread 2: Doing SNMP GET for $_\n" if (defined( +$_)); } } }; $thr1->join; $thr2->join;
|
---|