命名管道使用
来源:互联网 发布:广西专业技术人员网络 编辑:程序博客网 时间:2024/05/29 11:14
转载请注明t1234xy4原创:http://blog.csdn.net/t1234xy4/article/details/51317123
命名管道
命名管道相对于管道(pipe)有许多优点:
1、命令管道可以用于非父子关系的进程的通信;而pipe只能用于父进程创建子进程,然后进行通信,多用于shell命令。
2、命令管道是以文件的形式保存在文件中,因此可以提供给多个知道进程名字的进程通信。
3、命令管道又叫FIFO 使用mkfifo()创建。
简单的例子
client创建一个有名管道,server创建一个有名管道。client通过server的管道将自身的pid传送给server,server将通过client的管道返回值
server
//============================================================================// Name : server.cpp// Author : Tian// Version :// Copyright : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include <sys/types.h>#include <sys/stat.h>#include <sys/ipc.h>#include <sys/errno.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAXLINE 1024#define FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP |S_IROTH)#define SERV_FIFO "/tmp/serv.fifo"int main(int argc,char **argv) {int readfifo,writefifo,dummyfd;int fd;char *ptr,buff[MAXLINE+1],fifoname[MAXLINE];pid_t pid;ssize_t n;if( (mkfifo(SERV_FIFO,FIFO_MODE) < 0 ) && (errno != EEXIST) ) //创建printf("can't create %s",SERV_FIFO);readfifo = open(SERV_FIFO,O_RDONLY|O_NONBLOCK,0); // O_NONBLOCK不阻塞,否则可能会阻塞在此dummyfd = open(SERV_FIFO,O_RDONLY|O_NONBLOCK,0);while( (n = read(readfifo,buff,MAXLINE)) >=0 ){if(n == 0)continue;if(buff[n-1] == '\n')n--;buff[n] = '\0';if( (ptr = strchr(buff,'\0')) == NULL ){printf("bogus request :%s",buff);continue;}*ptr++ = 0;pid = atol(buff); snprintf(fifoname,sizeof(fifoname),"/tmp/fifo.%ld",(long)pid);if( (writefifo = open(fifoname,O_WRONLY,0)) < 0){printf("cannot open:%s",fifoname);continue;}if( (fd=open(ptr,O_WRONLY,0)) < 0 ){snprintf(buff+n,sizeof(buff)-n,":can't open,%s\n",strerror(errno));n = strlen(ptr);write(writefifo,ptr,n);close(writefifo);}else {while( (n = read(fd,buff,MAXLINE)) > 0 )write(writefifo,buff,n);close(fd);close(writefifo);}}exit(0); //#include<fcntl.h>}
client:
//============================================================================// Name : server.cpp// Author : Tian// Version :// Copyright : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include <iostream>#include <sys/types.h>#include <sys/stat.h>#include <sys/ipc.h>#include <sys/errno.h>#include <unistd.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;#define MAXLINE 1024#define FIFO_MODE (S_IRUSR | S_IWUSR | S_IRGRP |S_IROTH)#define SERV_FIFO "/tmp/serv.fifo"int main(int argc,char **argv) {int readfifo,writefifo;char *ptr,buff[MAXLINE+1],fifoname[MAXLINE];pid_t pid;size_t len;ssize_t n;pid = getpid();snprintf(fifoname,sizeof(fifoname),"/tmp/fifo.%ld",(long)pid);if( (mkfifo(fifoname,FIFO_MODE) <0) && (errno!=EEXIST))printf("can't create %s",fifoname);snprintf(buff,sizeof(buff),"%ld",(long)pid);len = strlen(buff);ptr = buff+len;//fgets(ptr,MAXLINE-len,stdin);//len = strlen(buff);writefifo = open(SERV_FIFO,O_WRONLY,0);write(writefifo,buff,len);readfifo = open(fifoname,O_RDONLY|O_NONBLOCK,0);while((n = read(readfifo,buff,MAXLINE)) >= 0){if(n == 0)continue;write(STDOUT_FILENO,buff,n);}close(readfifo);unlink(fifoname);exit(0);}
0 0
- 命名管道的使用
- 命名管道的使用
- linux 使用命名管道
- 使用命名管道
- 命名管道使用
- 使用命名管道传输对象
- 命名管道的简单使用
- 命名管道的简单使用
- 命名管道文件的使用
- 使用命名管道连接MySQL
- 使用命名管道访问SQL Server
- 使用命名管道访问SQL Server
- 使用命名管道实现进程间通信
- 使用命名管道实现进程间通信
- 使用命名管道实现进程间通信
- 命名管道的创建与使用
- .net 命名管道(NamedPipe) 的使用
- 使用命名管道实现进程间通信
- JDBC连接MySQL产生java.lang.ClassNotFoundException异常
- Linux设备模型(2)_Kobject
- Android学习记录(十一) 填坑之inputStream close关闭慢的处理。
- PLSQL Developer使用技巧
- Vim:黑客帝国屏保
- 命名管道使用
- Android亮度调整解析
- 在Vim中玩俄罗斯方块
- Windows下游戏制作(1)---win32窗口创建(2)
- 高仿今日头条(2)
- 欢迎使用CSDN-markdown编辑器
- Mysql之建立主从同步
- python csv模块使用实例
- Cisco Packet Tracer Student的路由器接口配置