【C/C++】Linux下使用system()函数一定要谨慎
来源:互联网 发布:excel2013数据透视表 编辑:程序博客网 时间:2024/05/20 20:44
曾经的曾经,被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.
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
}
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
- 【C/C++】Linux下使用system()函数一定要谨慎
- C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 【C/C++】Linux下使用system()函数一定要谨慎
- 小数在计算机中的存储形式
- tar 使用
- Jsoup提取指定table中所有td的内容
- MVC,MVP架构
- 指针数组(指向指针的指针)空间的释放
- 【C/C++】Linux下使用system()函数一定要谨慎
- jquery对象和DOM对象的区别介绍
- 播放起来非常流畅而无卡顿
- CSDN博客之旅_记录技术学习的点滴_共筑技术王国
- C++String类关键操作简介
- Android系列教程之四:Android项目的目录结构
- 【暮色天】成功,需要耐心(6.12)
- 时间
- LeetCode First Missing Positive