Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3624607
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2017-11-03 11:13:34

为了更好的理解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.



Linux的system()和popen()差异


1. system()和popen()简介

在linux中我们可以通过system()来执行一个shell命令,popen()也是执行shell命令并且通过管道和shell命令进行通信。
system()、popen()给我们处理了fork、exec、waitpid等一系列的处理流程,让我们只需要关注最后的返回结果(函数的返回值)即可。

2. system()、popen()源码

首先我们来看一下这两个函数在源码(伪代码)上面的差异。

int system(const char *command)
{ struct sigaction sa_ignore, sa_intr, sa_quit;
    sigset_t block_mask, orig_mask;
    pid_t pid;

    sigemptyset(&block_mask);
    sigaddset(&block_mask, SIGCHLD);
    sigprocmask(SIG_BLOCK, &block_mask, &orig_mask); //1. block SIGCHLD sa_ignore.sa_handler = SIG_IGN;
    sa_ignore.sa_flags = 0;
    sigemptyset(&sa_ignore.sa_mask);
    sigaction(SIGINT, &sa_ignore, &sa_intr); //2. ignore SIGINT signal sigaction(SIGQUIT, &sa_ignore, &sa_quit); //3. ignore SIGQUIT signal switch((pid = fork()))
    { case -1: return -1; case 0:
            sigaction(SIGINT, &sa_intr, NULL); 
            sigaction(SIGQUIT, &sa_quit, NULL); 
            sigprocmask(SIG_SETMASK, &orig_mask, NULL);
            execl("/bin/sh", "sh", "-c", command, (char *) 0);
            exit(127); default: while(waitpid(pid, NULL, 0) == -1) //4. wait child process exit { if(errno != EINTR)
                { break;
                }
            }
    }
} return 0;

上面是一个不算完整的system函数源码,后面需要我们关注和popen差异的部分已经用数字标示出来了。

static pid_t    *childpid = NULL; /* ptr to array allocated at run-time */ static int      maxfd; /* from our open_max(), {Prog openmax} */ #define SHELL   "/bin/sh"  FILE *  
popen(const char *cmdstring, const char *type)  
{  
    int     i, pfd[2];  
    pid_t   pid;  
    FILE    *fp; /* only allow "r" or "w" */ if ((type[0] != 'r' && type[0] != 'w') || type[1] != 0) {  
        errno = EINVAL; /* required by POSIX.2 */ return(NULL);  
    } if (childpid == NULL) { /* first time through */ /* allocate zeroed out array for child pids */ maxfd = open_max(); if ( (childpid = calloc(maxfd, sizeof(pid_t))) == NULL) return(NULL);  
    } if (pipe(pfd) < 0) return(NULL); /* errno set by pipe() */ if ( (pid = fork()) < 0) return(NULL); /* errno set by fork() */ else if (pid == 0) { /* child */ if (*type == 'r') { close(pfd[0]); if (pfd[1] != STDOUT_FILENO) {  
                dup2(pfd[1], STDOUT_FILENO); close(pfd[1]);  
            }  
        } else { close(pfd[1]); if (pfd[0] != STDIN_FILENO) {  
                dup2(pfd[0], STDIN_FILENO); close(pfd[0]);  
            }  
        } /* close all descriptors in childpid[] */ for (i = 0; i < maxfd; i++) if (childpid[ i ] > 0) close(i);  

        execl(SHELL, "sh", "-c", cmdstring, (char *) 0); _exit(127);  
    } /* parent */ if (*type == 'r') { close(pfd[1]); if ( (fp = fdopen(pfd[0], type)) == NULL) return(NULL);  
    } else { close(pfd[0]); if ( (fp = fdopen(pfd[1], type)) == NULL) return(NULL);  
    }  
    childpid[fileno(fp)] = pid; /* remember child pid for this fd */ return(fp);  
} 

上面是popen的源码。

3. 执行流程

从上面的源码可以看到system和popen都是执行了类似的运行流程,大致是fork->execl->return。但是我们看到system在执行期间调用进程会一直等待shell命令执行完成(waitpid等待子进程结束)才返回,但是popen无须等待shell命令执行完成就返回了。我们可以理解system为串行执行,在执行期间调用进程放弃了”控制权”,popen为并行执行。
popen中的子进程没人给它”收尸”了啊?是的,如果你没有在调用popen后调用pclose那么这个子进程就可能变成”僵尸”。
上面我们没有给出pclose的源码,其实我们根据system的源码差不多可以猜测出pclose的源码就是system中第4部分的内容。

4. 信号处理

我们看到system中对SIGCHLD、SIGINT、SIGQUIT都做了处理,但是在popen中没有对信号做任何的处理。
SIGCHLD是子进程退出的时候发给父进程的一个信号,system()中为什么要屏蔽SIGCHLD信号可以参考:system函数的总结waitpid(or wait)和SIGCHILD的关系,总结一句就是为了system()调用能够及时的退出并且能够正确的获取子进程的退出状态(成功回收子进程)。
popen没有屏蔽SIGCHLD,主要的原因就是popen是”并行”的。如果我们在调用popen的时候屏蔽了SIGCHLD,那么如果在调用popen和pclose之间调用进程又创建了其它的子进程并且调用进程注册了SIGCHLD信号处理句柄来处理子进程的回收工作(waitpid)那么这个回收工作会一直阻塞到pclose调用。这也意味着如果调用进程在pclose之前执行了一个wait()操作的话就可能获取到popen创建的子进程的状态,这样在调用pclose的时候就会回收(waitpid)子进程失败,返回-1,同时设置errno为ECHLD,标示pclose无法获取子进程状态。
system()中屏蔽SIGINT、SIGQUIT的原因可以继续参考上面提到的system函数的总结,popen()函数中没有屏蔽SIGINT、SIGQUIT的原因也还是因为popen是”并行的”,不能影响其它”并行”进程。

4. 功能

从上面的章节我们基本已经把这两个函数剖析的差不多了,这两个的功能上面的差异也比较明显了,system就是执行shell命令最后返回是否执行成功,popen执行命令并且通过管道和shell命令进行通信。

NOTE

在特权(setuid、setgid)进程中千万注意不要使用system和popen。

阅读(2551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~