system函数返回值测试分析
来源:互联网 发布:python wordcloud安装 编辑:程序博客网 时间:2024/06/05 16:46
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
阶段二:调用/bin/sh/拉起shell脚本,如果如果/bin/sh拉起shell命令失败,或者是shell命令没有正常执行 (比如命令根本就是非法的命令),那么,将原因填入status的8~15位。
阶段三:如果shell顺利执行完毕,那么将shell的返回值填到system返回值的8~15位<strong>
实际测试代码
#include <stdio.h>#include <stdlib.h>int main(int argc, char* argv[]){ int result = 0;if (argc != 2){printf("args is not equal 2! \n");return 1;}result = system(argv[1]);printf("system return value is %d. \n", result);return 0;}
测试结果
root@test:/opt/tmp# cat test.sh
#!/bin/bash
exit 1
root@test:/opt/tmp# ./systemtest /opt/tmp/test.sh
system return value is 256.
root@test:/opt/tmp# vi test.sh
root@test:/opt/tmp# cat test.sh
#!/bin/bash
exit 0
root@test:/opt/tmp# ./systemtest /opt/tmp/test.sh
system return value is 0.
root@test:/opt/tmp# rm test.sh
root@test:/opt/tmp# ./systemtest /opt/tmp/test.sh
sh: 1: /opt/tmp/test.sh: not found
system return value is 32512.
35152=256*127
- system函数返回值测试分析
- system()返回值分析
- system函数返回值
- system函数返回值
- system函数返回值
- system函数返回值
- system函数返回值
- system函数返回值
- Linux system函数返回值
- Linux system 函数返回值
- Linux system函数返回值
- Linux system函数返回值
- Linux system函数返回值
- system函数返回值探究
- Linux system函数返回值
- Linux system函数返回值
- Linux system函数返回值
- Linux system函数返回值
- 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ
- 爬取chaoshi.tmall商品详情
- python编写socket
- 机器学习之k近邻算法——4、特征值归一化
- tyvj P1003 越野跑
- system函数返回值测试分析
- listView实现分页加载数据
- CMD命令更改文件属性
- 撞库攻击:一场需要用户参与的持久战
- [LeetCode]***Binary Tree Level Order Traversal(查代码)
- margin:0 auto 和text-align:center的区别是什么???
- 生成缩略图
- 常量指针、指针常量、指向常量的指针常量
- 缩略图两种方法