[ARM入门]

对于linux下system()函数的深度理解

[复制链接]
577|0
手机看帖
扫描二维码
随时随地手机跟帖
qingxiao|  楼主 | 2017-11-27 15:53 | 显示全部楼层 |阅读模式
这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为是其他的代码影响到这个,或是内核驱动文件系统什么的异常导致,昨天有出现了这个问题,就随手百了一下度,问题出现了,很多人都说system()函数要慎用要少用要能不用则不用,system()函数不稳定? 
 下面对system函数做一个简单的介绍: 
头文件 
 #i nclude 
定义函数 
 int system(const char * string); 
函数说明 
 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命>令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信号则会被忽略。 返回值 =-1:出现错误 =0:调用成功但是没有出现子进程 >0:成功退出的子进程的id 如果system()在调用/bin/sh时失败则返回127,其他失败原因返回-1。嵌入式物联网智能硬件等系统学习企鹅意义气呜呜吧久零就易,若参数string为空指针(NULL),则返回非零值>。如果system()调用成功则最后会返回执行shell命令后的返回值,但是此返回值也有可能为 system()调用/bin/sh失败所返回的127,因此最好能再检查errno 来确认执行成功。 
附加说明 
 在编写具有SUID/SGID权限的程序时请勿使用system(),system()会继承环境变量,通过环境变量可能会造成系统安全的问题。 system函数已经被收录在标准c库中,可以直接调用,使用system()函数调用系统命令的基本使用方法如下: 
#include  
int main()

system("mkdir $HOME/.SmartPlatform/"); 
system("mkdir $HOME/.SmartPlatform/Files/"); 
system("cp mainnew.cpp $HOME/.SmartPlatform/Files/"); 
return 0; 

 下面我们来看看system函数的源码: 
#include  
#include  
#include  
#include  
int system(const char * cmdstring)

pid_t pid; 
int status; 
if(cmdstring == NULL)

return (1); 

if((pid = fork())<0)

status = -1; 

else if(pid = 0)
{
 execl("/bin/sh", "sh", "-c", cmdstring, (char *)0); -exit(127); //子进程正常执行则不会执行此语句 } 
else

while(waitpid(pid, &status, 0) < 0)
{
 if(errno != EINTER){ status = -1; break; 

}
 } 
return status; 

花了两天时间仔细研究了一下,在网上发现了一篇精品博客,介绍的很详细了,谢谢博主,直接转, 原文如下: 
http://my.oschina.net/renhc/blog/53580 
【C/C++】Linux下使用system()函数一定要谨慎 
曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入。只是简单的知道用这个函数执行一个系统命令,这远远不够,它的返回值、它所执行命令的返回值以及命令执行失败原因如何定位,这才是重点。当初因为这个函数风险较多,故抛弃不用,改用其他的方法。这里先不说我用了什么方法,这里必须要搞懂system()函数,因为还是有很多人用了system()函数,有时你不得不面对它。 
先来看一下system()函数的简单介绍: 
#include  
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()函数的实现: 
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成功,则返回子进程的返回状态 

 
仔细看完这个system()函数的简单实现,那么该函数的返回值就清晰了吧,那么什么时候system()函数返回0呢?只在command命令返回0时。 看一下该怎么监控system()函数执行状态 这里给我出的做法: 
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()函数的简单使用,还没

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

112

帖子

3

粉丝