命名管道是一種用于進(jìn)程間通信的機(jī)制,能夠在沒有親緣關(guān)系的進(jìn)程之間進(jìn)行數(shù)據(jù)交換。它們被稱為命名管道,因?yàn)樗鼈兺ㄟ^文件系統(tǒng)中的一個(gè)特殊文件來實(shí)現(xiàn)通信,盡管這個(gè)文件實(shí)際上并不存儲(chǔ)數(shù)據(jù),而是維護(hù)一個(gè)內(nèi)存中的緩沖區(qū),執(zhí)行先進(jìn)先出的數(shù)據(jù)傳輸。
要在文件系統(tǒng)中創(chuàng)建一個(gè)命名管道,可以使用mkfifo()或mknod()函數(shù)。創(chuàng)建后,一個(gè)進(jìn)程可以以寫入模式打開該管道并向其中寫入數(shù)據(jù),而另一個(gè)進(jìn)程則可以以讀取模式打開該管道并從中讀取數(shù)據(jù)。數(shù)據(jù)在命名管道中的流動(dòng)是單向的。
從命令行創(chuàng)建命名管道可以使用以下命令:
mkfifo filename
以p開頭的文件表示它是一個(gè)管道文件。
命名管道的讀寫操作是阻塞的,且管道文件的大小始終為0,因?yàn)閿?shù)據(jù)存儲(chǔ)在內(nèi)存中。
命名管道也可以通過編程方式創(chuàng)建,使用以下函數(shù):
為了管理命名管道的生命周期,可以創(chuàng)建一個(gè)namedPipe.hpp文件,提供管道創(chuàng)建和關(guān)閉的函數(shù):
#pragma once #include<iostream> #include <sys> #include <sys> #include<String> #include<cerrno> #include<cstdio> #include<unistd.h> using namespace std; <p>const string comm_path ="./myfifo";</p><p>int CreateNamedPipe(const string & path) { int res =mkfifo(path.c_str(),0666); if(res!=0) { perror("mkfifo"); } return res; }</p><p>int RemoveNamedPipe(const string & path) { int res =unlink(path.c_str()); if(res!=0) { perror("unlink"); } return res; } </unistd.h></cstdio></cerrno></string></sys></sys></iostream>
在server.cc中,可以使用這些函數(shù)來創(chuàng)建和刪除命名管道:
#include"namedPipe.hpp"</p><p>int main() { CreateNamedPipe(comm_path); RemoveNamedPipe(comm_path); return 0; }
為了簡(jiǎn)化操作,可以將這些功能封裝在一個(gè)類中:
class NamedPipe { public: NamedPipe(const string &path) : _fifo_path(path) { int res = mkfifo(path.c_str(), 0666); if (res != 0) { perror("mkfifo"); } } ~NamedPipe() { int res = unlink(_fifo_path.c_str()); if (res != 0) { perror("unlink"); } } private: const string _fifo_path; };
這樣,主函數(shù)中只需創(chuàng)建一個(gè)NamedPipe對(duì)象即可:
NamedPipe myfifo(comm_path);
管道會(huì)在對(duì)象銷毀時(shí)自動(dòng)釋放。
進(jìn)一步地,可以在類中添加其他功能,如打開管道、讀取和寫入數(shù)據(jù):
const string comm_path = "./myfifo";</p><h1>define DefaultFd -1</h1><h1>define Creater 1</h1><h1>define User 2</h1><h1>define Read O_RDONLY</h1><h1>define Write O_WRONLY</h1><p>class NamedPipe { private: bool OpenNamedPipe(int mode) { _fd = open(_fifo_path.c_str(), mode); if (_fd
隱藏打開文件的函數(shù)操作,并定義宏常量Read和Write來指定打開文件的方式。初始化時(shí)傳遞執(zhí)行者身份(創(chuàng)建者還是使用者),以決定是否需要再次創(chuàng)建管道文件。
int ReadNamedPipe(string <em> out)//輸出型參數(shù),輸入型const& 輸入輸出型& { char buffer[BaseSize]; int n = read(_fd,buffer,sizeof(buffer)); if(n>0) { buffer[n]=0; </em>out = buffer; }</p><pre class="brush:php;toolbar:false">return n;
} int WriteNamedPipe(const string & in) { write(_fd,in.c_str(),in.size()); }