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

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <errno.h> int main() {
pid_t pid, rpid;
int stat;
if ((pid = fork()) < ) {
perror("fork failue.");
}
printf("pid = %d\n", pid); // 打印子父进程的获得的返回值
if (pid == ) {
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(-, &stat, ); // 等待任意子进程结束
if (WIFEXITED(stat)) { // 状态字拥有24位,一些宏函数对状态字进行解释
printf("rpid = %d, stat = %d\n", rpid, WEXITSTATUS(stat));
}
}
return ;
}

程序A

#include <stdio.h>

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

程序B

调用成功后:

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码含义,来自网络http://hi.baidu.com/henfengduandie/item/7c06bdb8e3a617f362388eb6

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 not attached
errno.50 is: No CSI structure available
errno.51 is: Level 2 halted
errno.52 is: Invalid exchange
errno.53 is: Invalid request descriptor
errno.54 is: Exchange full
errno.55 is: No anode
errno.56 is: Invalid request code
errno.57 is: Invalid slot
errno.58 is: Unknown error 58
errno.59 is: Bad font file format
errno.60 is: Device not a stream
errno.61 is: No data available
errno.62 is: Timer expired
errno.63 is: Out of streams resources
errno.64 is: Machine is not on the network
errno.65 is: Package not installed
errno.66 is: Object is remote
errno.67 is: Link has been severed
errno.68 is: Advertise error
errno.69 is: Srmount error
errno.70 is: Communication error on send
errno.71 is: Protocol error
errno.72 is: Multihop attempted
errno.73 is: RFS specific error
errno.74 is: Bad message
errno.75 is: Value too large for defined data type
errno.76 is: Name not unique on network
errno.77 is: File descriptor in bad state
errno.78 is: Remote address changed
errno.79 is: Can not access a needed shared library
errno.80 is: Accessing a corrupted shared library
errno.81 is: .lib secion in a.out corrupted
errno.82 is: Attempting to link in too many shared libraries
errno.83 is: Cannot exec a shared library directly
errno.84 is: Invalid or incomplete multibyte or wide character
errno.85 is: Interrupted system call should be restarted
errno.86 is: Streams pipe error
errno.87 is: Too many users
errno.88 is: Socket operation on non-socket
errno.89 is: Destination address required
errno.90 is: Message too long
errno.91 is: Protocol wrong type for socket
errno.92 is: Protocol not available
errno.93 is: Protocol not supported
errno.94 is: Socket type not supported
errno.95 is: Operation not supported
errno.96 is: Protocol family not supported
errno.97 is: Address family not supported by protocol
errno.98 is: Address already in use
errno.99 is: Cannot assign requested address
errno.100 is: Network is down
errno.101 is: Network is unreachable
errno.102 is: Network dropped connection on reset
errno.103 is: Software caused connection abort
errno.104 is: Connection reset by peer
errno.105 is: No buffer space available
errno.106 is: Transport endpoint is already connected
errno.107 is: Transport endpoint is not connected
errno.108 is: Cannot send after transport endpoint shutdown
errno.109 is: Too many references: cannot splice
errno.110 is: Connection timed out
errno.111 is: Connection refused
errno.112 is: Host is down
errno.113 is: No route to host
errno.114 is: Operation already in progress
errno.115 is: Operation now in progress
errno.116 is: Stale NFS file handle
errno.117 is: Structure needs cleaning
errno.118 is: Not a XENIX named type file
errno.119 is: No XENIX semaphores available
errno.120 is: Is a named type file
errno.121 is: Remote I/O error
errno.122 is: Disk quota exceeded
errno.123 is: No medium found
errno.124 is: Wrong medium type
errno.125 is: Operation canceled
errno.126 is: Required key not available
errno.127 is: Key has expired
errno.128 is: Key has been revoked
errno.129 is: Key was rejected by service
errno.130 is: Owner died
errno.131 is: State not recoverable
errno.132 is: Unknown error 132
132-255全是Unknown error

