【C/C++】Linux下使用system()函数一定要谨慎
来源:互联网 发布:c语言 矩形 rect 编辑:程序博客网 时间:2024/06/05 15:21
#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 (1); //如果cmdstring为空,返回非零值,一般为1} if((pid = fork())<0){ status = -1; //fork失败,返回-1}else if(pid == 0){ execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); _exit(127); // exec执行失败返回127,注意exec只在失败时才返回现在的进程,成功的话现在的进程就不存在啦~~}else //父进程{ while(waitpid(pid, &status, 0) < 0) { if(errno != EINTR) { status = -1; //如果waitpid被信号中断,则返回-1 break; } }} return status; //如果waitpid成功,则返回子进程的返回状态}
int status;if(NULL == cmdstring) //如果cmdstring为空趁早闪退吧,尽管system()函数也能处理空指针{ return XXX;}status = system(cmdstring);if(status < 0){ 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
原文地址http://my.oschina.net/renhc/blog/53580
- 【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()函数一定要谨慎
- 最大流(一)
- 交叉编译的简单心得
- 如何在一个没有root权限的Android设备上创建一个SOCKS代理(英文转载)
- 写给刚工作的同学们的一些话
- JSON.parse( ) 和JSON.stringify( ) 的区别
- 【C/C++】Linux下使用system()函数一定要谨慎
- LP---精益生产(Lean Production)
- 自己在拖xib中遇到的一些小问题
- Barcode Reader SDK使用教程:网络摄像机读取条码
- LeetCode Integer to Roman
- MP4文件格式详解——元数据moov(一)mvhd box(转)
- URAL 1534 Football in Gondor
- KNN的Python实现(需添加库numpy和operator)
- 网络虚拟化技术(一): Linux网络虚拟化