點擊(此處)折疊或打開
#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大小就可以看到效果。新聞熱點
疑難解答