<分区>
我试验了系统函数调用。它用于执行程序中的命令。手册页包含以下有关系统返回值的信息。
返回值
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 命令的退出状态。但是值(value)变得不同了。
为什么?