linux 进程间管道通讯 popen以及pclose
来源:互联网 发布:mac上的大型网游 编辑:程序博客网 时间:2024/06/05 23:43
popen和pclose函数
================================================
一个常用的操作就是给一个进程创建管道,通过管道读取它的标准输出以及向它的标准输入发送数据,标准输入输出库提供过一个popen以及pclose函数,这两个函数处理了我们需要的所有细节工作:创建管道,创建子进程,关闭无用管道端,执行shell运行命令,并且等待命令结束。
#include <stdio.h>
FILE *popen(const char *cmdstring, const char *type);
返回:如果成功返回文件指针,如果错误返回NULL。
int pclose(FILE *fp);
返回:返回命令cmdstring的终止状态,或者如果错误就返回1。
调用popen返回的FILE * 的文件描述符可以调用接口 fwrite以及fread 进行读写操作。
进行读操作
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *read_fp;
char buffer[BUFSIZ +1];
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp = popen("uname -a","r");
if(read_fp != NULL)
{
chars_read = fread(buffer, sizeof(char),BUFSIZ,read_fp);
if(chars_read > 0)
{
printf("Output was : -\n%s\n",buffer);
}
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
进行写操作:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *read_fp;
char buffer[BUFSIZ +1];
int chars_read;
memset(buffer,'\0',sizeof(buffer));
read_fp = popen("od -c","w");
if(read_fp != NULL)
{
sprintf(buffer, "Once upon a time, there was ...\n");
fwrite(buffer, sizeof(char),strlen(buffer),read_fp);
pclose(read_fp);
exit(EXIT_SUCCESS);
}
exit(EXIT_FAILURE);
}
- linux 进程间管道通讯 popen以及pclose
- Linux中的popen-pclose
- linux popen和pclose
- linux进程间通讯--管道
- Linux-进程间通讯-管道
- Linux进程间通讯--管道(有名管道
- 【进程通信】popen与pclose,创建一个管道连接到另一个进程中
- 两个很有用的进程间通信函数popen,pclose
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- Linux popen和pclose函数
- [第2篇] 谈谈分布式事务之二:基于DTC的分布式事务管理模型[上篇]
- 1088--滑雪 动态规划
- Spring的事务管理难点剖析(5):联合军种作战的混乱
- String.format() (格式化输出)
- mapreduce实现推荐系统
- linux 进程间管道通讯 popen以及pclose
- “隐藏”自己?Get这些网络安全工具
- <C/C++ 版> 设计模式 学习之 策略模式+工厂模式
- 石述思 上海灾难
- 【甘道夫】HBase随机宕机事件处理 & JVM GC回顾
- nopad++快捷键
- java中的PrivateKey
- 毕业设计项目
- Thread线程同步问题详解