利用有名管道实现连个独立进程的对话程序
2016-01-12 17:34
459 查看
初学Linux系统编程,对进程的一些问题都不甚明了,近日学习了进程间通讯的章节,晚上和朋友讨论qq的通讯实现的方式,但还不很是明了。直到在网上看一些高手的博客,才发觉使用父子进程可以有效地解决这个问题。于是,自己就试着写了两个进程利用有名管道的可以对话的程序。
特分享一些,望初学Linux系统编程的共勉。
主要分为服务器端程序和客户端程序。其中他们又都有父子进程。利用两个有名管道实现对话通信。
服务器端程序
客户端程序
特分享一些,望初学Linux系统编程的共勉。
主要分为服务器端程序和客户端程序。其中他们又都有父子进程。利用两个有名管道实现对话通信。
服务器端程序
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> int main(int argc,char *argv[]) { int fd,fd1; pid_t fpid; char buf[20],buf1[20]; int bytes_read,bytes_write; int bytes_read1,bytes_write1; /* if(argc<2) { printf("输入重新!\n"); exit(9); } */ if((mkfifo("myfifo",0666))<0) { printf("creat pipe error!!\n"); exit(1); } if((mkfifo("yourfifo",0666))<0) { printf("2___creat pipe error!!\n"); exit(2); } fpid=fork(); /* if((fd=open("myfifo",O_RDWR))<0) { printf("open pipe error!\n"); exit(2); } */ /*子进程,用来接收客户端进程发送来的信息*/ if(fpid==0) { while(1) { printf(">"); if((fd1=open("yourfifo",O_RDWR))<0) { printf("open yourfifo error!\n"); exit(10); } if((bytes_read=read(fd1,buf1,sizeof(buf1)))<0) { printf("read yourfifo error!\n"); exit(11); } buf1[bytes_read]='\0'; printf("%s\n",buf1); close(fd1); } } /*父进程,用于给客户端发送信息*/ if(fpid>0) { while(1) { if((fd=open("myfifo",O_RDWR))<0) { printf("open pipe error!\n"); exit(2); } printf(">"); scanf("%s",buf); if((strcmp(buf,"quit"))==0) { exit(0); } /* if((bytes_read=read(fd,buf,sizeof(buf)))<0) { printf("read fifo error!!\n"); exit(2); } if(bytes_read>0) { buf[bytes_read]='\0'; printf("%s\n",buf); } */ if((bytes_write=write(fd,buf,sizeof(buf)))<0) { printf("write pipe error!!\n"); exit(3); } close(fd); } // close(fd); } return 0; }
客户端程序
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> int main(int argc,char *argv[]) { int fd; int fd1,fpid; char buf[20],buf1[20]; int bytes_read,bytes_write; int bytes_read1,bytes_write1; /* if(argc<2) { printf("输入重新!\n"); exit(9); } */ /* if((mkfifo("yourfifo",0666))<0) { printf("creat pipe error!!\n"); exit(1); } */ /* if((fd=open("myfifo",O_RDWR))<0) { printf("open pipe error!\n"); exit(2); } */ fpid=fork(); /*子进程,发送信息给服务器端*/ if(fpid==0) { while(1) { printf(">"); scanf("%s",buf1); if((strcmp(buf1,"quit"))==0) { exit(0); } if((fd1=open("yourfifo",O_RDWR))<0) { printf("open yourfifo error!\n"); exit(10); } if((bytes_write1=write(fd1,buf1,sizeof(buf1)))<0) { printf("write yourfifo error!\n"); exit(11); } close(fd1); } } /*父进程,接收从服务器端发送来的信息*/ if(fpid>0) { while(1) { /* printf(">"); scanf("%s",buf); if((strcmp(buf,"quit"))==0) { exit(0); } */ if((fd=open("myfifo",O_RDWR))<0) { printf("open pipe error!\n"); exit(2); } if((bytes_read=read(fd,buf,sizeof(buf)))<0) { printf("read fifo error!!\n"); exit(2); } buf[bytes_read]='\0'; printf("%s\n",buf); close(fd); /* if((bytes_write=write(fd,argv[1],strlen(argv[1])))<0) { printf("write pipe error!!\n"); exit(3); } */ } // close(fd); } return 0; }
相关文章推荐
- ED2k Resource
- ASP.NET MVC4.0+EF+LINQ+bui+bootstrap+网站+角色权限管理系统(开篇)
- 如何用 React Native 创建一个iOS APP?(二)
- nginx 日志过滤网络爬虫
- MySQL 触发器学习
- 一篇浅显介绍逻辑回归的文章
- NoSQL开篇——为什么要使用NoSQL
- UIAlertController 的使用(NS_CLASS_AVAILABLE_IOS(8_0)iOS8以后有效)
- 65条最常用正则表达式
- UIview转换UIimage
- android v4,v7-RecyclerView基于23的源码
- jQuery中$(function(){})与(function($){})(jQuery)、$(document).ready(function(){})等的区别详细讲解
- 控制台打印当月月历
- Android 高亮指示层提示
- 在IE中"对象不支持indexOf属性或方法"
- AngularJS中变量的作用域
- win7搭建ios开发环境
- nginx 根据IP 进行灰度发布
- English-句子的关系代词 that which....
- 安卓误区(1)