“命名管道”又名“命名管線”(Named Pipes),是一種簡單的進程間通信(IPC)機制,Microsoft Windows大都提供了對它的支持(但不包括Windows CE)。命名管道可在同一台計算機的不同進程之間或在跨越一個網路的不同計算機的不同進程之間,支持可靠的、單向或雙向的數據通信。推薦用命名管道作為進程通信方案的一項重要的原因是它們充分利用了Windows內建的安全特性(ACL等)。
用命名管道來設計跨計算機應用程式實際非常簡單,並不需要事先深入掌握底層網路傳送協定(如TCP、UDP、IP、IPX)的知識。這是由於命名管道利用了微軟網路提供者(MSNP)重定向器通過同一個網路在各進程間建立通信,這樣一來,應用程式便不必關心網路協定的細節。
基本介紹
- 中文名:命名管道
- 外文名:Named Pipes
- 支持平台:Windows,Linux,Unix等
簡介
作用
套用方向與學習方法
Linux實例
實例1
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>int main(void){ char buf[80];int fd; unlink("zieckey_fifo");mkfifo("zieckey_fifo",0777); if (fork()>0){ chars[]="Hello!\n";fd=open("zieckey_fifo",O_WRONLY); write(fd,s,sizeof(s)); //close(fd); } else { fd=open("zieckey_fifo",O_RDONLY); read(fd,buf,sizeof(buf)); printf("The message from the pipes:%s\n",buf); //close(fd); } return0;}/*執行結果為The message from the pipes:Hello!並且可以在程式執行目錄生成管道檔案zieckey_fifo*/
實例2
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>int main(void){ char buf[80]; int fd; unlink("zieckey_fifo"); mkfifo("zieckey_fifo",0777);}
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>int main(void){ int fd; chars[]="Hello!\n"; fd=open("zieckey_fifo",O_WRONLY); while(1) { write(fd,s,sizeof(s)); sleep(1); } return 0;}
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>int main(void){ int fd; char buf[80]; fd=open("zieckey_fifo",O_RDONLY); while(1) { read(fd,buf,sizeof(buf)); printf("%s\n",buf); sleep(1); } return0;}