Linux进程间通信(三).基于匿名管道的进程池的实现

一.何谓池化技术?

简单来说,就是创建并维护一个存放资源的"池子",然后一次性的申请大量的资源放在里面 ,当需要使用资源时,直接从池子里取,用完后再放回池子中,提升程序的效率。

二.理解进程池

父进程一次性创建出一批子进程和管道,这个体系就叫做进程池。

父进程每次向匿名管道写入不同内容,然后通过发送不同任务码(int code),进而操作不同的子进程完成不同的读操作。

根据写快,读慢的通信情况,只要父进程不写东西,子进程就会阻塞住,一直等待父进程。当父进程一写东西,子进程的阻塞就会被打断,进而去读取;父进程通过对写操作的继续与停止,进而导致子进程阻塞的停止与继续 ,这样的操作叫做唤醒和暂停子进程。

三.实现

用下标_next++即可完成管道的轮询,用整数code的地址作为buffer,保证一次读/写四个字节(code)。

复制代码
#pragma once
#include<iostream>
#include<vector>
#include<unistd.h>
#include<cstdlib>
#include<sys/wait.h>
class Channel
{//管道类,先描述
public:
    Channel(int fd,pid_t pid)
    :_wfd(fd)
    ,subid(pid)
    {
        _name = "channel-" + std::to_string(_wfd) + "-" + std::to_string(pid);
    }
    ~Channel()
    {}
    void Send(int code)
    {
        int n = write(_wfd,&code,sizeof(code));
        (void)n;//使用一下,防止警告  
    }
    int Fd(){return _wfd;}
    pid_t SubId(){return subid;}
    std::string Name(){return _name;}
    void Close()
    {
        close(_wfd);
    }
    void Wait()
    {
        pid_t rid = waitpid(subid,nullptr,0);
        (void)rid;
    }
private:
    int _wfd;
    pid_t subid;
    std::string _name;
};
//管理管道的类,再组织
class ChannelManager
{
public:
    ChannelManager()
    :_next(0)
    {}
    ~ChannelManager()
    {}
    void Build(int wfd,pid_t subid)
    {
        Channel c(wfd,subid);
        _channels.push_back(c);
    }
    void PrintChannel()
    {
        for(auto &channel: _channels)
        {
            std::cout << channel.Name() << std::endl;
        }
    }
    Channel& Selct()
    {
        auto &c =  _channels[_next];
        ++_next;
        _next %= _channels.size();//防止越界
        return c;
    }
    void StopProcess()
    {
        for(auto &channel:_channels)
        {
            channel.Close();
        }
    }
    void WaitProcess()
    {
        for(auto &channel:_channels)
        {
            channel.Wait();
        }
    }
    private:
    std::vector<Channel> _channels;
    int _next;
};
const int gdefaultnum = 5;//创建子进程的数量
class ProcessPool
{
public:
    ProcessPool(int num)
    :_Process_nms(num)
    {}
    ~ProcessPool()
    {}
    void Work(int rfd)
    {
       while(true)
       {
           int code = 0;
            ssize_t n = read(rfd,&code,sizeof(code));
            if(n > 0)
            {//读取成功
                //必须按规范,一次读取四个字节的数据。
                if(n != sizeof(code))
                {
                    continue;
                }
                else
                {
                    std::cout << "子进程[" << getpid() << "]收到一个任务码:" << code << std::endl;
                }
            }
            else if(n == 0)
            {//父进程写端关闭,子进程退出
                std::cout << "子进程退出" << std::endl;
                break;
            }
            else
            {//读取失败
                std::cout << "读取失败" << std::endl;
                break;
            }
       }
    }
    bool Create()
    {
        for(int i = 0;i < _Process_nms;++i)
        {
            //1.创建管道
        int pfd[2] = {0};
        int n = pipe(pfd);
        if(n < 0) return false;
        //2.创建进程
        pid_t subid = fork();
        if(subid < 0) return false;
        else if(subid == 0)//父写子读
        {//子进程
            //3.关闭不需要的fd
            close(pfd[1]);
            //4.读取
            Work(pfd[0]);
            //5.关所有的fd
            close(pfd[0]);
            exit(0);
        }
        else
        {//父进程
            //3.关闭不需要的fd
            close(pfd[0]);
            _cm.Build(pfd[1],subid);
        }
        }
        return true;
    }
    void debug()
    {
       _cm.PrintChannel();
    }
    void TaskPush(int task_code)
    {
        //1.选择一个信道,负载均衡的选择一个子进程,完成任务。
        //这里选择轮询来实现均衡。
        auto &c = _cm.Selct();//用下标_next来控制轮询。
        //2.发送(写入)数据
        c.Send(task_code);
        std::cout << "发送一个任务码:" << task_code << std::endl;
    }
    void Stop()
    {
        //关闭父进程的所有wfd,就可以停掉所有子进程
        _cm.StopProcess();
        //回收子进程
        _cm.WaitProcess();
    }
    private:
    ChannelManager _cm;
    int _Process_nms;//进程数量
};
相关推荐
Decay__17 小时前
PostgreSQL 数据库全栈运维实战系列 — 合集索引(24 篇)
运维·数据库·sql·mysql·架构
戊本菌18 小时前
进程(三)runqueue 与 Linux 的 O(1) 调度
linux·运维·服务器
光电笑映19 小时前
TCP 为什么可靠?从三次握手、滑动窗口到拥塞控制全面拆解(下)
linux·网络·网络协议·tcp
yz_aiks20 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
csdn_aspnet1 天前
在 Docker 容器中本地运行 SQL Server
运维·docker·容器·sqlserver
叼烟扛炮1 天前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
hi_ro_a1 天前
Ext系列⽂件系统详解
linux·服务器·操作系统
Z21-X1 天前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
你住过的屋檐1 天前
【SeaweedFS】Linux上搭建SeaweedFS文件存储服务完整教程
linux·运维·服务器
Decay__1 天前
PostgreSQL 体系架构全解 —— 进程模型、共享内存、MVCC 与 WAL 预写日志深入
运维·数据库·sql·mysql·架构