system函数返回值测试分析

本文详细介绍了system函数的返回值分析,包括错误返回-1、shell执行失败的情况及shell正常执行时如何获取退出状态。通过实际测试代码展示了不同情况下system函数返回值的变化,例如当脚本存在并正常退出、不存在或执行错误时的返回值。
摘要由CSDN通过智能技术生成

man system可以看到如下返回值说明

       The value returned is -1 on error (e.g.  fork(2) failed), and the return status of the command otherwise.   This  latter  return
       status  is in the format specified in wait(2).  Thus, the exit code of the command will be WEXITSTATUS(status).  In case /bin/sh
       could not be executed, the exit status will be that of a command that does exit(127).
       If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.
       system() does not affect the wait status of any other children.<strong>

system是个综合的操作,分解开来看就是相当于执行了
1.fork  生成一个子进程。
2.在子进程执行 execl("/bin/sh","sh","-c" command,(char*)0);
3.waitpid<strong>
system函数对返回值的处理分3个阶段
阶段一:创建子进程等准备工作,失败返回-1

阶段二:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值