標籤:style blog http color os 使用 io strong 檔案
1.標準流管道管道操作支援檔案流模式,用來建立連結另一個進程的管道,通過函數popen和pclosepopen的具體介紹在本blog:Linux 多進程學習中有詳細介紹2.無名管道(PIPE)特點:1)只能在親緣關係處理序間通訊(父子或兄弟)2)半雙工(固定的讀端和固定的寫端)3)是特殊檔案,可以用read,write等,在記憶體中管道函數原型:#include<unistd.h>int pipe(int fds[2]);管道在程式中用一對檔案描述符表示,一個是可讀屬性,一個是可寫屬性:fds[0]讀,fds[1]寫兩個進程必須有繼承關係,才能繼承這對開啟的檔案描述符
int main(){int fds[2];if(pipe(fds)==-1){perror("pipe:");return 0;}char buf[1024]="";if(fork()==0){close(fds[1]);while(memset(buf,0,sizeof(buf))){if(read(fds[0],buf,1024)==0){//當管道裡沒有資料的時候就退出break;}printf("child:read:");puts(buf);}exit(1);}else{close(fds[0]);//char p[1024];//char *p="Hello world!";while(memset(buf,0,1024),fgets(buf,1024,stdin)!=NULL)write(fds[1],buf,1024);close(fds[1]);printf("parents,finish\n");wait(NULL);//wait一定要放在close的後面,因為只有先將父進程的fds[1]關掉,當子進程沒有資料讀的時候才會退出,否則會導致死結}}
3具名管道(FIFO)無名管道只能在親緣關係的處理序間通訊大大限制的管道的使用,有名管道突破了這個限制,通過制定路徑名的範式實現不相關的進程通訊3.1建立,刪除FIFO檔案建立FIFO檔案與建立普通檔案很類似,只是建立之後的檔案FIFO建立FIFO檔案的函數原型#include<sys/types.h>#include<sys/stat.h>int mkfifo(const char *pathname,mode_t mode);參數pathname為要建立的FIFO檔案的全路徑名;參數mode為檔案的存取權限建立成功返回0,建立失敗為-1刪除FIFO檔案的函數原型為int unlink(const char *pathname);例子:
#include<iostream>using namespace std;int main(int argc,char *argv[]){if(mkfifo(argv[1],0666)==-1){perror("ew");return 0;}unlink(argv[1]);}3.2開啟,關閉FIFO檔案對於FIFO類型的檔案的開啟/關閉更普通檔案一樣,都是用open和close函數。用O_WRONLY開啟FIFO的寫入端,用O_RDONLY選項,則開啟FIFO的讀入端,寫入讀入端都可以被幾個進程同時開啟。fd_recv=open(argv[1],O_RDONLY);
fd_send=open(argv[2],O_WRONLY);
if(fd_send==-1){
perror("fd_send");
exit(1);
}
3.3讀寫FIFO讀:fd_recv=open(argv[1],O_RDONLY);
char buf_recv[1024]="";
if(read(fd_recv,buf_recv,1024)!=0) write(1,buf_recv,strlen(buf_recv));close (fd_recv)寫:fd_send=open(argv[2],O_WRONLY);
char buf_send[1024]="";
if(fgets(buf_send,1024,stdin)!=NULL) write(fp_send,buf_send,strlen(buf_send));close(fd_send);程式示範:pork_send.c與pork_recv.c聊天使用fork開闢留一個進程,兩個程式可以進行聊天功能
/*************************************************************************> File Name: pork_send.c> Author: yang> Mail:[email protected] > Created Time: 2014年08月22日 星期五 19:54:08 ************************************************************************/#include<stdio.h>#include<string.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<memory.h>int main(int argc,char *argv[]){if(mkfifo(argv[1],0666)==-1){perror("mkfifo1");exit(1);}int fd_send,fd_recv;fd_send=open(argv[1],O_WRONLY);fd_recv=open(argv[2],O_RDONLY);char buf_recv[1024]="";char buf_send[1024]="";printf("opening!\n");if(fork()==0){//開皮一個進程,用來接受資訊close(fd_send);while(memset(buf_recv,0,sizeof(buf_recv)),read(fd_recv,buf_recv,1024)>0){write(1,buf_recv,strlen(buf_recv));}close(fd_recv);exit(1);}close(fd_recv);while(memset(buf_send,0,sizeof(buf_send)),fgets(buf_send,1024,stdin)!=NULL){write(fd_send,buf_send,strlen(buf_send));//用來發送資訊}close(fd_send);wait(NULL);}
/*************************************************************************> File Name: pork_recv.c> Author: yang> Mail:[email protected] > Created Time: 2014年08月22日 星期五 20:12:32 ************************************************************************/#include<string.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<memory.h>#include<stdio.h>int main(int argc,char *argv[]){if(mkfifo(argv[2],0666)==-1){perror("mkfifo2");exit(1);}int fd_send,fd_recv;fd_recv=open(argv[1],O_RDONLY);//注意這裡與上面程式的這個位置有所不一樣fd_send=open(argv[2],O_WRONLY);char buf_recv[1024]="";char buf_send[1024]="";printf("opening!\n");if(fork()==0){//接受資訊close(fd_send);while(memset(buf_recv,0,sizeof(buf_recv)),read(fd_recv,buf_recv,1024)>0){write(1,buf_recv,strlen(buf_recv));}close(fd_recv);exit(1);}close(fd_recv);//發送資訊while(memset(buf_send,0,sizeof(buf_send)),fgets(buf_send,1024,stdin)!=NULL){write(fd_send,buf_send,strlen(buf_send));}close(fd_send);wait(NULL);}
Linux IPC(Inter-Process Communication,處理序間通訊)之管道學習