标准管道(popen)
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)的更多相关文章
- 详解linux进程间通信-管道 popen函数 dup2函数
前言:进程之间交换信息的唯一方法是经由f o r k或e x e c传送打开文件,或通过文件系统.本章将说明进程之间相互通信的其他技术-I P C(InterProcess Communication ...
- Linux进程间通信(三):匿名管道 popen()、pclose()、pipe()、close()、dup()、dup2()
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一.什 ...
- Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...
- python标准模块(下)
Python 系统标准模块(shutil.logging.shelve.configparser.subprocess.xml.yaml.自定义模块) 目录: shutil logging模块 she ...
- Linux内核解析:进程间通信:管道
管道的定义管道的用途管道的操作管道非法read与write内核实现解析管道通信原理及其亲戚通信解析父子进程通信解析亲缘关系的进程管道通信解析管道的注意事项及其性质管道有以下三条性质shell管道的实现 ...
- 《嵌入式linux应用程序开发标准教程》笔记——8.进程间通信
, 8.1 概述 linux里使用较多的进程间通信方式: 管道,pipe和fifo,管道pipe没有实体文件,只能用于具有亲缘关系的进程间通信:有名管道 named pipe,也叫fifo,还允许无亲 ...
- Linux C 收藏
某招聘要求:熟悉高性能分布式网络服务端设计开发,熟悉epoll.多线程.异步IO.事件驱动等服务端技术: <UNIX环境高级编程(第3版)>apue.h等源码文件的编译安装 <UNI ...
- PHP 面试知识点整理归纳
基础篇了解大部分数组处理函数 array_chunk — 将一个数组分割成多个 array_column — 返回数组中指定的一列 array_combine — 创建一个数组,用一个数组 ...
- No.0
算法类 1.快速排序算法 2.树的非递归后序排序算法 3.希尔排序 4.冒泡排序 5.链表和链表转向 6.其他 设计模式 1.单例模式 2.工厂模式 3.抽象工厂模式 4.面向对象设计,ooa,o ...
随机推荐
- 5.Knockout.Js(自定义绑定)
前言 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定义很容易使用和重用的绑定.例如,你可以在form表单里自定义像grid ...
- 管理员必备的20个Linux系统监控工具
需要监控Linux服务器系统性能吗?尝试下面这些系统内置或附件的工具吧.大多数Linux发行版本都装备了大量的监控工具.这些工具提供了能用作取得相关信息和系统活动的量度指标.你能使用这些工具发现造成性 ...
- iOS学习之UI可视化编程-XIB
一.Interface Builder可视化编程 1.Interface Builder简介: GUI:图形用户界面(Graphical User Interface,简称GUI,又称图形用户接口)是 ...
- java数据结构和算法------顺序查找
package iYou.neugle.search; public class Sequence_search { public static int SequenceSearch(double[] ...
- MYSQL procedure
没怎么接触过mysql procedure,今天建个calendar表还磨磨唧唧的,记录一下: CREATE PROCEDURE `new_procedure` (start_date DATA,en ...
- 66.为什么有时候在ISE软件中,顶层文件不能置顶?
什么时候回出现顶层文件不能置顶呢?嘿嘿,肯定是工程中有错误啦. 如果你的顶层文件包含了include文件,这个时候就会出现这种情况了.但好像出现在刚新建工程的时候,因为当顶层文件不包括Include文 ...
- 通过find命令寻找文件并拷贝到一个指定目录方法详解
有这样的一个需求,需要将一部分符合条件的文件从一个目录拷贝到另一个目录中,可以通过使用find命令从源目录查找到符合条件的文件然后使用cp命令拷贝到目标目录 将通过find命令找到的文件拷贝到一个 ...
- java rest框架jersey数组单记录问题解决
JAVA数据接口采用jersey技术,可以返回xml,json等格式,可以根据客户端请求accept,如:Application/json,Application/xml 来得到不同的接口数据,非常好 ...
- fstream对象重复使用时注意clear()的调用
fstream对象重复使用时注意clear()的调用,否则会造成打开第二个文件失败.这是因为一个fstream对象对应磁盘上的一个文件,这种绑定关系在调用open()函数或者构造函数时指定,但有时我们 ...
- IO和NIO的区别
http://my.oschina.net/u/1010990/blog/192558 传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线 ...