Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why is my program written to multitask operating sequentially?

Status
Not open for further replies.

StevensonJian

Programmer
May 28, 2008
1
US
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

 
Use parallel::forkmanager :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top