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

在perl中,我们如何检测外部命令中的分段错误

发布时间:2020-12-15 21:55:11 所属栏目:大数据 来源:网络整理
导读:以下是注定要崩溃的C代码: #includestdio.h#includestdlib.hint main() { char *p = NULL; printf("Value at P: %cn",*p); return 0;} 当我编译并运行它(RH4机器与gcc 4.5.2)时,它可预测地给出了一个分段错误: % ./a.outSegmentation fault% echo $status
以下是注定要崩溃的C代码:
#include<stdio.h>
#include<stdlib.h>

int main() {
    char *p = NULL;
    printf("Value at P: %cn",*p);
    return 0;
}

当我编译并运行它(RH4机器与gcc 4.5.2)时,它可预测地给出了一个分段错误:

%  ./a.out
Segmentation fault

%  echo $status
139

如果我使用Perl v5.8.5运行它,会发生这种情况:

%  perl -e 'system("./a.out") and die "Status: $?"'
Status: 11 at -e line 1.

perlvar文件的价格是多少?说

Thus,the exit value of the subprocess is really ($?>> 8 ),and $? &
127
gives which signal,if any,the process died from,and $? & 128
reports whether there was a core dump.

11>> 8是0,11& 127是11.

为什么不同的退出状态?如果我们不能依赖退出状态,那么在外部命令中检测分段错误的方法应该是什么?

解决方法

阅读系统文档可能会回答您的问题:
system('a.out');

if ($? == -1) {
    print "failed to execute: $!n";
}
elsif ($? & 127) {
    printf "child died with signal %d,%s coredumpn",($? & 127),($? & 128) ? 'with' : 'without';
}
else {
    printf "child exited with value %dn",$? >> 8;
}

输出:

child died with signal 11,without coredump

shell只是以不同的方式对状态中的信号进行编码:139 – 128 = 11.例如,man bash说:

The return value of a simple command is its exit status,or 128+n if the command is terminated by signal n.

(编辑:李大同)

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

    推荐文章
      热点阅读