您的位置:首页 > 运维架构 > Linux

Linux的system()和popen()差异

2016-05-16 10:17 597 查看

Linux的system()和popen()差异

1. system()和popen()简介

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

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

01
#include
<stdio.h>
02
/*
03
函数功能:popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh
-c来执行参数command的指令。
04
参数type可使用“r”代表读取,“w”代表写入。
05
依照此type值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。
06
随后进程便可利用此文件指针来读取子进程的输出设备或是写入到子进程的标准输入设备中
07
返回值:若成功则返回文件指针,否则返回NULL,错误原因存于errno中
08
*/
09
FILE
*
popen(
const
char
*
command,
const
char
*
type);
10
11
/*
12
函数功能:pclose()用来关闭由popen所建立的管道及文件指针。参数stream为先前由popen()所返回的文件指针
13
返回值:若成功返回shell的终止状态(也即子进程的终止状态),若出错返回-1,错误原因存于errno中
14
*/
15
int
pclose(
FILE
*
stream);

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;


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是”并行的”,不能影响其它”并行”进程。

5. 功能

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

popen() 创建一个管道,通过fork或者invoke一个子进程,然后执行command。返回值在标准IO流中,由于是在管道之中,因此数据流是单向的,command只能产生stdout或者读取stdin,因此type只有两个值:‘w’或‘r’。r表示command从管道中读取数据流,而w表示command的stdout输出到管道中。command无法同时读取和输出。popen返回该FIFO数据流的指针。

6. 实例

01
/*取得当前目录下的文件个数*/
02
#include
<stdio.h>
03
#include
<stdlib.h>
04
#include
<errno.h>
05
#include
<sys/wait.h>
06
07
#define
MAXLINE 1024
08
09
int
main()
10
{
11
char
result_buf[MAXLINE],
command[MAXLINE];
12
int
rc
= 0;
//
用于接收命令返回值
13
FILE
*fp;
14
15
/*将要执行的命令写入buf*/
16
snprintf(command,
sizeof
(command),
"ls
./ | wc -l"
);
17
18
/*执行预先设定的命令,并读出该命令的标准输出*/
19
fp
= popen(command,
"r"
);
20
if
(NULL
== fp)
21
{
22
perror
(
"popen执行失败!"
);
23
exit
(1);
24
}
25
while
(
fgets
(result_buf,
sizeof
(result_buf),
fp) != NULL)
26
{
27
/*为了下面输出好看些,把命令返回的换行符去掉*/
28
if
(
'\n'
==
result_buf[
strlen
(result_buf)-1])
29
{
30
result_buf[
strlen
(result_buf)-1]
=
'\0'
;
31
}
32
printf
(
"命令【%s】
输出【%s】\r\n"
,
command, result_buf);
33
}
34
35
/*等待命令执行完毕并关闭管道及文件指针*/
36
rc
= pclose(fp);
37
if
(-1
== rc)
38
{
39
perror
(
"关闭文件指针失败"
);
40
exit
(1);
41
}
42
else
43
{
44
printf
(
"命令【%s】子进程结束状态【%d】命令返回值【%d】\r\n"
,
command, rc, WEXITSTATUS(rc));
45
}
46
47
return
0;
48
}
管道读:
先创建一个文件test,然后再test文件内写入“Read pipe successfully !”

  #include “stdio.h”

  #include “stdlib.h”

  int main()

  {

  FILE *fp;

  char buf[200] = {0};

  if((fp = popen(“cat test”, “r”)) == NULL) {

  perror(“Fail to popen\n”);

  exit(1);

  }

  while(fgets(buf, 200, fp) != NULL) {

  printf(“%s”, buf);

  }

  pclose(fp);

  return 0;

  }

  打印输出: Read pipe successfully !

管道写:

  #include “stdio.h”

  #include “stdlib.h”

  int main()

  {

  FILE *fp;

  char buf[200] = {0};

  if((fp = popen(“cat > test1″, “w”)) == NULL) {

  perror(“Fail to popen\n”);

  exit(1);

  }

  fwrite(“Read pipe successfully !”, 1, sizeof(“Read pipe successfully !”), fp);

  pclose(fp);

  return 0;

  }

  执行完毕后,当前目录下多了一个test1文件,打开,里面内容为Read pipe successfully !


NOTE

popen可以控制程序的输入或者输出,而system的功能明显要弱一点,比如无法将ls的结果用到程序中。如果不需要使用到程序的I/O数据流,那么system是最方便的。

而且system函数是C89和C99中标准定义的,可以跨平台使用。而popen是Posix 标准函数,可能在某些平台无法使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: