系统函数的返回值 [英] Return value of System Function

查看:40
本文介绍了系统函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试验了系统函数调用.它用于执行程序中的命令.手册页包含以下有关系统返回值的信息.

I Experimented the system function call. It is used to execute the command with in the program. The man page contains the following information about the return value of system.

返回值

The value returned is -1 on error (e.g.  fork(2) failed), and the return status of the command otherwise.

根据手册页参考,我检查了以下程序.

As per the man page reference, I checked the following program.

程序:

#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 命令的退出状态.但价值变得不同.为什么?

The above output shows that, return value of system function is 512 and the exit status of ls a command is 2. As per the reference, I expect the return value of system function is equal to exit status of the ls command. But the value becomes different. Why?

推荐答案

您忘记阅读整个部分.

[T]他的返回值是一个等待状态",可以使用 waitpid(2) 中描述的宏检查.(即,WIFEXITED()WEXITSTATUS() 等等).

[T]he return value is a "wait status" that can be examined using the macros described in waitpid(2). (i.e., WIFEXITED() WEXITSTATUS() and so on).

这篇关于系统函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