unix网络编程卷2:管道和FIFO
2017-02-08 23:58
309 查看
管道和FIFO
管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先的进程之间进行通信。命名管道(named pipe):命名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。命名管道在文件系统中有对应的文件名。命名管道通过命令mkfifo或系统调用mkfifo来创建。
#include <unistd.h> int pipe(int fd[2])
该函数返回两个文件描述符 fd[[0]和fdp[1],前者用于读,后者用于写。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h> #include <string.h> void client(int readfd, int writefd); void server(int readfd, int writefd); int main(int argc,char **argv) { int pipe1[2], pipe2[2]; int childpid; pipe(pipe1); pipe(pipe2); if( (childpid = fork()) == 0) //child { close(pipe1[1]); close(pipe2[0]); server(pipe1[0],pipe2[1]); exit(0); } close(pipe1[0]); close(pipe2[1]); client(pipe2[0],pipe1[1]); waitpid(childpid, NULL, 0); exit(0); } void server(int readfd, int writefd) { int fd; size_t n; char buff[4097]; if( (n = read(readfd, buff, 4096)) == 0) { std::cout<< "read file error " << std::endl; return; } buff = '\0'; std::cout << "the server get filename from client:" << buff << std::endl; if( (fd = open(buff, O_RDONLY)) < 0) { std::cout << "the file file can't open file" << std::endl; write(writefd, buff , n); } else { while( (n = read(fd, buff, 4096)) > 0) { std::cout << "the server read file:" << buff; write(writefd, buff , n); } close(fd); } } void client(int readfd, int writefd) { size_t len; size_t n; char buff[4097]; std::cout<< "pls input filename" << std::endl; fgets(buff, 4096, stdin); len = strlen(buff); if( buff[len-1] == '\n') len--; write(writefd, buff, len); while( (n = read(readfd, buff ,4096)) > 0) { std::cout << "the client read message from server:" <<buff; } }
有一个test文件 文件有一行数据this is a test
FIFO
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <fcntl.h> #include <string.h> #include <errno.h> #define FIFO1 "/tmp/fifo1" #define FIFO2 "/tmp/fifo2" #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) void client(int readfd, int writefd); void server(int readfd, int writefd); int main(int argc, char**argv) { int readfd, writefd; pid_t childpid; if ((mkfifo(FIFO1, FILE_MODE) < 0) && (errno != EEXIST)) printf("can't create %s", FIFO1); if ((mkfifo(FIFO2, FILE_MODE) < 0) && (errno != EEXIST)) { unlink(FIFO1); printf("can't create %s", FIFO2); } if ( (childpid = fork()) == 0) { /* child */ readfd = open(FIFO1, O_RDONLY, 0); writefd = open(FIFO2, O_WRONLY, 0); server(readfd, writefd); exit(0); } /* 4parent */ writefd = open(FIFO1, O_WRONLY, 0); readfd = open(FIFO2, O_RDONLY, 0); client(readfd, writefd); waitpid(childpid, NULL, 0); /* wait for child to terminate */ close(readfd); close(writefd); unlink(FIFO1); unlink(FIFO2); exit(0); } void server(int readfd, int writefd) { int fd; size_t n; char buff[4097]; if( (n = read(readfd, buff, 4096)) == 0) { std::cout<< "read file error " << std::endl; return; } buff = '\0'; std::cout << "the server get filename from client:" << buff << std::endl; if( (fd = open(buff, O_RDONLY)) < 0) { std::cout << "the file file can't open file" << std::endl; write(writefd, buff , n); } else { while( (n = read(fd, buff, 4096)) > 0) { std::cout << "the server read file:" << buff; write(writefd, buff , n); } close(fd); } } void client(int readfd, int writefd) { size_t len; size_t n; char buff[4097]; std::cout<< "pls input filename" << std::endl; fgets(buff, 4096, stdin); len = strlen(buff); if( buff[len-1] == '\n') len--; write(writefd, buff, len); while( (n = read(readfd, buff ,4096)) > 0) { std::cout << "the client read message from server:" <<buff; } }
相关文章推荐
- Linux进程通信:命名管道FIFO小结
- UNPv2:进程间通信(二) 管道和FIFO
- Linux有名管道(FIFO)的阻塞和非阻塞读写
- 有名管道FIFO的使用
- linux中的命名管道FIFO
- 【编撰】linux IPC 002 - 匿名管道PIPE和有名管道FIFO的概念和实例,以及应用比较
- linux 无名管道pipe和有名管道FIFO
- 管道通信:有名管道(FIFO) 和 无名管道(pipe)
- 有名管道(FIFO)实现无亲缘关系的客户服务器
- 【IPC通信】FIFO命名管道
- pipe/fifo 管道与有名管道
- linux系统编程之管道(三):命令管道(FIFO)
- 进程间通讯(一)IPC之管道FIFO
- Linux学习笔记25——命名管道(FIFO)
- Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)
- 进程间通信--管道、命名管道、FIFO
- 命名管道(FIFO)的实现
- Linux--进程间通信(管道及有名管道FIFO)
- linux下进程间的通信——有名管道fifo学习笔记
- 命名管道 FIFO(转)