转载自:http://my.oschina.net/renhc/blog/53580
linux尽量避免使用system。
曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入。只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值、它所执行命令的返回值以及命令执行失败原因如何定位,这才是重点。当初因为这个函数风险较多,故抛弃不用,改用其他的方法。这里先不说我用了什么方法,这里必须要搞懂system()函数,因为还是有很多人用了system()函数,有时你不得不面对它。
先来看一下system()函数的简单介绍:
1 #include <stdlib.h>
2 int system(const char *command);

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.

system()函数调用/bin/sh来执行参数指定的命令,/bin/sh 一般是一个软连接,指向某个具体的shell,比如bash,-c选项是告诉shell从字符串command中读取命令;
在该command执行期间,SIGCHLD是被阻塞的,好比在说:hi,内核,这会不要给我送SIGCHLD信号,等我忙完再说;
在该command执行期间,SIGINT和SIGQUIT是被忽略的,意思是进程收到这两个信号后没有任何动作。
再来看一下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()函数返回值,需要了解其执行过程,实际上system()函数执行了三步操作:
1.fork一个子进程;
2.在子进程中调用exec函数去执行command;
3.在父进程中调用wait去等待子进程结束。
对于fork失败,system()函数返回-1。
如果exec执行成功,也即command顺利执行完毕,则返回command通过exit或return返回的值。
(注意,command顺利执行不代表执行成功,比如command:"rm debuglog.txt",不管文件存不存在该command都顺利执行了)
如果exec执行失败,也即command没有顺利执行,比如被信号中断,或者command命令根本不存在,system()函数返回127.
如果command为NULL,则system()函数返回非0值,一般为1.
看一下system()函数的源码
看完这些,我想肯定有人对system()函数返回值还是不清楚,看源码最清楚,下面给出一个system()函数的实现:
01 int system(const char * cmdstring)
02 {
03     pid_t pid;
04     int status;
05  
06 if(cmdstring == NULL)
07 {
08     return (1); //如果cmdstring为空,返回非零值,一般为1
09 }
10  
11 if((pid = fork())<0)
12 {
13     status = -1; //fork失败,返回-1
14 }
15 else if(pid == 0)
16 {
17     execl("/bin/sh""sh""-c", cmdstring, (char *)0);
18     _exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~
19 }
20 else //父进程
21 {
22     while(waitpid(pid, &status, 0) < 0)
23     {
24         if(errno != EINTR)
25         {
26             status = -1; //如果waitpid被信号中断,则返回-1
27             break;
28         }
29     }
30 }
31  
32     return status; //如果waitpid成功,则返回子进程的返回状态
33 }

仔细看完这个system()函数的简单实现,那么该函数的返回值就清晰了吧,那么什么时候system()函数返回0呢?只在command命令返回0时。

看一下该怎么监控system()函数执行状态
这里给我出的做法:
01 int status;
02 if(NULL == cmdstring) //如果cmdstring为空趁早闪退吧,尽管system()函数也能处理空指针
03 {
04     return XXX;
05 }
06 status = system(cmdstring);
07 if(status < 0)
08 {
09     printf("cmd: %s\t error: %s", cmdstring, strerror(errno)); // 这里务必要把errno信息输出或记入Log
10     return XXX;
11 }
12  
13 if(WIFEXITED(status))
14 {
15     printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
16 }
17 else if(WIFSIGNALED(status))
18 {
19     printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断,取得信号值
20 }
21 else if(WIFSTOPPED(status))
22 {
23     printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行,取得信号值
24 }

到于取得子进程返回值的相关介绍可以参考另一篇文章:http://my.oschina.net/renhc/blog/35116

system()函数用起来很容易出错,返回值太多,而且返回值很容易跟command的返回值混淆。这里推荐使用popen()函数替代,关于popen()函数的简单使用也可以通过上面的链接查看。

popen()函数较于system()函数的优势在于使用简单,popen()函数只返回两个值:
成功返回子进程的status,使用WIFEXITED相关宏就可以取得command的返回结果;
失败返回-1,我们可以使用perro()函数或strerror()函数得到有用的错误信息。

这篇文章只涉及了system()函数的简单使用,还没有谈及SIGCHLD、SIGINT和SIGQUIT对system()函数的影响,事实上,之所以今天写这篇文章,是因为项目中因有人使用了system()函数而造成了很严重的事故。现像是system()函数执行时会产生一个错误:“No child processes”。

