linux 之 popen函数

简介: <p><span style="font-family:courier new,courier"><span>描述 </span></span></p> <p><span style="font-family:courier new,courier"><span><strong>popen()</strong> 函数 用 创建管道 的 方式启动一个 进程, 并调用 shell. 因为 管

描述

popen() 函数 用 创建管道 的 方式启动一个 进程, 并调用 shell. 因为 管道是被定义成单向的, 所以 type 参数 只能定义成 只读或者 只写, 不能是 两者同时, 结果流也相应的 是只读 或者 只写.

command 参数 是 一个 字符串指针, 指向的是一个 以 null 结束符 结尾的字符串, 这个字符串包含 一个 shell 命令. 这个命令 被送到 /bin/sh 以-c 参数 执行, 即由 shell 来执行. type 参数 也是 一个 指向 以 null 结束符结尾的 字符串的指针, 这个字符串 必须是 'r' 或者 'w’ 来指明 是 读还是写.

popen() 函数 的 返回值 是一个普通的 标准I/O流, 它只能用pclose() 函数 来关闭, 而不是 fclose(). 函数. 向这个流 的 写入被转化为 对 command 命令的标准输入; 而 command 命令的 标准输出 则是和 调用popen(), 函数 的 进程 相同,除非 这个被command命令 自己改变. 相反的, 读取 一个 “被popen了的” 流, 就相当于 读取 command 命令的标准输出, 而 command 的标准输入 则是和 调用popen, 函数的进程 相同.

注意, popen 函数的 输出流默认是被全缓冲的.

pclose 函数 等待 相关的进程结束并返回 一个 command 命令的 退出状态, 就像wait4 函数 一样

本文介绍了popen函数的使用方法和行为机理,并给出实际的例子来辅助说明了popen函数的使用方法。
文件中还介绍了几个文件操作的函数,如fopen,fread,fwrite等
  

popen使用FIFO管道执行外部程序。

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

popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。

下面看一个例子:

/*******************************************************************************************
** Name:popen.c
**      This program is used to show the usage of popen() .
** Author:zieckey,(zieckey@yahoo.com.cn)
** Date:2007/9/30 11:47
** All rights reserved!
*******************************************************************************************/
#include <sys/types.h>  
#include <unistd.h>  
#include <stdlib.h>  
#include <stdio.h>  
#include <string.h>

int main( void )  
{  
    FILE   *stream;  
    FILE    *wstream;
    char   buf[1024];
    
    memset( buf, '/0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
    stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
    wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件

    fread( buf, sizeof(char), sizeof(buf),  stream);  //将刚刚FILE* stream的数据流读取到buf中
    fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE    *wstream对应的流中,也是写到文件中
   
    pclose( stream ); 
    fclose( wstream );
   
    return 0;
}  

[root@localhost src]# gcc popen.c
[root@localhost src]# ./a.out  
[root@localhost src]# cat test_popen.txt
总计 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root  542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root  480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root  162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root  443 09-30 00:55 system.c
-rwxr-xr-x 1 root root    0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt

 

 

//

rec_fp = popen(t_cmd, "w");

//发送q 退出进程

 fwrite(quit_cmd, sizeof(char), strlen(quit_cmd), rec_fp);
 pclose(rec_fp);

//

相关文章
|
25天前
|
Linux
关于Linux目录访问函数总结
关于Linux目录访问函数总结
13 1
|
1月前
|
算法 Linux C++
【Linux系统编程】深入解析Linux中read函数的错误场景
【Linux系统编程】深入解析Linux中read函数的错误场景
205 0
|
1月前
|
存储 监控 Linux
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
53 0
|
1月前
|
存储 算法 Linux
【Linux系统编程】深入理解Linux目录扫描函数:scandir目录函数(按条件扫描目录
【Linux系统编程】深入理解Linux目录扫描函数:scandir目录函数(按条件扫描目录
39 0
|
3天前
|
算法 Linux Shell
【linux进程(二)】如何创建子进程?--fork函数深度剖析
【linux进程(二)】如何创建子进程?--fork函数深度剖析
|
19天前
|
Linux 开发者
Linux文件编程(open read write close函数)
通过这些函数,开发者可以在Linux环境下进行文件的读取、写入和管理。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
85 4
|
1月前
|
存储 Unix Linux
深入理解 Linux 系统下的关键网络接口和函数,gethostent,getaddrinfo,getnameinfo
深入理解 Linux 系统下的关键网络接口和函数,gethostent,getaddrinfo,getnameinfo
15 0
|
1月前
|
Linux C语言
Linux 下pause函数是如何实现的?
Linux 下pause函数是如何实现的?
14 0
|
1月前
|
网络协议 Linux C++
Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)
Linux C/C++ 网络编程中地址格式转换(inet_pton和inet_ntop函数)
26 0
|
1月前
|
存储 Linux
linux waitpid函数详解
linux waitpid函数详解
23 0