[Perl]REAPER

場景

在程序中遇到耗時(shí)較長的操作(如等待用戶響應(yīng),連接remote server),通常會想到創(chuàng)建 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 任由它自生自滅,就會導(dǎo)致Zombie process肯骇。

回收方法

有兩種方式如下窥浪,無論哪種都涉及waitpid(),當(dāng)然也可以使用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.

可以看出規(guī)律,回收的順序是按照`fork()`的順序, 可在實(shí)際中 child process 耗時(shí)有長有短,后fork()的process很有可能比較早的先結(jié)束笛丙,所以引入第二種Reap機(jī)制漾脂,使用$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.

可以看出但某個(gè)Child exit 后會發(fā)送SIGCHLD給Parent,順序依據(jù)exit先后胚鸯。當(dāng)然符相,這是Parent跟蹤C(jī)hildren(如獲取返回信息local $?,以上都返回 0 )作出相應(yīng)操作蠢琳。如果無需如此啊终,可直接$SIG{CHLD}='INGNORE'; 交給OS吧。

總結(jié)

  • 使用 SIGCHLD REAPER 更符合現(xiàn)實(shí)場景傲须,結(jié)合waitpid()Non blocking wait 方式
  • Parent 負(fù)責(zé) Children 的生命周期蓝牲,以免出現(xiàn) Zombie process
  • PID 1 init ## 所有Process 的 parent
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市泰讽,隨后出現(xiàn)的幾起案子例衍,更是在濱河造成了極大的恐慌,老刑警劉巖已卸,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件佛玄,死亡現(xiàn)場離奇詭異,居然都是意外死亡累澡,警方通過查閱死者的電腦和手機(jī)梦抢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來愧哟,“玉大人奥吩,你說我怎么就攤上這事∪镂啵” “怎么了霞赫?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長肥矢。 經(jīng)常有香客問我端衰,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任旅东,我火速辦了婚禮惕味,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘玉锌。我一直安慰自己名挥,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布主守。 她就那樣靜靜地躺著禀倔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪参淫。 梳的紋絲不亂的頭發(fā)上救湖,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機(jī)與錄音涎才,去河邊找鬼鞋既。 笑死,一個(gè)胖子當(dāng)著我的面吹牛耍铜,可吹牛的內(nèi)容都是我干的邑闺。 我是一名探鬼主播,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼棕兼,長吁一口氣:“原來是場噩夢啊……” “哼陡舅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起伴挚,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤靶衍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后茎芋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體颅眶,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年田弥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涛酗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,117評論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡皱蹦,死狀恐怖煤杀,靈堂內(nèi)的尸體忽然破棺而出眷蜈,到底是詐尸還是另有隱情沪哺,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布酌儒,位于F島的核電站辜妓,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜籍滴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一酪夷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧孽惰,春花似錦晚岭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至狂鞋,卻和暖如春片择,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背骚揍。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工字管, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人信不。 一個(gè)月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓嘲叔,卻偏偏與公主長得像,于是被迫代替她去往敵國和親抽活。 傳聞我的和親對象是個(gè)殘疾皇子借跪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容

  • Python處理Windows進(jìn)程 psutil(Python system and process utilit...
    Zhaifg閱讀 22,476評論 0 13
  • 最近一直在看游雙的《高性能linux服務(wù)器編程》一書,下載鏈接: http://download.csdn.net...
    張小方閱讀 1,184評論 0 2
  • 又來到了一個(gè)老生常談的問題酌壕,應(yīng)用層軟件開發(fā)的程序員要不要了解和深入學(xué)習(xí)操作系統(tǒng)呢掏愁? 今天就這個(gè)問題開始,來談?wù)劜?..
    tangsl閱讀 4,088評論 0 23
  • 最幸福的就是我了卵牍,今天看到一唯美之文果港,忍不住一遍遍讀來,仿佛心情悵然豁亮了許多糊昙,摘錄下來辛掠,心情煩躁之時(shí)得以靜心可好...
    那一年的初夏閱讀 189評論 0 0
  • 一把刀生銹了,被棄置在一角释牺。這把刀曾經(jīng)鋒利萝衩,被人天天使用,然而現(xiàn)在它卻銹跡斑斑没咙,被冷落了猩谊。也許是主人又有了新的刀,...
    蘿卜三叔閱讀 572評論 0 0