system()函数功能强大。以下小编就为大家介绍一下在C语言中system()函数的用法。需要的朋友可以过来参考下,希望对大家有所帮助
system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码:
代码如下:
#include
#include
#include
#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 来源gao.dai.ma.com搞@代*码网= -1;
break;
}
}
}
return status;
}
以上就是C语言中system()函数的用法总结的详细内容,更多请关注gaodaima搞代码网其它相关文章!