- 论坛徽章:
- 0
|
本帖最后由 aweizai 于 2011-07-06 13:34 编辑
有一段perl代码用到了perl 的fork 生成多个子线程,客户执行后说是有很多子线程的执行被忽略掉了,我看了客户提供的执行日志,发现最后一个子线程在主线程关闭后反而能正常执行,怀疑子线程占用的资源是不是都要等到主线程结束后才释放,在本地测试了一下,重现不了这个问题,也没发现有内存泄漏。
大家忙我看看吧,如果有应该要怎么改?- #!/usr/local/bin/perl -w
- use strict;
- use POSIX ":sys_wait_h";
- use threads::shared;
- use JSON;
- use Data::Dumper;
- use Getopt::Long qw(GetOptions);
- my @feeds = ();
- my $children_num;
- my $help;
- my $debug;
- my $verbose;
- my $main_start = time();
- print "\nStart process_feeds_multi.pl, main process pid is $$\n" if $debug;
- my @children;
- my $curr_children_num :shared = 0;
- my $feeds_index :shared = 0;
- my $front_pid;
- $children_num = 1 unless $children_num;
- @feeds = split ',', join ',', @feeds;
- my $feeds_to_run;
- pipe(RD,WT) or die "Can't open pipe: $!\n";
- my $f_pid = fork();
- if ( $f_pid ) {
- $front_pid = $f_pid;
- close WT;
- $feeds_to_run = jsonToObj(<RD>);
- }
- elsif ( $f_pid == 0) {
- if ( $debug ) {
- my $ppid = getppid();
- print "Create child process to get active feeds, its pid is $$ and parent pid is $ppid \n";
- }
- $feeds_to_run = &get_active_feeds(@feeds);
- if ( $verbose ) {
- my $feeds_str = join (', ', map { $_->{feed_id} } @$feeds_to_run);
- print "Active feeds: [ $feeds_str ] \n";
- }
- my $feeds_json = objToJson($feeds_to_run);
- close RD;
- select WT;
- $| = 1;
- print $feeds_json;
- exit 0;
- }
- else {
- die "couldn't fork:$!";
- }
- if ( waitpid( $front_pid, 0 ) ) {
- print "Finish getting active feeds, done with child-process $front_pid \n" if $debug;
- while ( $curr_children_num < ($children_num - 1) && $feeds_index < scalar (@$feeds_to_run) ) {
- &create_child();
- }
- while ( $feeds_index < scalar (@$feeds_to_run) ) {
- &create_child();
- my $child = undef;
- do {
- $child = waitpid(-1, WNOHANG);
- } until $child > 0;
- }
- }
- print "Done with parent-process $$, all child-processes are created.\n" if $debug;
- exit;
- sub create_child() {
- my $pid = fork();
- if ( $pid ) {
- $curr_children_num++;
- $feeds_index++;
- push ( @children, $pid );
- }
- elsif ( $pid == 0 ) {
- my $child_begin_time = time();
- if ( $debug ) {
- my $ppid = getppid();
- print "Create a new child-process, its pid is $$ and parent-process is $ppid \n";
- }
- my $feed_id = $tmp_data->{feed_id};
- print "Start producing feed $feed_id ...\n" if $debug;
- process_feed( $tmp_data );
- my $child_end_time = time();
- if ( $verbose ) {
- my $cost_time = $child_end_time - $child_begin_time;
- print "Done with producing " if $debug;
- print "feed $feed_id, cost $cost_time s\n";
- }
- print "Done with child-process $$ and exit.\n" if $debug;
- exit 0;
- }
- else {
- die "couldn't fork:$!";
- }
- return 1;
- }
- sub get_active_feeds {
- my @limit = @_;
- ...
- return \@feeds;
- }
- sub process_feed {
- ...
- }
复制代码 运行日志大概如此- Start process_feeds_multi.pl, main process pid is 5706
- Create child process to get active feeds, its pid is 5708 and parent pid is 5706
- Get active feeds:
- Active feeds: [ 34, 44, 62, 63, 65, 66, 67, 72, 78, 79, 84, 85, 86, 87, 88, 91, 92, 97, 100, 104, 106, 110, 115, 116, 119, 122, 132, 134, 135, 137, 138, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 154, 156, 157, 158, 159, 160, 161, 162, 163, 171, 172, 175, 176, 184, 185, 187, 188, 189, 190, 196, 200, 209, 210, 211, 214, 215, 227, 231, 232, 233, 234, 235, 236, 237, 238, 241, 247, 248, 249, 250, 253, 257, 258, 260, 267, 269, 271, 272, 274, 275, 278, 279, 280, 282, 283, 284, 285, 286, 287, 288, 292, 293, 294, 295, 300, 301, 302, 305, 306, 307, 309, 310, 311, 315, 318, 319, 322, 323, 324, 325, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 342, 343, 344, 345, 346, 347, 348, 349, 350, 356, 357, 358, 359, 360, 361, 362, 365, 368, 372, 375, 376, 377, 384, 386, 387, 388, 389, 390, 392, 394, 397, 398, 400, 402, 403, 404, 405, 406, 407, 409, 410, 412, 418, 419, 420, 421, 424, 427, 429, 431, 433, 434, 435, 436, 437, 438, 439, 445, 447, 448, 449, 450, 452, 453, 454, 456 ]
- Finsh getting active feeds, done with child-process 5708
- Create a new child-process, its pid is 5710 and parent-process is 5706
- Start producing feed 44 ...
- Done with producing feed 44, cost 27 s
- Done with child-process 5710 and exit.
- Create a new child-process, its pid is 5973 and parent-process is 5706
- Start producing feed 62 ...
- Done with producing feed 62, cost 1083 s
- Done with child-process 5973 and exit.
- Create a new child-process, its pid is 5709 and parent-process is 5706
- Start producing feed 34 ...
- Done with producing feed 34, cost 1111 s
- Done with child-process 5709 and exit.
- Create a new child-process, its pid is 8084 and parent-process is 5706
- Start producing feed 65 ...
- Done with producing feed 65, cost 789 s
- Done with child-process 8084 and exit.
- Create a new child-process, its pid is 9448 and parent-process is 5706
- Start producing feed 66 ...
- Done with producing feed 66, cost 73 s
- Done with child-process 9448 and exit.
- Create a new child-process, its pid is 9491 and parent-process is 5706
- Start producing feed 67 ...
- .....
- .....
- Create a new child-process, its pid is 21087 and parent-process is 5706
- Start producing feed 196 ...
- Create a new child-process, its pid is 21088 and parent-process is 5706
- Start producing feed 200 ...
- Create a new child-process, its pid is 21089 and parent-process is 5706
- Start producing feed 209 ...
- Create a new child-process, its pid is 21090 and parent-process is 5706
- Start producing feed 210 ...
- Create a new child-process, its pid is 21091 and parent-process is 5706
- Start producing feed 211 ...
- .....
- .....
- Create a new child-process, its pid is 21287 and parent-process is 5706
- Start producing feed 454 ...
- Done with parent-process 5706, all child-processes are created.
- Create a new child-process, its pid is 21288 and parent-process is 5706
- Start producing feed 456 ...
- Create a new child-process, its pid is 20787 and parent-process is 5706
- Start producing feed 184 ...
- Done with producing feed 184, cost 723 s
- Done with child-process 20787 and exit.
复制代码 |
|