2010-05-25 43 views
6

考慮的fork()這個簡單的例子荷蘭國際集團,然後等待孩子的Perl死:

#!/usr/bin/perl 

use strict; 
use warnings; 

if (fork() == 0) { 
     exit(1); 
} 

waitpid(-1,0); 

print $?; 

運行在Solaris 10上的腳本中,我得到這樣的結果:

$ perl test.pl 
256 

我懷疑的是值被向上移動,因爲當我在exit(2)在孩子,輸出變成512

我似乎無法在perl的waitpid中找到此記錄。這是我的系統上的錯誤還是我做錯了什麼?

回答

23

它記錄在perlvar手冊頁的$?部分。

即實際退出代碼是$? >> 8

+9

它也包含在[perldoc -f系統](http://perldoc.perl.org/functions/system.html)中。 – Ether 2010-05-25 22:54:24

1

孩子可能甚至沒有打電話給exit。因此,$?包含的信息不僅僅是exit參數。

if ($? == -1 ) { die "Can't launch child: $!\n"; } 
elsif ($? & 0x7F) { die "Child killed by signal ".($? & 0x7F)."\n"; } 
elsif ($? >> 8 ) { die "Child exited with error ".($? >> 8)."\n"; } 
else    { print "Child executed successfully\n"; } 

這更詳細地記錄在system的文檔中。

相關問題