基于匿名管道实现的进程池

这是一份基于匿名管道实现的进程池代码,供学习参考

ProcessPool.cc

cpp 复制代码
#include <iostream>
#include <string>
#include <vector>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "Task.hpp"
// master
class Channel
{
public:
    Channel(int wfd, pid_t id, const std::string &name) : _wfd(wfd), _id(id), _name(name)
    {
    }
    int Getwfd()
    {
        return _wfd;
    }
    int Getid()
    {
        return _id;
    }
    std::string &Getname()
    {
        return _name;
    }
    static int NextChannel(int num)
    {
        static int next = 0;
        int ret = next;
        next++;
        return ret % num;
    }
    void Close(){
        ::close(_wfd);
    }
    ~Channel()
    {
    
    }

private:
    int _wfd;
    pid_t _id;
    std::string _name;
};

void clean(std::vector<Channel> &Ch)
{
    for (auto &e : Ch)
    {
        close(e.Getwfd());
    }
    for (auto &e : Ch)
    {
        waitpid(e.Getid(), nullptr, 0);
    }
}

void CreateChannel(std::vector<Channel> &channels, int num, task_t task)
{
    for (int i = 0; i < num; i++)
    {
        int pipefd[2]; // 管道
        if (pipe(pipefd) < 0)
            exit(1);
        pid_t id = fork();
        if (id == 0)
        {
            if (!channels.empty())
            {
                for(auto & channel : channels) channel.Close();
            }
            // worker;
            close(pipefd[1]);   // close w   ---> r
            dup2(pipefd[0], 0); // 重定向

            task();

            exit(0);
        }
        close(pipefd[0]); // w
        channels.emplace_back(pipefd[1], id, "Channel-" + std::to_string(i));
    }
}

void SendTaskCommend(Channel &ch, int task)
{
    write(ch.Getwfd(), &task, sizeof(task));
}
void ctrlProcess(std::vector<Channel> &channels, int num, size_t times = 10)
{
    while (times--)
    {
        //      选择任务
        int task = SelectTask();
        //      选择进程
        int ch = Channel::NextChannel(num);
        //      发送
        SendTaskCommend(channels[ch], task);

        // 回收进程
        sleep(1);
    }
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << "processnum" << std::endl;
        return 1;
    }
    int num = std::stoi(argv[1]);
    std::vector<Channel> channels;

    InitTask();
    // 创建
    CreateChannel(channels, num, worker);
    // 控制,分配任务
    ctrlProcess(channels, num);
    // 回收
    clean(channels);
    return 0;
}

Task.hpp

cpp 复制代码
#pragma once

#include <iostream>
#include <functional>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>

#define TaskNum 3

// typedef void (*task_t)();
using task_t = std::function<void()>;

void Print()
{
    std::cout<< "I am Print Task" << std::endl;
}
void DownLoad()
{
    std::cout<< "I am DownLoad Task" << std::endl;
}
void Flush()
{
    std::cout<< "I am Flush Task" << std::endl;
}

task_t tasks[TaskNum];

void InitTask()
{
    srand(time(nullptr) ^ getpid());
    tasks[0] = Print;
    tasks[1] = DownLoad;
    tasks[2] = Flush;
}

void ExcuteTask(int num)
{
    tasks[num]();
}
int SelectTask()
{
    return rand() % TaskNum;
}
void worker()
{
    while(true) {
        int command = 0;

        // 卡在read了
        int i = read(0, &command, sizeof(command));
        if(i == 0) {
            std::cout<<getpid()<<"  quit "<<std::endl;
            break;
        }
        ExcuteTask(command);
    }
}
相关推荐
珠海西格电力科技1 小时前
微电网能量平衡理论的实现条件在不同场景下有哪些差异?
运维·服务器·网络·人工智能·云计算·智慧城市
A星空1231 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
释怀不想释怀1 小时前
Linux环境变量
linux·运维·服务器
zzzsde1 小时前
【Linux】进程(4):进程优先级&&调度队列
linux·运维·服务器
凡人叶枫1 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
yuanmenghao2 小时前
Linux 性能实战 | 第 7 篇 CPU 核心负载与调度器概念
linux·网络·性能优化·unix
qq_297574672 小时前
Linux 服务器 Java 开发环境搭建保姆级教程
java·linux·服务器
70asunflower2 小时前
Emulation,Simulation,Virtualization,Imitation 的区别?
linux·docker
神梦流3 小时前
ops-math 算子库的扩展能力:高精度与复数运算的硬件映射策略
服务器·数据库
神梦流3 小时前
GE 引擎的内存优化终局:静态生命周期分析指导下的内存分配与复用策略
linux·运维·服务器