點擊(此處)折疊或打開
#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);}運行結(jié)果:點擊(此處)折疊或打開
#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有名管道的介紹無名管道,由于沒有名字,只能用于親緣關(guān)系的進(jìn)程間通信.。為了克服這個缺點,提出了有名管道(FIFO)。FIFO不同于無名管道之處在于它提供了一個路徑名與之關(guān)聯(lián),以FIFO的文件形式存在于文件系統(tǒng)中,這樣,即使與FIFO的創(chuàng)建進(jìn)程不存在親緣關(guān)系的進(jìn)程,只要可以訪問該路徑,就能夠彼此通過FIFO相互通信,因此,通過FIFO不相關(guān)的進(jìn)程也能交換數(shù)據(jù)。值的注意的是,F(xiàn)IFO嚴(yán)格遵循先進(jìn)先出(first in first out),對管道及FIFO的讀總是從開始處返回數(shù)據(jù),對它們的寫則把數(shù)據(jù)添加到末尾。它們不支持諸如lseek()等文件定位操作。注意:有名管道的名字存在于文件系統(tǒng)中,內(nèi)容存放在內(nèi)存中。1.2有名管道的創(chuàng)建點擊(此處)折疊或打開
#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;}探究發(fā)現(xiàn),如果open時沒有使用O_NONBLOCK參數(shù),我們發(fā)現(xiàn)不論讀端還是寫端先打開,先打開者都會阻塞,一直阻塞到另一端打開。讀者自己可以探究,如果open時使用了O_NONBLOCK參數(shù),此時打開FIFO 又會是什么情況?1.4有名管道的讀寫規(guī)則A.從FIFO中讀取數(shù)據(jù)約定:如果一個進(jìn)程為了從FIFO中讀取數(shù)據(jù)而以阻塞的方式打開FIFO, 則稱內(nèi)核為該進(jìn)程的讀操作設(shè)置了阻塞標(biāo)志<1>如果有進(jìn)程為寫而打開FIFO,且當(dāng)前FIFO內(nèi)沒有數(shù)據(jù),則對于設(shè)置了阻塞標(biāo)志的讀操作來說,將一直阻塞。對于沒有設(shè)置阻塞標(biāo)志讀操作來說返回-1,當(dāng)前errno值為EAGAIN,提醒以后再試。<2>對于設(shè)置阻塞標(biāo)志的讀操作說,造成阻塞的原因有兩種:當(dāng)前FIFO內(nèi)有數(shù)據(jù),但有其他進(jìn)程正在讀這些數(shù)據(jù);另外就是FIFO內(nèi)沒有數(shù)據(jù)。解阻塞的原因則是FIFO中有新的數(shù)據(jù)寫入,不論寫入數(shù)據(jù)量的大小,也不論讀操作請求多少數(shù)據(jù)量。<3>如果沒有進(jìn)程寫打開FIFO,則設(shè)置了阻塞標(biāo)志的讀操作會阻塞<4>如果寫端關(guān)閉,管道中有數(shù)據(jù)讀取管道中的數(shù)據(jù),如果管道中沒有數(shù)據(jù)讀端將不會繼續(xù)阻塞,此時返回0。注意:如果FIFO中有數(shù)據(jù),則設(shè)置了阻塞標(biāo)志的讀操作不會因為FIFO中的字節(jié)數(shù)小于請求讀的字節(jié)數(shù)而阻塞,此時,讀操作會返回FIFO中現(xiàn)有的數(shù)據(jù)量。B.向FIFO中寫入數(shù)據(jù)約定:如果一個進(jìn)程為了向FIFO中寫入數(shù)據(jù)而阻塞打開FIFO,那么稱該進(jìn)程內(nèi)的寫操作設(shè)置了阻塞標(biāo)志。對于設(shè)置了阻塞標(biāo)志的寫操作:<1>當(dāng)要寫入的數(shù)據(jù)量不大于PIPE_BUF時,linux將保證寫入的原子性。如果此時管道空閑緩沖區(qū)不足以容納要寫入的字節(jié)數(shù),則進(jìn)入睡眠,直到當(dāng)緩沖區(qū)中能夠容納寫入的字節(jié)數(shù)時,才開始進(jìn)行一次性寫操作。<2>當(dāng)要寫入的數(shù)據(jù)量大于PIPE_BUF時,Linux將不再保證寫入的原子性。FIFO緩沖區(qū)一有空閑區(qū)域,寫進(jìn)程就會試圖向管道寫入數(shù)據(jù),寫操作在寫完所有請求寫的數(shù)據(jù)后返回。對于沒有設(shè)置阻塞標(biāo)志的寫操作:<1>當(dāng)要寫入的數(shù)據(jù)量大于PIPE_BUF時,linux將不再保證寫入的原子性。在寫滿所有FIFO空閑緩沖區(qū)后,寫操作返回。<2>當(dāng)要寫入的數(shù)據(jù)量不大于PIPE_BUF時,linux將保證寫入的原子性。如果當(dāng)前FIFO空閑緩沖區(qū)能夠容納請求寫入的字節(jié)數(shù),寫完后成功返回;如果當(dāng)前FIFO空閑緩沖區(qū)不能夠容納請求寫入的字節(jié)數(shù),則返回EAGAIN錯誤,提醒以后再寫。注意:只有讀端存在,寫端才有意義。如果讀端不在,寫端向FIFO寫數(shù)據(jù),內(nèi)核將向?qū)?yīng)的進(jìn)程發(fā)送SIGPIPE信號(默認(rèn)終止進(jìn)程);案例一、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大小就可以看到效果。新聞熱點
疑難解答