有名管道FIFO进程间数据传输实例
2024-09-06 21:09:46
紧接着上面一个博客的简单介绍,下面进行一个没有血缘关系的进程间通信的实例,实现文件拷贝传输。
有两个进程,一个主要是fifow进程:读文件Makefile内容,写入管道;另一个进程fifor:读管道内容,写入到Makefile2。
首先,写端会创建一个管道,然后读取Makefile内容,写入到管道tp中:
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h> #include<signal.h>
#define ERR_EXIT(m)\
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0) //宏要求一条语句
int main(int argc,char*argv[])
{
int infd;
umask(0);
mkfifo("tp",0644);//创建tp管道
infd=open("Makefile",O_RDONLY);//打开Makefile
if(infd==-1)
ERR_EXIT("open error");
int outfd;
outfd=open("tp",O_WRONLY);
if(outfd==-1)
ERR_EXIT("open error");
char buf[1024];
int n;
while((n=read(infd,buf,1024))>0)//读Makefile数据
{
write(outfd,buf,n);//写入管道
}
close(infd);
close(outfd);
return 0;
}
下面的进程就是读取管道数据,管道中有Makefile的内容,将它们读取出来,然后写入Makefile2,就可以实现拷贝功能了。
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<string.h> #include<signal.h>
#define ERR_EXIT(m)\
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main(int argc,char*argv[])
{
int outfd;
umask(0);
outfd=open("Makefile2",O_WRONLY|O_CREAT|O_TRUNC,0644);//打开Makefile
if(outfd==-1)
ERR_EXIT("open error");
int infd;//读取管道数据。
infd=open("tp",O_RDONLY);
if(infd==-1)
ERR_EXIT("open error");
char buf[1024];
int n;
while((n=read(infd,buf,1024))>0)//读管道数据
{
write(outfd,buf,n);//写入Makefile2
}
close(infd);
close(outfd);
unlink("tp");
return 0;
}
最新文章
- PHP函数基础知识.png
- SAX和DOM解析的区别
- wpf框架模型分析
- JS open App(未安装就跳转下载页面)
- BZOJ 1822 Frozen Nova 冷冻波(最大流)
- 网站安全扫描工具--Netsparker的使用
- dll--二进制层面的复用
- win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
- Matlab之类型转换
- 使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)
- day55
- poj2253(最短路小变形)
- 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持多包扫描(六)
- Mac os x下几款mysql客户端
- 我理解的websocket
- 初识Velocity
- Python_函数的初识、函数的返回值、函数的参数
- Git命令行基本操作
- css中的float属性以及清除方法 (2011-09-03 17:36:26)
- JavaScript中十种一步拷贝数组的方法
热门文章
- Rust之路(3)——数据类型 下篇
- 怎样学好 java ?
- MeteoInfoLab脚本示例:读取远程文件
- localhost与127.0.0.1与0.0.0.0
- 【传递闭包】HDU 2157 How many ways??
- ORACLE结构化查询语句
- linux创建www用户组和用户
- Tomcat6.0 支持 https
- 面经分享:看非科班研究生如何转行斩获 ATM 大厂的 Offer ?
- CentOS8安装本地mail工具-mailx-12.5-29.el8.x86_64