紧接着上面一个博客的简单介绍,下面进行一个没有血缘关系的进程间通信的实例,实现文件拷贝传输。

有两个进程,一个主要是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;
}

最新文章

  1. PHP函数基础知识.png
  2. SAX和DOM解析的区别
  3. wpf框架模型分析
  4. JS open App(未安装就跳转下载页面)
  5. BZOJ 1822 Frozen Nova 冷冻波(最大流)
  6. 网站安全扫描工具--Netsparker的使用
  7. dll--二进制层面的复用
  8. win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
  9. Matlab之类型转换
  10. 使用微软分布式缓存服务Velocity(Windows Server AppFabric Caching Service)
  11. day55
  12. poj2253(最短路小变形)
  13. 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持多包扫描(六)
  14. Mac os x下几款mysql客户端
  15. 我理解的websocket
  16. 初识Velocity
  17. Python_函数的初识、函数的返回值、函数的参数
  18. Git命令行基本操作
  19. css中的float属性以及清除方法 (2011-09-03 17:36:26)
  20. JavaScript中十种一步拷贝数组的方法

热门文章

  1. Rust之路(3)——数据类型 下篇
  2. 怎样学好 java ?
  3. MeteoInfoLab脚本示例:读取远程文件
  4. localhost与127.0.0.1与0.0.0.0
  5. 【传递闭包】HDU 2157 How many ways??
  6. ORACLE结构化查询语句
  7. linux创建www用户组和用户
  8. Tomcat6.0 支持 https
  9. 面经分享:看非科班研究生如何转行斩获 ATM 大厂的 Offer ?
  10. CentOS8安装本地mail工具-mailx-12.5-29.el8.x86_64