【发布时间】:2016-01-12 07:45:26
【问题描述】:
我试验了系统函数调用。它用于执行程序中的命令。手册页包含以下有关 system 的返回值的信息。
返回值
The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise.
根据手册页参考,我检查了以下程序。
程序:
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("System returns: %d\n",system("ls a"));
}
输出:
$ ./a.out
ls: cannot access a: No such file or directory
System returns: 512
$ ls a
ls: cannot access a: No such file or directory
mohanraj@ltsp63:~/Advanced_Unix/Chapter8/check$ echo $?
2
$
以上输出显示,系统函数的返回值为 512,ls a 命令的退出状态为 2。根据
参考,我希望系统函数的返回值等于 ls 命令的退出状态。但价值变得不同。
为什么?
【问题讨论】:
-
您可能希望在该值上使用
WEXITSTATUS(取决于适当的条件)。 -
那么,我怎样才能得到传递给系统函数的命令的真正退出状态。
-
从“If status is not NULL”开始读取链接的文档,大约一半。