[Perl]REAPER

场景

在程序中遇到耗时较长的操作(如等待用户响应,连接remote server),通常会想到创建 Child process 去处理。在Perl 中 使用fork()

## fork a child process
die "$@" unless defined( my $child_pid = fork());
if ($child_pid) {  # If I have a child PID, then I must be the parent
    push @children_pids, $child_pid;
    print "children's PIDs: @children_pids\n";
} else { # I am the child
    my $wait_time = int(rand(10));
    sleep $wait_time;
    my $localtime = localtime;
    print "Child: Some child exited at $localtime\n";
    exit 0; # Exit the child
}

那么 Child process 的回收问题怎么解决?如果 Parent process 任由它自生自灭,就会导致Zombie process。

回收方法

有两种方式如下,无论哪种都涉及waitpid(),当然也可以使用wait()(不建议)

  • Blocking wait
  • Non blocking wait
    在此提一下waitpid()下文code有使用

waitpid PID,FLAGS
PID: -1(stands for any child process) | pid ( > 0 )
FLAGS: 0 (for a blocking wait) | WHOHANG (return 0 or -1if no dead children)
i.e,. waitpid($pid, 0) ## in a blocking wait
waitpid(-1, WHOHANG) ## no blocking wait

Blocking wait

见如下code:

my @children_pids;
print "Parent: my pid $$\n";
for my $count (1..10){
    die "$@" unless defined( my $child_pid = fork());
    if ($child_pid) {  # If I have a child PID, then I must be the parent
        push @children_pids, $child_pid;
        print "children's PIDs: @children_pids\n";
    } else { # I am the child
        my $wait_time = int(rand(10));
        sleep $wait_time;
        my $localtime = localtime;
        print "Child: Some child exited at $localtime\n";
        exit 0; # Exit the child
    }
}

foreach my $child (@children_pids) {
        print "Parent: Waiting on $child\n";
        waitpid($child, 0); ## will not go to next step unless $child reaped
        my $localtime = localtime;
        print "Parent: Child $child was reaped - $localtime.\n";
}

Output:

[root@VTB93-PC1 ~]# perl /tmp/test_fork.pl
Parent: my pid 15117
children's PIDs: 15118
children's PIDs: 15118 15119
children's PIDs: 15118 15119 15120
children's PIDs: 15118 15119 15120 15121
children's PIDs: 15118 15119 15120 15121 15122
children's PIDs: 15118 15119 15120 15121 15122 15123
children's PIDs: 15118 15119 15120 15121 15122 15123 15124
children's PIDs: 15118 15119 15120 15121 15122 15123 15124 15125
children's PIDs: 15118 15119 15120 15121 15122 15123 15124 15125 15126
children's PIDs: 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127
Parent: Waiting on 15118
Child: Some child exited at Thu Apr 21 13:28:48 2016
Parent: Child 15118 was reaped - Thu Apr 21 13:28:48 2016.
Parent: Waiting on 15119
Child: Some child exited at Thu Apr 21 13:28:48 2016
Child: Some child exited at Thu Apr 21 13:28:48 2016
Child: Some child exited at Thu Apr 21 13:28:50 2016
Child: Some child exited at Thu Apr 21 13:28:51 2016
Parent: Child 15119 was reaped - Thu Apr 21 13:28:51 2016.
Parent: Waiting on 15120
Child: Some child exited at Thu Apr 21 13:28:51 2016
Child: Some child exited at Thu Apr 21 13:28:52 2016
Child: Some child exited at Thu Apr 21 13:28:53 2016
Child: Some child exited at Thu Apr 21 13:28:54 2016
Parent: Child 15120 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15121
Parent: Child 15121 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15122
Parent: Child 15122 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15123
Child: Some child exited at Thu Apr 21 13:28:54 2016
Parent: Child 15123 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15124
Parent: Child 15124 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15125
Parent: Child 15125 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15126
Parent: Child 15126 was reaped - Thu Apr 21 13:28:54 2016.
Parent: Waiting on 15127
Parent: Child 15127 was reaped - Thu Apr 21 13:28:54 2016.

