system、wait,waitpid,fork使用时注意

来源:互联网 发布:美工常用72种字体打包 编辑:程序博客网 时间:2024/06/05 19:56

system,waitpid使用时的坑

system这个函数可以用来执行shell命令,这也是我们平常使用比较多,或者说比较喜欢使用的地方,但使用时也有很多坑等着你。

下面这段代码大家应该不难理解,作用是忽略掉SIGCHLD这个信号量。

/* Ignore SIGCHLD to avoid zombie process */    if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {        return -1;    } else {        return 0;    }

你把SIGCHLD忽略掉,你会很理直气壮的说,防止wait/waitpid收尸时出现僵死进程,你说得很对,很合理,僵尸进程是需要有人来处理,否则也是一件很糟糕的事。

可是,在程序的另一个模块里,可能不是你这组的人开发的,是另一组负责开发的,他们想用system函数去执行一些shell命令来为他们处理一些业务逻辑之类的事,比如:
int status = system(“gzip -c /var/opt/abc.txt > /var/opt/abc.zip”);

你会发现status一直不等于0,为什么呢?在回答为什么前,先熟悉下system函数。

  • system函数
    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源码

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时。

将坑填平

前面status不为0,我们可以打印下系统的出错信息strerror(errno),发现打印了“ No child processes”,这是什么errno呢,不难发现它对应的是ECHILD,那个ECHILD是哪里来的呢,结合前面system函数源码的执行过程,我们知道system()函数执行过程为:fork()->exec()->waitpid()。我们可以猜想,这个ECHILD很有可能是waitpid送出来的,我们可以看看waitpid的帮助文档(man waitpid):

ECHILD
(for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can happen for one’s own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)
果然有料,如果SIGCHLD信号行为被设置为SIG_IGN时,waitpid()函数有可能因为找不到子进程而报ECHILD错误,结合前面,另一组的人的确把SIG_IGN忽略了。似乎我们找到了问题的解决方案:在调用system()函数前重新设置SIGCHLD信号为缺省值,即signal(SIGCHLD, SIG_DFL)。

解决的方法如下:
old_handler = signal(SIGCHLD, SIG_DFL);
ret = system(cmd_line);
signal(SIGCHLD, old_handler);

小结

说到这里,不是太推荐上面system的用法。
system()函数用起来很容易出错,返回值太多,而且返回值很容易跟command的返回值混淆。这里推荐使用popen()函数替代,关于popen()函数的简单使用也可以通过上面的链接查看。

popen()函数较于system()函数的优势在于使用简单,popen()函数只返回两个值:
成功返回子进程的status,使用WIFEXITED相关宏就可以取得command的返回结果;
失败返回-1,我们可以使用perro()函数或strerror()函数得到有用的错误信息。

这篇文章只涉及了system()函数的简单使用,还没有谈及SIGCHLD、SIGINT和SIGQUIT对system()函数的影响,事实上,之所以今天写这篇文章,是因为项目中因有人使用了system()函数而造成了很严重的事故。现像是system()函数执行时会产生一个错误:“No child processes”。

原创粉丝点击