- 论坛徽章:
- 0
|
最近遇到一个头疼的问题,采用fork()进行多进程处理数据,最多允许一次允许50个子进程,但发现经常会遇到回收
不到子进程的情况,导致产生了一堆僵尸进程,程序就无法往后面运行了。能不能帮我解决一下。下面是我的代码。- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX ":sys_wait_h";
- sub mutiple_ref_content_count{
- my $max_process_ref_num = 50;
- my $num_proc = 0;
- my $num_collect = 0;
- my $collect;
- my @all_pid;
- my @All_ref = sort keys %ref_infor;
- ## == get the child signal ==
- $SIG{CHLD} = sub { $num_proc-- };
- for (my $i=0;$i<=$#All_ref ;$i++) {
- my $pid = fork();
- if (!defined($pid)) {
- print "Error in fork: $!";
- exit 1;
- }
- if ($pid == 0) {
- ## == child proc ==
- my $pid = $;
- push @all_pid,$pid;
- sleep(10);
- single_ref_content_count($All_ref[$i]);
- exit 0;
-
- }
- $num_proc ++;
- ## == if need to collect zombies ==
- if (($i-$num_proc-$num_collect) > 0) {
- while (($collect = waitpid(-1, WNOHANG)) > 0) {
- $num_collect ++;
- }
- }
- while ($num_proc >=$max_process_ref_num ) {
- sleep(11);
- }
- }
- while ($num_proc >=1) {
- sleep(10);
- }
- }
复制代码 |
|