可以看出规律,回收的顺序是按照`fork()`的顺序, 可在实际中 child process 耗时有长有短,后fork()的process很有可能比较早的先结束,所以引入第二种Reap机制,使用$SIG{CHLD}

Non blocking wait

这里先提一下$SIG{CHLD}, 可接受的赋值如下:

$SIG{CHLD} = 'IGNORE'; ## Children reaped by system
$SIG{CHLD} = 'DEFAULT'; ## System defined
$SIG{CHLD} = &REAPER; ## do REAPER if SIGCHLD catched

use POSIX ":sys_wait_h";

$SIG{CHLD}=\&REAPER;
sub REAPER {
    my $child;
    while(( $child = waitpid(-1, &WNOHANG)) > 0){
        my $localtime = localtime;
        print "Parent: Child $child was reaped - $localtime.\n";
    }
    $SIG{CHLD}=\&REAPER;
}
my @children_pids;
print "Parent: my pid $$\n";
for my $count (1..10){
    die "$@" unless defined( my $child_pid = fork());
    if ($child_pid) {  # If I have a child PID, then I must be the parent
        push @children_pids, $child_pid;
        print "children's PIDs: @children_pids\n";
    } else { # I am the child
        my $wait_time = int(rand(10));
        sleep $wait_time;
        my $localtime = localtime;
        print "Child: Some child exited at $localtime\n";
        exit 0; # Exit the child
    }
}
## Keep parent alive to reap all children
while (1) {
    sleep;
}

Outpuit:

Parent: my pid 15189
children's PIDs: 15194
children's PIDs: 15194 15195
children's PIDs: 15194 15195 15196
children's PIDs: 15194 15195 15196 15197
children's PIDs: 15194 15195 15196 15197 15198
children's PIDs: 15194 15195 15196 15197 15198 15199
children's PIDs: 15194 15195 15196 15197 15198 15199 15200
children's PIDs: 15194 15195 15196 15197 15198 15199 15200 15201
children's PIDs: 15194 15195 15196 15197 15198 15199 15200 15201 15202
children's PIDs: 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203
Child: Some child exited at Thu Apr 21 13:42:25 2016
Parent: Child 15202 was reaped - Thu Apr 21 13:42:25 2016.
Child: Some child exited at Thu Apr 21 13:42:27 2016
Parent: Child 15197 was reaped - Thu Apr 21 13:42:27 2016.
Child: Some child exited at Thu Apr 21 13:42:29 2016
Parent: Child 15201 was reaped - Thu Apr 21 13:42:29 2016.
Child: Some child exited at Thu Apr 21 13:42:30 2016
Parent: Child 15194 was reaped - Thu Apr 21 13:42:30 2016.
Child: Some child exited at Thu Apr 21 13:42:31 2016
Parent: Child 15198 was reaped - Thu Apr 21 13:42:31 2016.
Child: Some child exited at Thu Apr 21 13:42:31 2016
Parent: Child 15200 was reaped - Thu Apr 21 13:42:31 2016.
Child: Some child exited at Thu Apr 21 13:42:31 2016
Parent: Child 15203 was reaped - Thu Apr 21 13:42:31 2016.
Child: Some child exited at Thu Apr 21 13:42:32 2016
Parent: Child 15199 was reaped - Thu Apr 21 13:42:32 2016.
Child: Some child exited at Thu Apr 21 13:42:33 2016
Parent: Child 15195 was reaped - Thu Apr 21 13:42:33 2016.
Child: Some child exited at Thu Apr 21 13:42:34 2016
Parent: Child 15196 was reaped - Thu Apr 21 13:42:34 2016.

可以看出但某个Child exit 后会发送SIGCHLD给Parent,顺序依据exit先后。当然,这是Parent跟踪Children(如获取返回信息local $?,以上都返回 0 )作出相应操作。如果无需如此,可直接$SIG{CHLD}='INGNORE'; 交给OS吧。

总结

  • 使用 SIGCHLD REAPER 更符合现实场景,结合waitpid()Non blocking wait 方式
  • Parent 负责 Children 的生命周期,以免出现 Zombie process
  • PID 1 init ## 所有Process 的 parent
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容