【转】Linux下(C/C++)使用system()函数一定要谨慎
#include <stdlib.h>
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.
int system(const char * cmdstring)
{
pid_t pid;
int status; if(cmdstring == NULL)
{
return (); //如果cmdstring为空,返回非零值,一般为1
} if((pid = fork())<)
{
status = -; //fork失败,返回-1
}
else if(pid == )
{
execl("/bin/sh", "sh", "-c", cmdstring, (char *));
_exit(); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~
}
else //父进程
{
while(waitpid(pid, &status, ) < )
{
if(errno != EINTR)
{
status = -; //如果waitpid被信号中断,则返回-1
break;
}
}
} return status; //如果waitpid成功,则返回子进程的返回状态
}
仔细看完这个system()函数的简单实现,那么该函数的返回值就清晰了吧,那么什么时候system()函数返回0呢?只在command命令返回0时。
int status;
if(NULL == cmdstring) //如果cmdstring为空趁早闪退吧,尽管system()函数也能处理空指针
{
return XXX;
}
status = system(cmdstring);
if(status < )
{
printf("cmd: %s\t error: %s", cmdstring, strerror(errno)); // 这里务必要把errno信息输出或记入Log
return XXX;
} if(WIFEXITED(status))
{
printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); //取得cmdstring执行结果
}
else if(WIFSIGNALED(status))
{
printf("abnormal termination,signal number =%d\n", WTERMSIG(status)); //如果cmdstring被信号中断,取得信号值
}
else if(WIFSTOPPED(status))
{
printf("process stopped, signal number =%d\n", WSTOPSIG(status)); //如果cmdstring被信号暂停执行,取得信号值
}
到于取得子进程返回值的相关介绍可以参考另一篇文章: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下(C/C++)使用system()函数一定要谨慎的更多相关文章
- 【C/C++】Linux下使用system()函数一定要谨慎
[C/C++]Linux下使用system()函数一定要谨慎 http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为 ...
- linux下c程序调用reboot函数实现直接重启【转】
转自:http://www.blog.chinaunix.net/uid-20564848-id-73878.html linux下c程序调用reboot函数实现直接重启 当然你也可以直接调用syst ...
- Linux 下使用C语言 gets()函数报错
在Linux下,使用 gets(cmd) 函数报错:warning: the 'gets' function is dangerous and should not be used. 解决办法:采用 ...
- Linux下使用system()函数一定要谨慎
转载自:http://my.oschina.net/renhc/blog/53580 linux尽量避免使用system. 曾经的曾经,被system()函数折磨过,之所以这样,是因为对syste ...
- [转]【C/C++】Linux下使用system()函数一定要谨慎
曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值.它所执行命令的返回值以及命令执行失败原 ...
- (转)Linux下使用system()函数一定要谨慎
转:http://my.oschina.net/renhc/blog/53580 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.只是简单的知道用这个函 ...
- Linux下多进程编程之exec函数语法及使用实例
exec函数族 1)exec函数族说明 fork()函数用于创建一个子进程,该子进程几乎复制了父进程的全部内容,但是,这个新创建的进程如何执行呢?exec函数族就提供了一个在进程中启动另一个程序执行的 ...
- LINUX下C语言编程调用函数、链接头文件以及库文件
LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时, ...
- linux下c语言利用iconv函数实现utf-8转unicode
iconv是linux下的编码转换的工具,它提供命令行的使用和函数接口支持 man手册iconv命令用法如下: iconv -f encoding -t encoding inputfile 有如下选 ...
随机推荐
- RecordWriter接口解析
RecordWriter是将Map/Reduce结果(Key-Value)输出到文件系统中. /** * <code>RecordWriter</code> writes th ...
- bjfu1099 度度熊大战僵尸
这也是2011年百度之星的一道题. 这题我就是乱搞搞过的,打代码之前自己心里也没底,不知道能不能过的. 我的做法很简单,就是按时间顺序依次构造能杀死的僵尸血量,找到第k小的.构造的方法也很暴力:对t时 ...
- UIBezierPath 贝塞尔曲线
1. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(30, 30, 100, 100) corner ...
- cocos2d CCLayer 触摸相关
要让一个 CCLayer 能够接受触摸输入 需要进行如下设置: [selfsetTouchEnabled:YES]; cocos2d-x提供了两种触摸事件处理机制, 分别是CCStandardTo ...
- VS2010生成Qt程序图标修改方法
转自:http://blog.csdn.net/simeone18/article/details/7344547 1.准备ico文件,nuistcard.ico 2.在nuistcard工程目录,建 ...
- NGUI学习笔记-UISprite
所有的Sprite使用前,得先准备个图集,然后选择里面的图片进行填充 UISprite里面有几个属性做个笔记: Type: Smple:除了显示内容从图集里面获取外,其他都和Texture一样的绘制 ...
- oracle检查点checkpoint信息
1.关于checkpoint的概述 checkpoint是oracle在数据库一致性关闭.实例恢复和oracle基本操作中不可缺少的机制,包含以下相关的含义: A.检查点的位置(checkpoint ...
- Red Hat Enterprise Linux 6.4常用命令
常用命令: profile文件立即生效:source /etc/profile source /home/kituser/.bash_profile 修改文件夹子文件权限:chmod -R 777 / ...
- [JS代码]如何判断ipad或者iphone是否为横屏或者竖屏 - portrait或者landscape
//判断横屏或者竖屏 function orient() { //alert('gete'); if (window.orientation == 0 || window.orientation == ...
- CodeForces 567A Gerald is into Art
http://codeforces.com/problemset/problem/567/A A. Lineland Mail time limit per test 3 seconds memory ...