关于这个错误的分析,感兴趣的朋友可以看一下:http://my.oschina.net/renhc/blog/54582

Linux下使用system()函数一定要谨慎相关推荐

  1. 【C/C++】Linux下使用system()函数一定要谨慎

    曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原 ...

  2. Linux下使用system函数获取命令执行返回结果

    在Linux C语言中,需要获取设备挂载和空间容量信息,这时候最简单的方式就是使用命令工具进行查询,但是system函数调用之能返回进行执行的状态,不能返回执行的结果:所以这里自己实现system函数 ...

  3. linux getline参数,Linux下的getline函数

    最近在做国嵌的mp3项目,在mp3主控程序中用到了这个函数,挺好使的,在这里记录一下.注意是linux下的,不是C++中的. 函数原型 ssize_t getline(char **lineptr, ...

  4. linux windows c system 函数简介

    windows 在windows下的system函数中命令可以不区别大小写!  功 能: 发出一个DOS命令 #include <stdlib.h>int system(char *com ...

  5. sleep头文件linux,Linux下的sleep函数 要用的话得包涵什么头文件啊?

    Linux下的sleep函数 要用的话得需要#include sleep把进程的运行状态改为睡眠,将其从系统可执行队列去掉,这样系统就不会调度到该进程,不会分配CPU时间片,同时根据该进程的睡眠时间, ...

  6. linux父进程中显示子进程pid,请教linux下c语言函数fork父进程打印子进程的PID

    请教linux下c语言函数fork父进程打印子进程的PID 关注:296  答案:2  信息版本:手机版 解决时间 2019-01-14 04:55 雨不眠的下 2019-01-13 12:23 用于 ...

  7. [转帖]关于Linux下的icotl函数

    关于Linux下的icotl函数 最近接触android开发,因为有时间所以就关注了下android的源码,在跟踪源码过程中到最后都会遇到icotl函数,虽然在Symbian中曾经遇到过RSocket ...

  8. Linux下的延时函数

    Linux下的延时函数 1.sleep函数 头文件:#include<unistd.h> 功能:执行挂起操作一段时间,以秒为单位 一般形式:unsigned sleep(unsigned ...

  9. Linux下无法使用 itoa 函数的解决方法

    起因 在Linux环境下进行C++编程,使用 itoa 函数时出现以下错误: ' itoa ' was not declared in this scope. 翻阅Linux下的 stdlib.h 头 ...

最新文章

  1. 一篇文章全面了解监控知识体系--转
  2. 面试题热个身:5 亿整数的大文件,来排个序?
  3. php每个月头一天与最后一天,PHP获取每月第一天与最后一天
  4. Angular ERROR NullInjectorError: R3InjectorError(AppModule)的错误分析
  5. linux之可视化查看磁盘大小并且删除大文件
  6. Anaconda中软件库更新
  7. 【Pytorch神经网络基础理论篇】 07 线性回归 + 基础优化算法
  8. MySQL删除数据库
  9. centos 6.8安装git_git 入门教程之 git 私服搭建教程
  10. 基于JAVA+SpringBoot+Mybatis+MYSQL的今日头条新闻网站
  11. 故障恢复 stm32_77条STM32知识,千万不能错过!干货
  12. dataTransfer.getData()在dragover,dragenter,dragleave中无法获取数据的问题
  13. 【光学】基于matlab圆孔菲涅尔衍射【含Matlab源码 522期】
  14. 蓝桥杯新增web应用开发科目—送给想要参赛的小伙伴们一份备赛指南
  15. mysql preparedstatement_关于PreparedStatement你知道多少
  16. 搜索引擎网站登录入口|免费登录|百度登录|谷歌登录|网站收录入口
  17. TVB十大女星比美十大名花
  18. Linux Kernel Patched
  19. JavaScript名词解释
  20. Java数据结构--线性表

热门文章

  1. Solidity合约记录——(三)如何在合约中对操作进行权限控制
  2. shell的嵌入命令大全
  3. 内核添加dts后,device和device_driver的match匹配的变动:通过compatible属性进行匹配【转】...
  4. Linux中升级更新命令yum upgrade和yum update的区别
  5. Java HashMap涉及的数据结构及实现
  6. Exchange 2016 證書無效如何處理?
  7. 解读《电力发展“十三五”规划》
  8. 六边形块级元素的绘制
  9. java 表现层:jsp、freemarker、velocity
  10. IOS 开发之-- textfield和textview,return键的改变,点击return键