加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl fork多进程

发布时间:2020-12-15 23:40:02 所属栏目:大数据 来源:网络整理
导读:原文地址:http://blog.sina.com.cn/s/blog_574962d801010vc3.html Perl fork() Forking in perl is a nice thing to do,and for some it’s a hard thing to understand. It can be pretty easy to get lost especially since there are 100 ways to the sa

原文地址:http://blog.sina.com.cn/s/blog_574962d801010vc3.html

Perl fork()

Forking in perl is a nice thing to do,and for some it’s a hard thing to understand. It can be pretty easy to get lost especially since there are 100 ways to the same thing. I’m going to attempt to explain a little bit of the inner workings of fork() in Perl.

First you have to understand what fork() returns. When you do:

my $pid = fork();

If it is the parent,$pid will be assigned the PID of the child.
If it is the child,$pid will be assigned 0.
If it cannot fork anymore because of no resources,$pid will be undefined.

To help show how a fork() program works,I’m going to use this sample script:

#!/usr/bin/perl

my $pid = fork();
if (not defined $pid) {
print “resources not avilable.n”;
} elsif ($pid == 0) {
print “IM THE CHILDn”;
sleep 5;
print “IM THE CHILD2n”;
exit(0);
} else {
print “IM THE PARENTn”;
waitpid($pid,0);
}
print “HIYAn”;

If run,you will see this:

$ ./f.pl
IM THE CHILD
IM THE PARENT
- sleep for 5 seconds -
IM THE CHILD2
HIYA

$ ps -ef | grep fork1.pl
derek 6440 2888 0 15:45 pts/2 00:00:00 /usr/bin/perl ./fork1.pl
derek 6441 6440 0 15:45 pts/2 00:00:00 /usr/bin/perl ./fork1.pl

This is a pretty simple script and self explanatory. It starts out with the fork and then checks the value of $pid through the if statements and executes the block of code accordingly. What you really have to understand is that?when fork() is called,you now have 2 programs that are the same. So in this example,when we do my $pid = fork(); you now have 2 processes running.?Each process will run the code.?It looks like $pid is only being assigned one value here but it is actually being assigned two or even three values (undefined if necessary). When the parent runs checking through the if statements,it will catch on the last else statement here because $pid is assigned PID of the child. When the child runs through this block of code,it will catch on the if ($pid == 0) because the $pid is assigned 0 for a child. The waitpid() call just waits for the child to exit. If you do not do this it will become a zombie (defunct) process,which means it has become detached from it’s parent.
So here is exactly what happens when you run this:

- The program forks,you now have 2 processes,one is the child,one is the parent.
- if (not defined $pid) gets run on?both?processes,and die’s if resources aren’t available.
- elsif ($pid == 0) gets run on?both?processes,if its the child,print “IM THE CHILD”,sleep for 5 seconds and then print “IM THE CHILD 2″ and exit(0);
- While the above statement is running on the child,the parent is going along with the else {} block of code and prints “IM THE PARENT” and then waits for the child to exit.

NOTE: The exit(0) in the child block is very important.. you need the child to exit its process when it is done,so it will no longer exist.

fork

首先说说?fork?函数。这个函数用来创 建一个进程,不过创建方法有些不太好理解。 先看下面的程序 fork-test.pl。我是用perl写的,不过相同的功能也可以用 C 来完成。

#!/usr/bin/perl
#------------------------------------
# fork-test.pl
print "Program started,pid=$$.n";
if ($child_pid = fork()) {
print "I'm parent,my pid=$$,child's pid=$child_pid.n";
} else {
print "I'm child,pid=$$.n";
}

运行之后显示下面的结果。

Program started,pid=8934.
I'm child,pid=8935.
I'm parent,my pid=8934,child's pid=8935.

IGCHLD信号和僵尸进程

首先说说什么是僵尸进程(zombie process)。我们知道 Linux 使用进程表来管理进程, 每个进程都在进程表中占据一个位置。当我们用 fork 生成一个子进程, 然后该子进程退出时,系统不会自动回收该子进程所占位置。 此时虽然进程表中有这个子进程的信息,但实际上该子进程早已结束, 于是这个进程就成了“僵尸进程”。

