加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Perl多线程和多进程实例

(2010-01-30 12:03:16)
标签:

perl

多线程

多进程

杂谈

分类: perl语言学习

Added by Andree Toonk, last edited by Andree Toonk on May 27, 2009  (view change)
Comment:
Below you'll find an example of a multi-threaded Perl program as well as a forked Perl program.Both seem to work fine. A disadvantage of threads is that Perl needs to be compiled with threads support.Not all versions of Perl come with that by default.

A Perl Thread example


#!/usr/local/roadm/bin/perl

# This is compiled with threading support

use strict;
use warnings;
use threads;
use threads::shared;

print "Starting main program\n";

my @threads;
for ( my $count = 1; $count <= 10; $count++) {
        my $t = threads->new(\&sub1, $count);
        push(@threads,$t);
}
foreach (@threads) {
        my $num = $_->join;
        print "done with $num\n";
}
print "End of main program\n";

sub sub1 {
        my $num = shift;
        print "started thread $num\n";
        sleep $num;
        print "done with thread $num\n";
        return $num;
}


Will print:

Starting main program


started thread 1
started thread 2
started thread 3
started thread 4
started thread 5
started thread 6
started thread 7
started thread 8
started thread 9
started thread 10
done with thread 1
done with 1
done with thread 2
done with 2
done with thread 3
done with 3
done with thread 4
done with 4
done with thread 5
done with 5
done with thread 6
done with 6
done with thread 7
done with 7
done with thread 8
done with 8
done with thread 9
done with 9
done with thread 10
done with 10
End of main program

 

Perl Fork example


This examples fork 10 child processes.
It will wait for all childs to finish before exiting.

#!/usr/local/roadm/bin/perl

 

use strict;
use warnings;

print "Starting main program\n";
my @childs;

for ( my $count = 1; $count <= 10; $count++) {
        my $pid = fork();
        if ($pid) {
        # parent
        #print "pid is $pid, parent $$\n";
        push(@childs, $pid);
        } elsif ($pid == 0) {
                # child
                sub1($count);
                exit 0;
        } else {
                die "couldnt fork: $!\n";
        }

 

}

foreach (@childs) {
        my $tmp = waitpid($_, 0);
         print "done with pid $tmp\n";

     

print "End of main program\n";


sub sub1 {
        my $num = shift;
        print "started child process for  $num\n";
        sleep $num;
        print "done with child process for $num\n";
        return $num;
}

 

Output looks like:

Starting main program


started child process for  1
started child process for  2
started child process for  3
started child process for  4
started child process for  5
started child process for  6
started child process for  9
started child process for  10
started child process for  7
started child process for  8
done with child process for 1
done with pid 5584
done with child process for 2
done with pid 5585
done with child process for 3
done with pid 5586
done with child process for 4
done with pid 5587
done with child process for 5
done with pid 5588
done with child process for 6
done with pid 5589
done with child process for 7
done with pid 5590
done with child process for 8
done with pid 5591
done with child process for 9
done with pid 5593
done with child process for 10
done with pid 5594
End of main program

来源:http://wiki.bc.net/atl-conf/pages/viewpage.action?pageId=20548191


 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有