Linux函数popen/pclose学习

时间:2022-12-21 05:54:25

本文针对项目中用到的几个函数进行详细分析,并尽可能的添加示例进行验证学习。比如fcntl/ioctl函数、system/exec函数、popen/pclose函数、mmap函数等。 重点参考了《UNP》和《Linux程序设计》第四版。

一、概念

#include <stdio.h>
FILE * popen ( const char * command , const char * type );
int pclose ( FILE * stream );

popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。

  • command 参数是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用-c 标志,shell 将执行这个命令。
  • type 参数只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 “r” 则文件指针连接到 command 的标准输出;如果 type 是 “w” 则文件指针连接到 command 的标准输入。
  • popen 的返回值是个标准 I/O 流,必须由 pclose 来终止,否则会产生僵尸子进程。pclose调用只在popen启动的进程结束后才返回。
  • 当使用popen()时,不要屏蔽SIGCHLD信号,popen()使用fork()创建了子进程来运行所给的命令,需要通过此信号判断子进程是否已经退出。

二、读写示例

1. 读取外部程序的输出

我们在程序中用popen访问uname命令给出的信息。命令uname -a的作用是打印系统信息,包括计算机型号、操作系统名称、版本和发行号、以及计算机网络名。
完成程序的初始化工作后,打开一个连接到uname命令的管道,把管道设置为可读方式并让read_fp指向该命令的输出。最后,关闭read_fp指向的管道。

#include <sys/types.h> 
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
FILE *read_fp;
char buf[1024];
int chars_read;

memset(buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
read_fp = popen( "uname -a", "r" ); //将“uname -a”命令的输出通过管道读取(“r”参数)到FILE* stream
if(read_fp != NULL)
{
chars_read = fread( buf, sizeof(char), sizeof(buf), read_fp);//将数据流读取到buf中
if(chars_read >0)
printf("my output:\n%s\n",buf);

pclose( read_fp );
}

return 0;
}

Linux函数popen/pclose学习

2. 将输出送往外部程序

这里将数据写入管道,使用的额是od(八进制输出)的命令。

#include <sys/types.h> 
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
FILE *write_fp;
char buf[1024];
int chars_write;

memset(buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
sprintf(buf,"hello world...\n");
write_fp = popen( "od -c", "w" ); //通过管道(“w”参数)写入到FILE* stream
if(write_fp != NULL)
{
fwrite( buf, sizeof(char), sizeof(buf), write_fp); //将FILE* write_fp的数据流写入到buf中
pclose( write_fp );
}

return 0;
}

程序使用带有参数“w”的popen启动od -c命令,这样就可以向该命令发送数据了。然后它给od -c命令发送一个字符串,该命令接收并处理它,最后把处理结果打印到自己的标准输出上。
在命令行上,我们可以使用下面命令得到同样的输出结果:
echo “hello world…\n” | od -c

Linux函数popen/pclose学习

三、返回值分析

和system调用类似,也需要考虑调用返回值。popen执行一个 shell 以运行命令来开启一个进程。pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。

这里重点参考一位博主的文章,首先要明确几点:

  • pclose 失败返回 -1, 成功则返回 exit status, 同 system 类似,需要用 WIFEXITED, WEXITSTATUS 等获取命令返回值。
  • 和 system 一样,SIGCHLD 依然会影响 popen,如果设置了SIGCHLD 则获取不到子进程的状态。
  • 管道只能处理标准输出,不能处理标准错误输出。
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

int main(int argc, char* argv[])
{
char cmd[1024];
char line[1024];
FILE* pipe;
int rv;

if (argc != 2)
{
printf("Usage: %s <path>\n", argv[0]);
return -1;
}

// pclose fail: No child processes
//signal(SIGCHLD, SIG_IGN);

snprintf(cmd, sizeof(cmd), "ls -l %s 2>/dev/null", argv[1]);

pipe = popen(cmd, "r");
if(NULL == pipe)
{
printf("popen() failed: %s\n", cmd);
return -1;
}

while(fgets(line, sizeof(line),pipe) != NULL)
{
printf("%s", line);
}

rv = pclose(pipe);

if (-1 == rv)
{
printf("pclose() failed: %s\n", strerror(errno));
return -1;
}

if (WIFEXITED(rv))
{
printf("subprocess exited, exit code: %d\n", WEXITSTATUS(rv));
if (0 == WEXITSTATUS(rv))
{
// if command returning 0 means succeed
printf("command succeed\n");
}
else
{
if(127 == WEXITSTATUS(rv))
{
printf("command not found\n");
return WEXITSTATUS(rv);
}
else
{
printf("command failed: %s\n", strerror(WEXITSTATUS(rv)));
return WEXITSTATUS(rv);
}
}
}
else
{
printf("subprocess exit failed\n");
return -1;
}

return 0;
}

四、总结

总的来说,请求popen调用运行一个程序时,它首先启动shell,即系统的shell命令,然后将command字符串作为一个参数传递给它。这样就有了优缺点:
优点是:由于所有类Unix系统中参数扩展都是由shell完成的,所有它运行我们通过popen完成非常复杂的shell命令。而其他一些创建进程的函数(如execl)调用起来就复杂的多,因为调用进程必须自己完成shell扩展。
缺点是:针对每个popen调用,不仅要启动一个被请求的程序,还要启动一个shell,即每个popen调用将启动两个进程。从节省系统资源的角度来看,popen函数的调用成本略高,并且对目标命令的调用比正常方式慢一些(通过pipe改进)。

和system相比,system就是执行shell命令最后返回是否执行成功,popen执行命令并且通过管道和shell命令进行通信。