Linux--进程间的通信-命名管道

马肤
这是懒羊羊

前文:

Linux–进程间的通信-匿名管道

Linux–进程间的通信–进程池

命名管道的概念

命名管道是一种进程间通信(IPC)机制,运行不同进程之间进行可靠的、单向或双向的数据通信。

特点和作用:

  1. 跨平台性:在WIndows和Linux系统中都有实现,因此具有良好的跨平台性。
  2. 简单易用: 使用命名管道进行通信相对简单。
  3. 安全性:命名管道充分利用了操作系统提供的安全特性,如WIndows的访问控制列表(ACL),从而确保通信的安全性。

命名管道的模拟实现

Linux--进程间的通信-命名管道,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,服务,li,进行,第1张

mkfifo是用于创建命名管道的命令,它的作用是在文件系统中创建一个特殊类型的文件,该文件可被多个进程用于进程间的通信。

Linux--进程间的通信-命名管道,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,服务,li,进行,第2张

这是mkfifo函数。

代码

Comm.hpp: 这里面包含了一个有关命名管道的类,包括管道的创建和销毁。

#ifndef __COMM_HPP__
#define __COMM_HPP__
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define Mode 0666
#define Path "./fifo"
class fifo
{
public:
    fifo(const string & path)
    :_path(path)
    {
        umask(0);
        int n=mkfifo(_path.c_str(),Mode);
        if(n==0)
        {
            cout
            cerr 
        int n=unlink(_path.c_str());
        if (n == 0)
        {
            cout 
            cerr 
    int wfd=open(Path,O_WRONLY);
    if(wfd
        cerr 
        cout
            cerr 
    fifo ff(Path);
    int rfd=open(Path,O_RDONLY);
    if(rfd
        cerr 
        ssize_t n=read(rfd,buffer,sizeof(buffer)-1);
        if(n0)
        {
            buffer[n]=0;
            cout 
            cout 
            cerr 

文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复:表情:
评论列表 (暂无评论,0人围观)

还没有评论,来说两句吧...

目录[+]

取消
微信二维码
微信二维码
支付宝二维码