僵尸进程虽然不占用系统资源,但是它会浪费进程表的位置。如果僵尸进程太多, 有可能会导致不能创建新进程。下面的例子 zombie-test.pl 演示了如何创建僵尸进程:

#!/usr/bin/perl
#------------------------------------
# zombie-test.pl

sub child {
print "I'm child,pid=$$.n";
}

while (1) {
if (fork() == 0) {
&child; # 如果当前进程是子进程,则执行 child 函数
exit; # 并退出
} else {
sleep 5; # 如果是父进程,则睡眠 5 秒
}
}

该程序每隔 5 秒创建一个子进程,子进程输出一行文字后退出。 执行该程序片刻之后,从其他终端用 ps -ef 命令可以看到进程状态。 标有 <defunct> 的就是僵尸进程。

charlee 11687 10870 0 02:01 pts/1 00:00:00 /usr/bin/perl perl/zombie-test.pl
charlee 11688 11687 0 02:01 pts/1 00:00:00 [zombie-test.pl] <defunct>
charlee 11691 11687 0 02:01 pts/1 00:00:00 [zombie-test.pl] <defunct>
charlee 11695 11687 0 02:01 pts/1 00:00:00 [zombie-test.pl] <defunct>

如何避免僵尸进程?当子进程结束时,系统会向父进程发送 SIGCHLD 信号。 父进程只要在处理这个信号时回收子进程占用的资源即可。

利用 waitpid 回收僵尸进程

一个方法就是在父进程中利用 waitpid 函数。该函数回收指定进程的资源, 并返回已结束进程的进程id。若指定进程不存在,则返回 -1。 我们可以通过调用 waitpid(-1,WNOHANG) 来回收所有子进程。 Perl 提供了全局变量 %SIG,只要设置该变量即可安装信号处理程序。 下面的 waitpid_test1.pl 演示了使用方法。完整的代码可以从本文的附件中下载。

use POSIX ":sys_wait_h";
$SIG{CHLD} = &;REAPER;
sub REAPER {
my $pid;
while (($pid = waitpid(-1,WNOHANG)) > 0) {
# 进行一些处理
}
}

执行这个程序并用 ps -ef 查看进程,可以发现僵尸进程不再出现了。

不过上面这个程序有个问题。Linux的信号是不能排队的, 如果信号到达进程时进程不能接收该信号,这个信号就会丢失。 REAPER 中包含比较耗时的 while 循环,如果在 REAPER 执行过程中 发生 SIGCHLD 信号,这个信号就会丢失。为了避免这种情况, 我们可以尽量减少信号处理的执行时间。参考下面的 waitpid_test2.pl。

our $zombies = 0; # 记录系统中僵尸进程的数目
$SIG{CHLD} = sub { $zombies++ }; # 信号处理程序中仅仅统计僵尸进程数目

# 主程序
while (1) {
if (fork() == 0) {
&child; # 如果当前进程是子进程,则执行 child 函数
exit; # 并退出
} else {
&REAPER if $zombies;
sleep 5; # 如果是父进程,则睡眠 5 秒
}
}

实际上,waitpid_test2.pl 并不能及时回收结束的子进程—— 由于 REAPER 在主程序中执行,如果子进程结束时主程序尚未执行到 REAPER 一行, 那么系统中可能会出现相当数量的僵尸进程,直到主程序执行 REAPER 为止。 不过一般情况下这种方法已经足够用了。

忽略 SIGCHLD 回收僵尸进程

另一个比较简单的方法就是直接忽略 SIGCHLD 信号,系统会自动回收结束的子进程。 参见下面的 ignore_sigchld_test.pl。

$SIG{CHLD} = 'IGNORE'; # 忽略 SIGCHLD 信号

与前面的 waitpid 方法相比,此方法虽然方便, 但缺点就是父进程无法在子进程结束时做些处理。 可根据实际需要选择最合适的方法

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读