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. Windows server 2012清除并重建SID

    首先介绍下什么是SID SID也就是安全标识符(Security Identifiers),是标识用户.组和计算机帐户的唯一的号码.在第一次创建该帐户时,将给网络上的每一个帐户发布一个唯一的 SID. ...

  2. SQL-Server数据库学习笔记-表

    1. 表及其属性 表(Table):也称实体,是存储同类型数据的集合. 列(Field):也称字段.域或属性,它构成表的架构,具体表示为一条信息中的一个属性. 行(Row):也称元组(Tuple),存 ...

  3. 失败的数据库迁移UDB

    公司采用的是ucloud的云主机,数据库也是架设在云主机上.由于数据越来越多数据查询数据越来越慢,所以我决定往 UDB上迁移.当时考虑的理由如下: (1)云主机底层架设在虚拟机上IO性能有折损,而UD ...

  4. spring IOC源码分析(1)

    1.何谓Spring IOC 何谓Spring IOC?书上谓之“依赖注入”,那何谓“依赖注入”? 作为一个Java程序猿,应该遇到过这样的问题,当你在代码中需要使用某个类提供的功能时,你首先需要ne ...

  5. WPF——数据绑定(一)什么是数据绑定

    注意:本人初学WPF,文中可能有表达或者技术性问题,欢迎指正!谢谢! 一:什么是数据绑定? “Windows Presentation Foundation (WPF) 数据绑定为应用程序提供了一种简 ...

  6. The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

    如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...

  7. 菜鸟搭建Android环境~~~~绝对靠谱

    因为要测试移动设备.搭建了一下Android环境 这是菜鸟级别的安装 因为sdk版本,eclipse版本,adt版本各自有版本要求,所以我选择都去官网下载新版本,这样总不会出现版本兼容性问题了吧~~ ...

  8. 利用Java实现表达式二叉树

    (*^-^*) 什么是二叉树,这里不再介绍,可以自行百度:二叉树.在这里利用java实现“表达式二叉树”. 表达式二叉树的定义 第一步先要搞懂表达式二叉树是个什么东东?举个栗子,表达式:(a+b×(c ...

  9. [错误代码:0x80070002]IIS7及以上伪静态报错404

    故障现象:DTCMS开启伪静态功能,VS2010预览正常,发布到IIS后报错404.0错误 (WIN7,WIN8,SERVER2008).模块IISWebCore通知MapRequestHandler ...

  10. Gitlab 7.12 发布 SAML支持及其他更多功能

    官方文章:https://about.gitlab.com/2015/06/22/gitlab-7-12-released/ CSDN翻译文章:http://geek.csdn.net/news/de ...