??<1>主頁(yè)::我的代碼愛(ài)吃辣
??<2>知識(shí)講解:Linux——進(jìn)程間通信——命名管道
??<3>開(kāi)發(fā)環(huán)境:Centos7
??<4>前言:命名管道是一種特殊的文件存放在文件系統(tǒng)中,而不是像管道那樣存放在內(nèi)核中。命名管道可以用于任何兩個(gè)進(jìn)程間的通信,而不限于同源的兩個(gè)進(jìn)程。當(dāng)進(jìn)程對(duì)命名管道的使用結(jié)束后,命名管道依然存在于文件系統(tǒng)中,除非對(duì)其進(jìn)行刪除操作,否則該命名管道不會(huì)自行消失 。
目錄
一.命名管道特點(diǎn):
?二.創(chuàng)建一個(gè)命名管道
1.命令行創(chuàng)建
2.程序創(chuàng)建
?三.匿名管道與命名管道的區(qū)別
?四.命名管道的打開(kāi)規(guī)則
?五.用命名管道實(shí)現(xiàn)server&client通信
?
一.命名管道特點(diǎn):
- 管道應(yīng)用的一個(gè)限制就是只能在具有共同祖先(具有親緣關(guān)系)的進(jìn)程間通信。
- 如果我們想在不相關(guān)的進(jìn)程之間交換數(shù)據(jù),可以使用FIFO(first in first out)文件來(lái)做這項(xiàng)工作,它經(jīng)常被稱(chēng)為命名管道。
- 命名管道是一種特殊類(lèi)型的文件。
?二.創(chuàng)建一個(gè)命名管道
1.命令行創(chuàng)建
命名管道可以從命令行上創(chuàng)建,命令行方法是使用下面這個(gè)命令:
mkfifo filename
?
2.程序創(chuàng)建
命名管道也可以從程序里創(chuàng)建,相關(guān)函數(shù)有:
int mkfifo(const char *filename,mode_t mode);
?測(cè)試代碼:
#include <cstring>
#include <cerrno>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
const char *namepipe = "./namepipe";
mode_t mode = 0664;
int main()
{
// 1.創(chuàng)建命名管道namepipe,
// namepipe 匿名管道的路徑和名字,mode時(shí)命令管道的創(chuàng)建權(quán)限
// 成功返回0,出錯(cuò)返回1。錯(cuò)誤碼被設(shè)置
int ret = mkfifo(namepipe, mode);
if (ret == -1)
{
cerr << errno << ":" << strerror(errno) << endl;
}
return 0;
}
測(cè)試結(jié)果:
?
?三.匿名管道與命名管道的區(qū)別
- 匿名管道由pipe函數(shù)創(chuàng)建并打開(kāi)。
- 命名管道由mkfifo函數(shù)創(chuàng)建,打開(kāi)用open
- FIFO(命名管道)與pipe(匿名管道)之間唯一的區(qū)別在它們創(chuàng)建與打開(kāi)的方式不同,一但這些工作完成之后,它們具有相同的語(yǔ)義。
?四.命名管道的打開(kāi)規(guī)則
如果當(dāng)前打開(kāi)操作是為讀而打開(kāi)FIFO時(shí):
- ??? O_NONBLOCK disable:阻塞直到有相應(yīng)進(jìn)程為寫(xiě)而打開(kāi)該FIFO
- ??? O_NONBLOCK enable:立刻返回失敗,錯(cuò)誤碼為ENXIO
如果當(dāng)前打開(kāi)操作是為寫(xiě)而打開(kāi)FIFO時(shí):
- ??? O_NONBLOCK disable:阻塞直到有相應(yīng)進(jìn)程為讀而打開(kāi)該FIFO
- ??? O_NONBLOCK enable:立刻返回失敗,錯(cuò)誤碼為ENXIO
?
?五.用命名管道實(shí)現(xiàn)server&client通信
代碼:
Comm.hpp
#include <string.h>
#include <cstdio>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
const char *namepipe = "./namepipe";
mode_t mode = 0666;
server.cc
#include "Comm.hpp"
int main()
{
// 1.創(chuàng)建命名管道namepipe,
// server,client,只需要一端創(chuàng)建管道即可
int ret = mkfifo(namepipe, mode);
if (ret == -1)
{
cerr << errno << ":" << strerror(errno) << endl;
}
// 2.打開(kāi)管道
// server給client發(fā)消息,server以寫(xiě)的方式打開(kāi)
int pipefd = open(namepipe, O_WRONLY);
if (pipefd == -1)
{
cerr << errno << ":" << strerror(errno) << endl;
}
// 3.開(kāi)始通信
while (1)
{
int quit = 0;
char buff[1024] = {0};
char *str = fgets(buff, sizeof(1024), stdin);
// 如果寫(xiě)入的字符串是"quit",將quit制1,用于退出
if (strcmp(str, "quit") == 0)
{
quit = 1;
}
// 向指定的文件描述符寫(xiě)入即可
size_t n = write(pipefd, buff, strlen(str));
if (quit)
{
break;
}
}
return 0;
}
client.cc
#include "Comm.hpp"
int main()
{
// 1.打開(kāi)管道
int pipefd = open(namepipe, O_RDONLY);
// 2.開(kāi)始通信,接受信息
while (1)
{
int quit = 0;
char buff[1024] = {0};
size_t n = read(pipefd, buff, sizeof(buff));
if (n == 0)
continue;
buff[n] = 0;
printf("%s", buff);
fflush(stdout);
if (strcmp(buff, "quit") == 0)
{
break;
}
}
return 0;
}
?makefile:
all:client server
client:client.cc
g++ -o $@ $^ -std=c++11
server:server.cc
g++ -o $@ $^ -std=c++11
.PHONY:clean
clean:
rm -rf client server namepipe
測(cè)試結(jié)果:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-713375.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-713375.html
到了這里,關(guān)于linux——進(jìn)程間通信——命名管道的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!