管道的建立

2021-10-25 22:24:33 字數 1430 閱讀 1562

普通管道建立 失敗則顯示 creat pipe failed

int fd[2];     

int pid;

char buf[128];

if(pipe(fd) ==-1)

然後建立程序  

pid = fork();           //建立程序

if(pid<0)

else if( pid >0)else

父程序 關閉讀的功能 ,只往管道裡面寫資料  子程序關閉寫的功能 ,只從管道裡面讀資料

程式執行結果 先是父程序 往裡面寫資料,然後是子程序 從buf裡面讀資料

fifo 管道命名是一種檔案型別,有路徑名與之關聯,以一種特殊裝置檔案形式存在於檔案系統中

mkfifo("./file",0600);
建立乙個file檔案。

建立fifo管道 如果唯讀開啟檔案會阻塞 必須等到其他程序寫而開啟fifo建立的檔案才會繼續執行  

這是 唯讀開啟檔案 單獨執行無法開啟

char buf[24]=;

if(mkfifo("./file",0600)== -1&& errno !=eexist)

int fd=open("./file",o_rdonly); //唯讀open會阻塞 直到其他程序寫而開啟此fifo 才會執行

printf("open sucess\n");

int nread = read(fd,buf,24);

printf("read %d byte form fifo,context:%s\n",nread,buf);

close(fd);

return 0;

必須等到寫而開啟次此檔案程式執行後才會執行

char *str ="hello word";

int fd=open("./file",o_wronly); //此程序是為了開啟檔案而建立的

printf("write sucess\n");

write(fd,str,strlen(str));

close(fd);

先執行讀檔案 沒有反應

在執行寫檔案

再次觀察讀檔案

此次執行成功。

語言 管道 建立匿名管道

本質 特點 實現方式 侷限性 匿名管道 僅適用於有血緣關係的程序通訊.本功能實現的是 ps axu grep bash 通過呼叫 ps 和 grep命令 然後分別將他們的輸出 和 輸入的檔案描述符改變.從終端改入到管道中.intmain int argc,char ar pid t pid fork...

linux 管道建立程序

程序i o函式,與pclose函式一起使用。include stdio.h file popen const char command,const char type intpclose file stream popen 函式通過建立乙個管道,呼叫 fork 產生乙個子程序,執行乙個 shell ...

建立有名管道

有名管道fifo管道在檔案系統中是可見的 建立函式 include include int mkfifo const char filename,mode t mode mode 用來指定建立的管道的讀寫執行許可權,一般設定為mode t mode 0666 利用命令列中乙個引數來建立乙個有名管道 ...