点击(此处)折叠或打开
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#define N 10#define MAX 100int child_read_pipe(int fd){ char buf[N]; int n = 0; while(1) { n = read(fd,buf,sizeof(buf)); buf[n] = '/0'; printf("Read %d bytes : %s./n",n,buf); if(strncmp(buf,"quit",4) == 0) break; } return 0;}int father_write_pipe(int fd){ char buf[MAX] = {0}; while(1) { printf(">"); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-1] = '/0'; write(fd,buf,strlen(buf)); usleep(500); if(strncmp(buf,"quit",4) == 0) break; } return 0;}int main(){ int pid; int fd[2]; if(pipe(fd) < 0) { perror("Fail to pipe"); exit(EXIT_FAILURE); } if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ close(fd[1]); child_read_pipe(fd[0]); }else{ close(fd[0]); father_write_pipe(fd[1]); } exit(EXIT_SUCCESS);}运行结果:点击(此处)折叠或打开
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/stat.h>#include <sys/types.h>#include <fcntl.h>#define MAX 100int child_work(int pfd,char *fname){ int n,fd; char buf[MAX]; if((fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,0666)) < 0) { fprintf(stderr,"Fail to open %s : %s./n",fname,strerror(errno)); return -1; } while( n = read(pfd,buf,sizeof(buf)) ) { write(fd,buf,n); } close(pfd); return 0;}int father_work(int pfd,char *fname){ int fd,n; char buf[MAX]; if((fd = open(fname,O_RDONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s./n",fname,strerror(errno)); return -1; } while(n = read(fd,buf,sizeof(buf))) { write(pfd,buf,n); } close(pfd); return 0;}int main(int argc,char *argv[]){ int pid; int fd[2]; if(argc < 3) { fprintf(stderr,"usage %s argv[1] argv[2]./n",argv[0]); exit(EXIT_FAILURE); } if(pipe(fd) < 0) { perror("Fail to pipe"); exit(EXIT_FAILURE); } if((pid = fork()) < 0) { perror("Fail to fork"); exit(EXIT_FAILURE); }else if(pid == 0){ close(fd[1]); child_work(fd[0],argv[2]); }else{ close(fd[0]); father_work(fd[1],argv[1]); wait(NULL); } exit(EXIT_SUCCESS);}二、有名管道1.1有名管道的介绍无名管道,由于没有名字,只能用于亲缘关系的进程间通信.。为了克服这个缺点,提出了有名管道(FIFO)。FIFO不同于无名管道之处在于它提供了一个路径名与之关联,以FIFO的文件形式存在于文件系统中,这样,即使与FIFO的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过FIFO相互通信,因此,通过FIFO不相关的进程也能交换数据。值的注意的是,FIFO严格遵循先进先出(first in first out),对管道及FIFO的读总是从开始处返回数据,对它们的写则把数据添加到末尾。它们不支持诸如lseek()等文件定位操作。注意:有名管道的名字存在于文件系统中,内容存放在内存中。1.2有名管道的创建点击(此处)折叠或打开
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc,char *argv[]){ int fd; if(argc < 2) { fprintf(stderr,"usage : %s argv[1]./n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { fprintf(stderr,"Fail to mkfifo %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((fd = open(argv[1],O_WRONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open for write success./n"); return 0;}B.open for read点击(此处)折叠或打开
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int main(int argc,char *argv[]){ int fd; if(argc < 2) { fprintf(stderr,"usage : %s argv[1]./n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { fprintf(stderr,"Fail to mkfifo %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((fd = open(argv[1],O_RDONLY)) < 0) { fprintf(stderr,"Fail to open %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open for read success./n"); return 0;}探究发现,如果open时没有使用O_NONBLOCK参数,我们发现不论读端还是写端先打开,先打开者都会阻塞,一直阻塞到另一端打开。读者自己可以探究,如果open时使用了O_NONBLOCK参数,此时打开FIFO 又会是什么情况?1.4有名管道的读写规则A.从FIFO中读取数据约定:如果一个进程为了从FIFO中读取数据而以阻塞的方式打开FIFO, 则称内核为该进程的读操作设置了阻塞标志<1>如果有进程为写而打开FIFO,且当前FIFO内没有数据,则对于设置了阻塞标志的读操作来说,将一直阻塞。对于没有设置阻塞标志读操作来说返回-1,当前errno值为EAGAIN,提醒以后再试。<2>对于设置阻塞标志的读操作说,造成阻塞的原因有两种:当前FIFO内有数据,但有其他进程正在读这些数据;另外就是FIFO内没有数据。解阻塞的原因则是FIFO中有新的数据写入,不论写入数据量的大小,也不论读操作请求多少数据量。<3>如果没有进程写打开FIFO,则设置了阻塞标志的读操作会阻塞<4>如果写端关闭,管道中有数据读取管道中的数据,如果管道中没有数据读端将不会继续阻塞,此时返回0。注意:如果FIFO中有数据,则设置了阻塞标志的读操作不会因为FIFO中的字节数小于请求读的字节数而阻塞,此时,读操作会返回FIFO中现有的数据量。B.向FIFO中写入数据约定:如果一个进程为了向FIFO中写入数据而阻塞打开FIFO,那么称该进程内的写操作设置了阻塞标志。对于设置了阻塞标志的写操作:<1>当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。如果此时管道空闲缓冲区不足以容纳要写入的字节数,则进入睡眠,直到当缓冲区中能够容纳写入的字节数时,才开始进行一次性写操作。<2>当要写入的数据量大于PIPE_BUF时,Linux将不再保证写入的原子性。FIFO缓冲区一有空闲区域,写进程就会试图向管道写入数据,写操作在写完所有请求写的数据后返回。对于没有设置阻塞标志的写操作:<1>当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。在写满所有FIFO空闲缓冲区后,写操作返回。<2>当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。如果当前FIFO空闲缓冲区能够容纳请求写入的字节数,写完后成功返回;如果当前FIFO空闲缓冲区不能够容纳请求写入的字节数,则返回EAGAIN错误,提醒以后再写。注意:只有读端存在,写端才有意义。如果读端不在,写端向FIFO写数据,内核将向对应的进程发送SIGPIPE信号(默认终止进程);案例一、write to FIFO#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define MAX 655360int main(int argc,char *argv[]){ int n,fd; char buf[MAX]; if(argc < 2) { fprintf(stderr,"usage : %s argv[1]./n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { fprintf(stderr,"Fail to mkfifo %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((fd = open(argv[1],O_WRONLY )) < 0) { fprintf(stderr,"Fail to open %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open for write success./n"); while(1) { printf(">"); scanf("%d",&n); n = write(fd,buf,n); printf("write %d bytes./n",n); } exit(EXIT_SUCCESS);}read from FIFO点击(此处)折叠或打开
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#define MAX 655360int main(int argc,char *argv[]){ int fd,n; char buf[MAX]; if(argc < 2) { fprintf(stderr,"usage : %s argv[1]./n",argv[0]); exit(EXIT_FAILURE); } if(mkfifo(argv[1],0666) < 0 && errno != EEXIST) { fprintf(stderr,"Fail to mkfifo %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((fd = open(argv[1],O_RDONLY )) < 0) { fprintf(stderr,"Fail to open %s : %s./n",argv[1],strerror(errno)); exit(EXIT_FAILURE); } printf("open for read success./n"); while(1) { printf(">"); scanf("%d",&n); n = read(fd,buf,n); printf("Read %d bytes./n",n); } exit(EXIT_SUCCESS);}读者可以将这两个程序运行,然后输入read和write FIFO大小就可以看到效果。新闻热点
疑难解答