Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230934
  • 博文数量: 131
  • 博客积分: 259
  • 博客等级: 二等列兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 21:15
文章分类

全部博文(131)

文章存档

2013年(3)

2011年(128)

分类:

2011-01-12 15:07:47

原文地址:得到进程返回值 作者:checl1987_EE

为了不同目的定义的宏(在‘;’)包括(stat是‘waitpid()’返回的值):

WIFEXITED(stat)'
     如果子进程正常退出则返回非0

`WEXITSTATUS(stat)'
     子进程返回的退出码

`WIFSIGNALED(stat)'
     如果子进程由与信号而 终止则返回非0

`WTERMSIG(stat)'
     终止子进程的信号代码

`WIFSTOPPED(stat)'
     如果子进程暂停(stopped)则返回非0

`WSTOPSIG(stat)'
     使子进程暂停的信号代码

`WIFCONTINUED(stat)'
     如果状态是表示子进程继续执行则返回非0

`WCOREDUMP(stat)'
     如果‘WIFSIGNALED(stat)’为非0,而如果这个进程产生一个内存映射文件
     (core dump)则返回非0


1,在程序中,用exit来设置进程的退出值时,虽然该函数的参数类型为int型,但再父进程中只能取到其值的低8位.所以用exit返回值时,高于255的值是没有意义的. 

2,对于system函数,返回值是由两部分组成的,低8位值表示所执行的脚本在执行过程中所接收到的信号值,其余的位表示的脚本exit退出时所设置的值, 

即脚本内exit退出是的值的低8位,在system返回值的低9-16位


例:
1  程序返回非0
#include
#include
#include
int main(int argc , char *argv[])
{
    int status = system("a");   //该目录下没有"a" 得到程序错误的返回值
    printf("%d == system(\"%s\");\n", WEXITSTATUS(status), "a");
    return 0;
}

chechunli@chechunli-desktop:~ $ gcc test.c -Wall -o test
chechunli@chechunli-desktop:~ $ ./test 
sh: a: not found
127 == system("a");


2 程序返回0
#include
#include
#include
int main(int argc , char *argv[])
{
    int status = system("ls");
    printf("%d == system(\"%s\");\n", WEXITSTATUS(status), "ls");
    return 0;
}

chechunli@chechunli-desktop:~ $ gcc test.c -Wall -o test
chechunli@chechunli-desktop:~ $ ./test 
test   test.c
0 == system("ls");



阅读(550) | 评论(0) | 转发(0) |
0

上一篇:errno

下一篇:vim的配置与使用

给主人留下些什么吧!~~