linux中 popen 和 system 区别和联系

一、system

为了更好的理解 system() 函数返回值,需要了解其执行过程,实际上 system() 函数执行了三步操作:

  • fork 一个子进程;
  • 在子进程中调用 exec 函数去执行 command;
  • 在父进程中调用 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() 和 popen() 简介

在 linux 中我们可以通过 system() 来执行一个 shell 命令,popen() 也是执行 shell 命令并且通过管道和 shell 命令进行通信。

system()、popen() 给我们处理了 fork、exec、waitpid 等一系列的处理流程,让我们只需要关注最后的返回结果(函数的返回值)即可。

三、system()、popen() 源码

1、system() 源码

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;
}

2、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);
}

四、执行流程

从上面的源码可以看到 system 和 popen 都是执行了类似的运行流程,大致是

fork -> execl -> return

但是我们看到 system 在执行期间调用进程会一直等待 shell 命令执行完成(waitpid 等待子进程结束)才返回,但是 popen 无须等待 shell 命令执行完成就返回了。我们可以理解 system 为串行执行,在执行期间调用进程放弃了”控制权”,popen 为并行执行

popen 中的子进程没人给它”收尸”了啊?是的,如果你没有在调用 popen 后调用 pclose 那么这个子进程就可能变成”僵尸”。

上面我们没有给出 pclose 的源码,其实我们根据 system 的源码差不多可以猜测出 pclose 的源码就是 system 中第 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 就是执行 shell 命令最后返回是否执行成功,popen 执行命令并且通过管道和 shell 命令进行通信。

七、总结

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

转载:linux中popen函数,system函数与popen函数_weixin_34732336的博客-CSDN博客

(SAW:Game Over!)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linuxpopen函数用于执行shell命令并通过管道与shell命令进行通信。popen函数的原型如下: ```c FILE *popen(const char *command, const char *mode); ``` 其,command参数是要执行的shell命令,mode参数可以是"r"或"w",分别表示读取命令的输出或向命令输入数据。 popen函数会创建一个管道,并调用fork函数创建一个子进程来执行shell命令。子进程通过dup2函数将管道的一端重定向到标准输入或标准输出,再通过exec函数执行shell命令。父进程则可以通过返回的FILE指针与子进程进行通信,读取子进程的输出或向子进程输入数据。 popen函数返回一个FILE指针,可以通过该指针进行文件操作,如读取命令的输出。使用完毕后,需要调用pclose函数关闭子进程,并释放相应的资源。 总结来说,popen函数在Linux用于执行shell命令并与其进行通信,方便地获取命令的输出或向命令输入数据。123 #### 引用[.reference_title] - *1* [Linux系统上的popen()库函数](https://blog.csdn.net/weixin_30520015/article/details/95963445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *2* *3* [linux popen 和 system 区别联系](https://blog.csdn.net/itworld123/article/details/125383834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值