在信号处理函数 (Perl) 中从 main:: 更新数组

发布于 2024-10-20 22:18:22 字数 1397 浏览 1 评论 0原文

我想维护一个我已分叉的子进程的 pidlist 数组,然后在它们退出时将其删除(以限制在任何给定时间我有多少个分叉进程)。我想我可能很聪明,通过在删除或拼接中使用 @main::pid_list 来做到这一点,但没有乐趣。我可以成功弹出一个元素,但显然它不会删除正确的 pid。有什么想法如何处理这个问题或者我会更好地以完全不同的方式来做这件事吗?

#!/usr/bin/perl -w
use POSIX ":sys_wait_h";
use Data::Dumper;

# Only allow 5 processes running at a time

sub REAPER {
    my $child = shift;
    while (($child = waitpid(-1, WNOHANG)) > 0) {
        # Need to remove child from pidlist here
        #pop(@main::pid_list);                     #This works
        #delete($main::pid_list[$child]);          #This does not

    }
    $SIG{CHLD} = \&REAPER;
}

@pid_list = ();
@files = (1 .. 20);

foreach my $file (@files) {
    my $processed = 'false';
    while ($processed eq 'false') {

        print "Working on file: $file\n";
        $SIG{CHLD} = \&REAPER;
        if (scalar(@pid_list) < 5) {
            $pid = fork();
            if ( $pid == 0 ) {
                print "$$: Child Processing file #" . $file . "\n";
                sleep(10);
                print "$$: Child done processing file #" . $file . "\n";
                exit(0);
            }
            push(@pid_list, $pid);
            print Dumper(@pid_list);
            $processed = 'true';
        }
        sleep(1);
    }
}

# Since we are at the end we need to wait for the last process to end
print "PID: $$ End of parent program\n";

exit 0;

I would like to maintain a pidlist array of children I have forked and then remove them as they exit (to restrict how many forked processes i have at any given time). I thought I might be clever and do this by using @main::pid_list in either a delete or splice, but no joy. I can successfully pop an element, but obviously it is not going to remove the correct pid. Any ideas how to handle this or would I be better doing this some entirely different way?

#!/usr/bin/perl -w
use POSIX ":sys_wait_h";
use Data::Dumper;

# Only allow 5 processes running at a time

sub REAPER {
    my $child = shift;
    while (($child = waitpid(-1, WNOHANG)) > 0) {
        # Need to remove child from pidlist here
        #pop(@main::pid_list);                     #This works
        #delete($main::pid_list[$child]);          #This does not

    }
    $SIG{CHLD} = \&REAPER;
}

@pid_list = ();
@files = (1 .. 20);

foreach my $file (@files) {
    my $processed = 'false';
    while ($processed eq 'false') {

        print "Working on file: $file\n";
        $SIG{CHLD} = \&REAPER;
        if (scalar(@pid_list) < 5) {
            $pid = fork();
            if ( $pid == 0 ) {
                print "$: Child Processing file #" . $file . "\n";
                sleep(10);
                print "$: Child done processing file #" . $file . "\n";
                exit(0);
            }
            push(@pid_list, $pid);
            print Dumper(@pid_list);
            $processed = 'true';
        }
        sleep(1);
    }
}

# Since we are at the end we need to wait for the last process to end
print "PID: $ End of parent program\n";

exit 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-10-27 22:18:22

使用哈希表而不是数组。

sub REAPER {
    my $child = shift;
    while (($child = waitpid(-1, WNOHANG)) > 0) {
        # Need to remove child from pidlist here
        delete $main::pid_list{$child};
    }
    $SIG{CHLD} = \&REAPER;
}

...

if ((scalar keys %main::pid_list) < 5) {
    ...
    if ($pid != 0) {
        ...
       exit(0);
    }
    $main::pid_list{$pid}++;
}

Use a hash table instead of an array.

sub REAPER {
    my $child = shift;
    while (($child = waitpid(-1, WNOHANG)) > 0) {
        # Need to remove child from pidlist here
        delete $main::pid_list{$child};
    }
    $SIG{CHLD} = \&REAPER;
}

...

if ((scalar keys %main::pid_list) < 5) {
    ...
    if ($pid != 0) {
        ...
       exit(0);
    }
    $main::pid_list{$pid}++;
}
梦言归人 2024-10-27 22:18:22

尝试 @main::pid_list = grep $_ != $child, @main::pid_list;

Try @main::pid_list = grep $_ != $child, @main::pid_list;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文