linux编程中接收主函数返回值以及错误码提示的更多相关文章

  1. Linux 编程中的API函数和系统调用的关系【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...

  2. C# ODP.NET 调用Oracle函数返回值时报错的一个解决方案

    有人在社区问到:C#调用Oracle中自定义函数的返回值时,无法正常调用.但在PL/SQL中正常调用返回. 于是动手一试: 1.准备函数(Oralce 11g.2.0.0.4) CREATE OR R ...

  3. 转 Perl函数返回值用法指导

    http://developer.51cto.com/art/201007/213003.htm Perl函数返回值用法指导   Perl编程语言中Perl函数返回值用法你是否比较熟悉,这里向大家简单 ...

  4. Linux Shell函数返回值

    转:http://blog.csdn.net/ithomer/article/details/7954577 Shell函数返回值,一般有3种方式:return,argv,echo 1) return ...

  5. 关于QT中evaluateJavaScript()函数返回值的处理问题

    关于QT中evaluateJavaScript()函数返回值的处理问题 - 寒风问雪的专栏 - 博客频道 - CSDN.NET 关于QT中evaluateJavaScript()函数返回值的处理问题 ...

  6. Linux Shell 函数返回值

    Shell函数返回值,常用的两种方式:return,echo 1) return 语句 shell函数的返回值,可以和其他语言的返回值一样,通过return语句返回. 示例: #!/bin/sh fu ...

  7. 获得函数返回值类型、参数tuple、成员函数指针中的对象类型

    //function_traits.h,获得函数返回值类型.参数tuple.成员函数指针中的对象类型 //参考https://github.com/qicosmos/cosmos/blob/maste ...

  8. Linux system函数返回值

    例: status = system("./test.sh"); 1.先统一两个说法: (1)system返回值:指调用system函数后的返回值,比如上例中status为syst ...

  9. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

随机推荐

  1. laravel5.3----------调度任务以及Artisan

    1.在使用的过程中会遇到有些函数不能用需要在php配置文件里面打开: disable_functions = exec,passthru,popen,proc_open,shell_exec,syst ...

  2. git-----------------git:如何让git识别我修改了文件夹名字和文件名字的大小写问题。

    修改每个项目里面的隐藏的.git文件里面的config文件.将箭头指的原本是true改成false.

  3. eclipse自动补全快捷键失效,sysout用不了!

    好久没写Java代码了,使用新版Neon的Eclipse Java EE IDE开发时,自动补全各种失败,sysout也各种用不了, 开始还以为是电脑卡比呢,原来是版本的快捷键不同了,修改方法如下! ...

  4. 无语啊,sublime给我弄乱玩,玩坏了,而且安装插件也安装不了

    国内的什么插件地址都TMMD失效了,没办法,只能翻"强"到外面找了,而且找了很多也用不了,所以收藏一个为了预防以后不行有补救的方法: 百度的99%都不行,不是报这个错就是那个错,可 ...

  5. Hadoop namenode无法启动

    最近遇到了一个问题,执行start-all.sh的时候发现JPS一下namenode没有启动        每次开机都得重新格式化一下namenode才可以        其实问题就出在tmp文件,默 ...

  6. [原] MSSQL MDF文件太大问题

    USE 你的库名; GO -- 数据库空间使用情况 EXEC sp_spaceused; -- 查下文件空间使用情况 SELECT file_id, name, [文件大小(MB)] = size / ...

  7. 整盘恢复黑苹果后,重新安装Win7,卡在正在启动

    是这样的: GHOST整个黑苹果的镜像,然后恢复到Thinkpad e450c上,能启动,但是驱动不对,最主要是网卡驱动不了(据说) 然后重新分区,安装Win7 哦豁 卡在 正在启动windows 多 ...

  8. python学习笔记之类class(第六天)

         参考文档:              1.金角大王博客:http://www.cnblogs.com/alex3714/articles/5188179.html               ...

  9. Swift语言之类型方法

    Swift语言有很多特性,其中之一就是类型方法,相对于其他比较流行的编程语言(C#.Java),在Swift中类型方法最大的特征在于它的可继承性,我们举个例子说明: 俗话说,龙生龙凤生凤老鼠生儿会打洞 ...

  10. 前端使用js读取文件

    最近同时问我js能不能读取本地文件: 想起以前看到js读取本地文件的文章,然后自己写了个demo. ps:这有点想Java的IO流,但是又有差别. 首先我们定义一个input标签type=" ...