设为首页 加入收藏

TOP

Linux编程中接收主函数返回值以及错误码提示(一)
2015-02-02 14:22:42 来源: 作者: 【 】 浏览:29
Tags:Linux 编程 接收 函数 返回 以及 错误 提示

程序A创建子进程,并调用进程B,根据不调用的不同情况,最后显示结果不同。


程序A:


#include
#include
#include
#include
#include



int main() {
? ? pid_t pid, rpid;
? ? int stat;
? ? if ((pid = fork()) < 0) {
? ? ? ? perror("fork failue.");
? ? }
? ? printf("pid = %d\n", pid); // 打印子父进程的获得的返回值
? ? if (pid == 0) {
? ? ? ? int x = execl("./test123", "./test", NULL);
? ? ? ? printf("x = %d\n", x); // execl调用失败返回-1
? ? ? ? perror("execl failue"); // 直接使用perror函数输出错误字符串(相比数字,字符串更容易理解)
? ? ? ? printf("%s\n", strerror(errno)); // 间接使用strerror输出错误字符串
? ? } else {
? ? ? ? rpid = waitpid(-1, &stat, 0); // 等待任意子进程结束
? ? ? ? if (WIFEXITED(stat)) { // 状态字拥有24位,一些宏函数对状态字进行解释
? ? ? ? ? ? printf("rpid = %d, stat = %d\n", rpid, WEXITSTATUS(stat));
? ? ? ? }
? ? }
? ? return 0;
}


程序B:


#include


int main(int argc, char **argv) {
? ? printf("hello world.\n");
? ? exit(0x3244); // return与exit均能正常返回,返回值为 int & 0xff
}


调用成功后:


liuxu@kylin:~/projects/webserver$ ./ptest
pid = 4723
pid = 0
hello world.
rpid = 4723, stat = 68


?


调用失败后:


liuxu@kylin:~/projects/webserver$ ./ptest
pid = 4738
pid = 0
x = -1
execl failue: No such file or directory
No such file or directory
rpid = 4738, stat = 0


PS:error码含义


errno.00 is: Success? ? 成功
errno.01 is: Operation not permitted? ? ? ? 不允许此类操作
errno.02 is: No such file or directory? ? ? ? 没有此文件或目录
errno.03 is: No such process? ? ? ? ? ? ? ? ? ? 没有此进程
errno.04 is: Interrupted system call? ? ? ? ? 中断系统调用
errno.05 is: Input/output error? ? ? ? ? ? ? ? ? 输入输出错误
errno.06 is: No such device or address? ? 没有此设备或地址
errno.07 is: Argument list too long? ? ? ? ? ? 参数过长
errno.08 is: Exec format error? ? ? ? ? ? ? ? ? 执行格式错误
errno.09 is: Bad file descriptor? ? ? ? ? ? ? ? ? 无效的文件描述符
errno.10 is: No child processes? ? ? ? ? ? ? ? 不存在子进程
errno.11 is: Resource temporarily unavailable? 资源暂时不可用
errno.12 is: Cannot allocate memory? ? ? ? 分配内存失败
errno.13 is: Permission denied? ? ? ? ? ? ? ? ? 权限错误
errno.14 is: Bad address? ? ? ? ? ? ? ? ? ? ? ? ? 地址错误
errno.15 is: Block device required? ? ? ? ? ? 块设备请求
errno.16 is: Device or resource busy? ? ? ? 设备或资源忙
errno.17 is: File exists? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 文件已经存在
errno.18 is: Invalid cross-device link? ? ? ? ? 无效的交叉连接设备
errno.19 is: No such device? ? ? ? ? ? ? ? ? ? ? 没有此设备
errno.20 is: Not a directory? ? ? ? ? ? ? ? ? ? ? ? 不是一个目录
errno.21 is: Is a directory? ? ? ? ? ? ? ? ? ? ? ? ? 是一个目录
errno.22 is: Invalid argument? ? ? ? ? ? ? ? ? ? 参数非法
errno.23 is: Too many open files in system? ? 系统打开文件太多
errno.24 is: Too many open files? ? ? ? ? ? ? 打开文件太多
errno.25 is: Inappropriate ioctl for device? ? ? ? ? 设备部支持该操作
errno.26 is: Text file busy? ? ? ? ? ? ? ? ? ? ? ? ? 文本文件忙
errno.27 is: File too large? ? ? ? ? ? ? ? ? ? ? ? ? 文件太大
errno.28 is: No space left on device? ? ? ? ? ? 设备没有空间,一般为存储设备
errno.29 is: Illegal seek? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 非法的seek操作
errno.30 is: Read-only file system? ? ? ? ? ? ? 只读文件系统
errno.31 is: Too many links? ? ? ? ? ? ? ? ? ? ? ? 太多连接
errno.32 is: Broken pipe? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 管道破裂
errno.33 is: Numerical argument out of domain? ? ? ? ? 数值参数超出取值范围
errno.34 is: Numerical result out of range? ? ? ? ? ? ? ? ? 数值结果超出取值范围
errno.35 is: Resource deadlock avoided? ? ? ? ? ? ? ? ? 资源死锁
errno.36 is: File name too long? ? ? ? ? ? ? ? ? ? ? ? 文件名太长
errno.37 is: No locks available? ? ? ? ? ? ? ? ? ? ? ? 没有可用的锁
errno.38 is: Function not implemented? ? ? ? ? ? 函数没有执行
errno.39 is: Directory not empty? ? ? ? ? ? ? ? ? ? ? 目录非空
errno.40 is: Too many levels of symbolic links 太多级符号连接
errno.41 is: Unknown error 41? ? ? ? ? ? ? ? ? ? ? ? 41未知错误
errno.42 is: No message of desired type? ? ? ? 不被接受的消息类型
errno.43 is: Identifier removed? ? ? ? ? ? ? ? ? ? ? ? 标识符已被删除
errno.44 is: Channel number out of range? ? 。。。
errno.45 is: Level 2 not synchronized
errno.46 is: Level 3 halted
errno.47 is: Level 3 reset
errno.48 is: Link number out of range
errno.49 is: Protocol driver no

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉排序树实现(C++封装) 下一篇Effective Java - 用静态工厂方法..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: