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. 九度oj 1554 区间问题

    原题链接:http://ac.jobdu.com/problem.php?pid=1554 由数列的前缀和:$\begin{align*}\Large{} S_n &=\Large{}\sum ...

  2. android开发系列之git常用命令

    最近因为跳槽到新公司,然后新公司里面的代码管理工具是gitLab,所以我想在这篇博客里面整理一下git常用的语法. GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托 ...

  3. 48.Warning: (vsim-3534) [FOFIR] - Failed to open file "sp_rom_8x256_sr.mif" for reading.

    当在仿真ROM IP核文件时,会出现这种警告,而这种警告的结果是ROM不能输出数据,原因是mif文件要放在modelsim工程文件目录下.类似的,有时候会报错,Failed to open file& ...

  4. Chr()和chrb()的含义(转)

    http://blog.csdn.net/cunxiyuan108/article/details/5989701 Chr(charcode) 必要的 charcode 参数是一个用来识别某字符的 L ...

  5. shell if判断的种类

    if [ $# != 1 ] ; then echo "USAGE: $0 TABNAME" echo " e.g.: $0 CDR_CALL_20040701" ...

  6. c++中头文件include规则浅析[译]

    英文原文地址 在开发大型的软件项目时,头文件需要得到恰当的管理,甚至在c中也会面临这种问题,当我们用c++开发时,头文件的管理会变得更复杂,更加耗费我们的时间去管理,下面我将讲一些包含规则来简化这个苦 ...

  7. 远航1617团队alpha版本分数分配与人员调动

    一.根据项目开始初期的分数分配要求及项目发布后大家的讨论,我们对组内成员的分数分配如下: 刘昊岩 20.5 周  萱 20.0 林谋武 19.0 杨  帆 18.5 高小洲 21.0 谢勤政 21.5 ...

  8. 揭开NodeJS的神秘面纱!

    一.NodeJS是什么? Node是一个服务器端JavaScript解释器.Node.js是一套用来编写高性能网络服务器的JavaScript包. 二.Node的目标是什么? Node 公开宣称的目标 ...

  9. 【LRU Cache】cpp

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  10. 基于AutoCAD的空间数据共享平台雏形

    好久没有更新博客了,今天先透露一个新的产品——AutoMap.我自己对于这个产品的定位是“基于AutoCAD的空间数据共享平台”.用一句话来概括AutoMap的功能:为用户提供一个在AutoCAD下访 ...