StevensonJian
Programmer
Hi,
At work, I need to make a script that need to do parallel processing. I have tried both thread and fork, but somehow both of them operate sequentially not parallely on my computer. I am using Perl 5.10 and windows XP.
Fork Code:
############################################################
$pid = fork();
if ($pid == 0)
{
sleep(5);
print "I am the child";
}
else {
print "I am the parent";
}
exit(0);
-----------------------------------------------------------
Computer output: I am the ChildI am the parent
Desired output: I am the parentI am the child
-----------------------------------------------------------
Clearly, the program is not running parallely; the parent process is blocked by the child process.
###########################################################
Since fork is definitely not working, I then went ahead and tried threading:
###########################################################
use threads;
use threads::shared;
my $monitor1 = threads->create('timeMonitoring1');
my $monitor2 = threads->create('timeMonitoring2');
$monitor1->join();
$monitor2->join();
sub timeMonitoring1 {
my $i=10;
sleep(2);
print ("monitor1-1");
sleep(1);
print ("monitor1-2");
sleep(1);
print ("monitor1-3");
return 1;
}
sub timeMonitoring2 {
my $i=10;
sleep(1);
print ("monitor2-1");
sleep(3);
print ("monitor2-2");
return 1;
}
-----------------------------------------------------------
Computer ouput:monitor1-1 monitor1-2 monitor1-3 monitor2-1 monitor2-2
Desired output:monitor2-1 monitor1-1 monitor2-2 monitor1-2 monitor1-3
Again, the computer is treating my 2 threads sequentially. Thread containing monitor1 is blocking the other thread.
If anyone can help me see what I did wrong in either of these approaches, I will greatly, greatly appreciate it.
Thanks,
Stevo
At work, I need to make a script that need to do parallel processing. I have tried both thread and fork, but somehow both of them operate sequentially not parallely on my computer. I am using Perl 5.10 and windows XP.
Fork Code:
############################################################
$pid = fork();
if ($pid == 0)
{
sleep(5);
print "I am the child";
}
else {
print "I am the parent";
}
exit(0);
-----------------------------------------------------------
Computer output: I am the ChildI am the parent
Desired output: I am the parentI am the child
-----------------------------------------------------------
Clearly, the program is not running parallely; the parent process is blocked by the child process.
###########################################################
Since fork is definitely not working, I then went ahead and tried threading:
###########################################################
use threads;
use threads::shared;
my $monitor1 = threads->create('timeMonitoring1');
my $monitor2 = threads->create('timeMonitoring2');
$monitor1->join();
$monitor2->join();
sub timeMonitoring1 {
my $i=10;
sleep(2);
print ("monitor1-1");
sleep(1);
print ("monitor1-2");
sleep(1);
print ("monitor1-3");
return 1;
}
sub timeMonitoring2 {
my $i=10;
sleep(1);
print ("monitor2-1");
sleep(3);
print ("monitor2-2");
return 1;
}
-----------------------------------------------------------
Computer ouput:monitor1-1 monitor1-2 monitor1-3 monitor2-1 monitor2-2
Desired output:monitor2-1 monitor1-1 monitor2-2 monitor1-2 monitor1-3
Again, the computer is treating my 2 threads sequentially. Thread containing monitor1 is blocking the other thread.
If anyone can help me see what I did wrong in either of these approaches, I will greatly, greatly appreciate it.
Thanks,
Stevo