现在的位置: 首页 > 综合 > 正文

Linux中的popen-pclose

2013年03月21日 ⁄ 综合 ⁄ 共 3285字 ⁄ 字号 评论关闭

popen

Linux C

  进程I/O函数,与pclose函数一起使用。

表头文件

  #include <stdio.h>

函数定义

  FILE * popen ( const char * command , const char * type );

  int pclose ( FILE * stream );

函数说明

  popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数。pclose() 函数关闭标准 I/O
流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。

  type 参数只能是读或者写中的一种,得到的返回值(标准 I/O 流)也具有和 type 相应的只读或只写类型。如果 type 是 "r" 则文件指针连接到 command 的标准输出;如果 type 是 "w" 则文件指针连接到 command 的标准输入。

  command 参数是一个指向以 NULL 结束的 shell 命令字符串的指针。这行命令将被传到 bin/sh 并使用-c 标志,shell 将执行这个命令。

  popen 的返回值是个标准 I/O 流,必须由 pclose 来终止。前面提到这个流是单向的。所以向这个流写内容相当于写入该命令的标准输入;命令的标准输出和调用 popen 的进程相同。与之相反的,从流中读数据相当于读取命令的标准输出;命令的标准输入和调用 popen 的进程相同。

返回值

  如果调用 fork() 或 pipe() 失败,或者不能分配内存将返回NULL,否则返回标准 I/O 流。

返回错误

  popen 没有为内存分配失败设置 errno 值。

  如果调用 fork() 或 pipe() 时出现错误,errno 被设为相应的错误类型。

  如果 type 参数不合法,errno将返回EINVAL。

使用举例

  if((fp=popen("/usr/bin/uptime","r"))==NULL);

  {

  sprintf(buf,"error: %s\n", strerror(errno));

  ....//异常处理

  }

  else

  {

  ....

  pclose(fp);

  }

#define _LINE_LENGTH 300
  int get_path_total(const char *path, long long* total) {
  int err=-1;
  FILE *file;
  char line[_LINE_LENGTH];
  char *p;
  char tmp[100];
  char *token;
  sprintf(tmp, "df %s", path);
  file = popen(tmp, "r");
  if (file != NULL) {
  if (fgets(line, _LINE_LENGTH, file) != NULL) {
  if (fgets(line, _LINE_LENGTH, file) != NULL) {
  token = strtok(line, " ");
  if (token != NULL) {
  // printf("token=%s\n", token);
  }
  token = strtok(NULL, " ");
  if (token != NULL) {
  // printf("token=%s\n", token);
  *total=atoll(token)/1024;//k/1024
  err=0;
  }
  }
  }
  pclose(file);
  }
  return err;
  }


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等

   
作者:zieckey (http://zieckey.cublog.cn)
    All Rights Reserved!

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则用于使用结束后关闭这个指针。

下面看一个例子:

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


-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


抱歉!评论已关闭.