0
点赞
收藏
分享

微信扫一扫

system 使用checklist

函数定义
        int system(const char *command) ;
函数说明
        system()会调用fork()产生子进程,由子进程来调用/bin/sh-c command来执行参数command字符串所代表的命令,此命令执行完后随即返回原调用的进程。在调用
system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。

返回值

=-1:出现错误
=0:调用成功但是没有出现子进程
>0:成功退出的子进程的id

WIFEXITED/WEXITSTATUS/WIFSIGNALED
If the exit status value (*note Program Termination::) of the child
process is zero, then the status value reported by `waitpid' or `wait'
is also zero. You can test for other kinds of information encoded in
the returned status value using the following macros. These macros are
defined in the header file `sys/wait.h'.
-- Macro: int WIFEXITED (int STATUS)
This macro returns a nonzero value if the child process terminated
normally with `exit' or `_exit'.
-- Macro: int WEXITSTATUS (int STATUS)
If `WIFEXITED' is true of STATUS, this macro returns the low-order
8 bits of the exit status value from the child process. *Note
Exit Status::.
-- Macro: int WIFSIGNALED (int STATUS)
This macro returns a nonzero value if the child process terminated
because it received a signal that was not handled. *Note Signal
Handling::.

 

子进程的结束状态返回后存于status,底下有几个宏可判别结束情况
WIFEXITED(status)如果子进程正常结束则为非0值。
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。
WSTOPSIG(status)取得引发子进程暂停的信号代码

note:不通过比较返回值是否为-1(或<0)来判断system执行是否成功。而应该通过WIFEXITED、WEXITSTATUS、WIFSIGNALED等宏来处理system的返回值

int status;

status= system("ls -l");
/*需检测system()返回值*/
if(WIFSIGNALED(status) && (WTERMSIG(status)==SIGINT || WTERMSIG(status)==SIGQUIT))

需要判断
status != 1
WIFEXITED(status)为真
0=WEXITSTATUS(status)
都满足表示执行成功

 

http代理服务器(3-4-7层代理)-网络事件库公共组件、内核kernel驱动 摄像头驱动 tcpip网络协议栈、netfilter、bridge 好像看过!!!! 但行好事 莫问前程 --身高体重180的胖子

举报

相关推荐

0 条评论