标准管道(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 ...
随机推荐
- 关于Filezilla是否支持sftp
我们知道filezilla是一个开源的ftp的解决方案,它提供了客户端和服务器端,支持的fpt, sftp, ftps,这是你可以从wiki中看到的关于filezilla的介绍,但是这里需要澄清一点就 ...
- Go中的指针与函数接收器
Go中使用*号表示指针,但是没有指针算数,不能对其进行加减.同时内存管理都由Go来负责,不需要拖动释放内存. Go中的函数接收者,可以为值类型,也可以是引用类型. 看代码: package main ...
- UITabelView 高级(自定义Cell)
自定义一个Cell 当我们要显示复杂数据的时候,例如要做一个扣扣聊天界面,或是新闻列表,系统的行已经不能满足我们的要求,这个时候我们可以通过自定义这个行,让他显示更多复杂结构的样式. 自定义cell就 ...
- js原型和构造函数混合模式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [转]tftp在put上传的时候显示File not found的解决办法
[转]tftp在put上传的时候显示File not found的解决办法 http://blog.163.com/pengcz%40126/blog/static/35908607201182433 ...
- FPGA中如何实现除法?
摘自:<xilinx FPGA 开发实用教程> 1)被除数重复的减去除数,直到检测到余数小于除数为止,优点:对于除数与被除数相差较小的情况下合适 2)通过如下图片方式实现+状态机.优点:挺 ...
- 4.FPGA芯片管脚解释
用户I/O:不用解释了. 配置管脚: MSEL[1:0] 用于选择配置模式,比如AS.PS等. DATA0 FPGA串行数据输入,连接到配置器件的串行数据输出管脚. DCLK FPGA串行时钟输出 ...
- 内部类&匿名内部类
内部类:如果A类需要直接访问B类中的成员,而B类又需要建立A类的对象.这时,为了方便设计和访问,直接将A类定义在B类中.就可以了.A类就称为内部类.内部类可以直接访问外部类中的成员.而外部类想要访问内 ...
- 0-N背包为题(动态规划算法)
/****************0-N背包问题****************** * 有n个物体装入容量为c的背包,每一个物体有一个体积 * 和一个价值,所装入的物体体积之和不大于背包体积, * ...
- Zclip复制页面内容到剪贴板兼容各浏览器
Zclip:复制页面内容到剪贴板兼容各浏览器 WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮 ...