NAME
popen, pclose - pipe stream to or from a process SYNOPSIS
#include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream); DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and
invoking the shell. Since a pipe is by definition unidirectional, the
type argument may specify only reading or writing, not both; the
resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string contain-
ing a shell command line. This command is passed to /bin/sh using the
-c flag; interpretation, if any, is performed by the shell. RETURN VALUE
The popen() function returns NULL if the fork(2) or pipe(2) calls fail,
or if it cannot allocate memory. The pclose() function returns -1 if wait4(2) returns an error, or some
other error is detected.

popen.c,如下:

/*************************************************************************
> File Name: popen.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Fri 22 Aug 2014 11:07:26 AM CST
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(int argc, char *argv[])
{
char buf[1024];
FILE *fp; while(memset(buf,0,1024),fgets(buf,1024,stdin) != NULL)
{
fp = popen(argv[1],"w");
fputs(buf,fp);
pclose(fp);
} return 0;
}

被调用函数reverse.c,如下:

/*************************************************************************
> File Name: reverse.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Sat 23 Aug 2014 11:21:27 AM CST
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(int argc, char *argv[])
{
char word[256]; //从标准输入读取字符串
char buf[256][256],index = 0; //保存从标准输入读取的字符串
while(memset(word,0,256), scanf("%s",word) != EOF )
{
strcpy(buf[index++],word);
}
index--;
while(index >=0 )
{
printf("%s ",buf[index]);
index--;
}
printf("\n");
return 0;
}

运行程序:

[purple@localhost popen]$ gcc popen.c -o main
[purple@localhost popen]$ gcc reverse.c -o reverse
[purple@localhost popen]$ ./main ./reverse
how are you
you are how
baby u r beautiful
beautiful r u baby
[purple@localhost popen]$

按 ctrl+D 退出popen.c中的循环,从而退出程序。

popen.c,如下:

/*************************************************************************
> File Name: popen.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Sun 24 Aug 2014 08:53:14 AM CST
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h> int main(int argc, char *argv[])
{
FILE *fp; /* 标准管道描述符 */
char buf[1024]; /* 存放所要调用程序的参数 */
char cmd[1024]; /* 存放所要调用的程序的命令行 */
while(memset(buf,0,1024),fgets(buf,1024,stdin))
{
sprintf(cmd,"%s %s",argv[1],buf);
fp = popen(cmd,"r");
memset(buf,0,1024);
fgets(buf,1024,fp);
puts(buf);
pclose(fp);
}
return 0;
}

被调用函数reverse.c,如下:

/*************************************************************************
> File Name: reverse.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Sun 24 Aug 2014 09:03:37 AM CST
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(int argc, char *argv[])
{
int index;
for(index = argc - 1; index > 0 ; index--)
{
printf("%s ",argv[index]);
}
return 0;
}

运行程序:

[purple@localhost popen_write]$ ./main ./reverse
how are you
you are how
hello world
world hello

按 ctrl+D 退出程序。

小结

1. 在主调程序中,若popen以读模式打开,说明主调函数需要从管道获取程序运行结果,因而重定向了被调函数的标准输出。此时,popen时,主调函数需要将运行被调函数的完整参数写入放进命令行。被调函数运行后会将运行结果送入管道,主调程序自行从管道取出运行结果,打印在屏幕上。

2. 在主调程序中,若popen以写模式打开,说明主调函数需要将被调函数的运行所需的参数送人管道,因而重定向了被调函数的标注输入。此时,popen时,主调函数只需将被调函数的path写入命令行即可。被调函数会从管道中取走自己的参数,运行结果由被调函数打印在屏幕上。

标准管道(popen)的更多相关文章

  1. 详解linux进程间通信-管道 popen函数 dup2函数

    前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统.本章将说明进程之间相互通信的其他技术-I P C(InterProcess Communication ...

  2. Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()

    在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...

  3. Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)

    目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...

  4. python标准模块(下)

    Python 系统标准模块(shutil.logging.shelve.configparser.subprocess.xml.yaml.自定义模块) 目录: shutil logging模块 she ...

  5. Linux内核解析:进程间通信:管道

    管道的定义管道的用途管道的操作管道非法read与write内核实现解析管道通信原理及其亲戚通信解析父子进程通信解析亲缘关系的进程管道通信解析管道的注意事项及其性质管道有以下三条性质shell管道的实现 ...

  6. 《嵌入式linux应用程序开发标准教程》笔记——8.进程间通信

    , 8.1 概述 linux里使用较多的进程间通信方式: 管道,pipe和fifo,管道pipe没有实体文件,只能用于具有亲缘关系的进程间通信:有名管道 named pipe,也叫fifo,还允许无亲 ...

  7. Linux C 收藏

    某招聘要求:熟悉高性能分布式网络服务端设计开发,熟悉epoll.多线程.异步IO.事件驱动等服务端技术: <UNIX环境高级编程(第3版)>apue.h等源码文件的编译安装 <UNI ...

  8. PHP 面试知识点整理归纳

    基础篇了解大部分数组处理函数 array_chunk — 将一个数组分割成多个    array_column — 返回数组中指定的一列    array_combine — 创建一个数组,用一个数组 ...

  9. No.0

    算法类 1.快速排序算法 2.树的非递归后序排序算法 3.希尔排序 4.冒泡排序 5.链表和链表转向 6.其他   设计模式 1.单例模式 2.工厂模式 3.抽象工厂模式 4.面向对象设计,ooa,o ...

随机推荐

  1. hdu 1026 Ignatius and the Princess I

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...

  2. OpenStack: 安装准备

    >安装准备1. 安装MySQL# apt-get install python-mysqldb mysql-server将/etc/mysql/my.cnf修改bind-address为&quo ...

  3. linux下安装protobuf教程+示例(详细)

    (.pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory 看这个就应该知道是没有找到头文件 ...

  4. 十一、从头到尾彻底解析Hash 表算法

    在研究MonetDB时深入的学习了hash算法,看了作者的文章很有感触,所以转发,希望能够使更多人受益! 十一.从头到尾彻底解析Hash 表算法 作者:July.wuliming.pkuoliver  ...

  5. Go语言的类型转化

    Go语言要求不同的类型之间必须做显示的转换.转化分为类型转换和接口转化. 类型转换的思路是: X类型需要转换为Y类型,语法是T(x). 如果对于某些地方的优先级拿不准可以自己加()约束,变成(T)(X ...

  6. Winform之ListView

    ListView表示 Windows 列表视图控件,该控件显示可用四种不同视图之一显示的项集合.

  7. ASP.NET Web API 2 对 CORS 的支持

    CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览器施加的同域策略安全限制. 所谓同域策略, ...

  8. trap命令使用

    分享一个shell脚本技巧,大家写shell脚本的时候,一般而言仅仅保证功能可用,但程序的鲁棒性却不是太好,不够健壮,多数是脚本处理 一些中断信号导致,应对非预期的系统信号,其实系统自带的trap命令 ...

  9. SpringMVC处理脚本,SQL注入问题

    SpringMVC处理脚本,SQL注入问题(写的不好勿喷,互相学习) 使用 Filter 来过滤浏览器发出的请求,对每个URI参数请求过滤些关键字,替换成安全的字符.所有请求的 getParamete ...

  10. 4、android xml中drawableTop(drawableBoottom、drawableLeft、drawableRight)在java代码中的动态配置

    做安卓开发的朋友都知道,我们在xml中可以通过这样来对button设置其上部或者(下.左.右)的图片资源: 那么如果需要动态配置图片呢?我们不得不使用java代码来进行操作: Drawable dra ...