进程通信(进程池的模拟实现) read write函数复习 Linux ─── 第23课

目录

进程池(process pool)

第一步: 创建并初始化processpool

第二步:主进程对子进程派发任务

补充:

第三步: 子进程执行完退出进程池

回收子进程

进程池的实现

Channel.hpp

ProcessPool.hpp

Task.hpp

main.cc

makefile


匿名管道的应用: 进程池

进程池(process pool)

先把进程创建出来,需要什么任务 ,派发什么任务.

让一个进程(master进程) ,给其他进程(work进程)派发任务 

下面实现process pool

第一步: 创建并初始化processpool

master要管理所有的管道 创建channel

创建管道 ,创建子进程 ,用vector管理全部的channel 

#include<iostream>
#include<unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include<vector>
#include<functional>//执行任一方法

//typedef std::function<void()> work_t;
using work_t =std::function<void()>;//定义了函数对象类型

enum 
{
    OK=0,
    UsageError,
    PipeError,
    ForkError,
    CloseError,
    Dup2Error
};


//先描述
class channel
{
public:
    channel(int wfd ,pid_t who):_wfd(wfd),_who(who)
    {
        _name ="channel-"+std::to_string(wfd)+"->"+ std::to_string(who);
    }

    ~channel()
    {

    }
    std::string Name()
    {
        return _name;
    }
private:
    int _wfd;
    std::string _name;//channel-3->203444
    pid_t _who;
};


void Worker()
{
    //read ->stdin
}
void Download()
{
    //read ->stdin
}


//channels是输出型参数
//work_t work 回调方法   使创建子进程与让子进程执行任务是解耦的
int InitProcessPool(std::vector<channel>& channels ,int num ,work_t work)
{
        //创建指定进程个数
        for(int i=0 ;i <num; i++)
        {   //管道
            int Pipefds[2]={0};
            int n =::pipe(Pipefds);
            if(n< 0) return PipeError;
    
            //子进程
            pid_t id =::fork();
            if(id == 0)
            {//child 读
                int close_ret = ::close(Pipefds[1]);//关闭写通道
                if(close_ret < 0) return CloseError;
                int dup2_ret =::dup2(Pipefds[0] ,0);//重定向,让子进程从标准输入中获取要执行的任务。不再使用Pipefds[0]了      
                if(dup2_ret< 0) return Dup2Error;
                work();
    
                ::close(Pipefds[0]);
                //sleep(10);  DebugPrint用
                ::exit(0);
            }
            else if(id < 0)
            {
                return ForkError;
            }
            else
            {//parent
                int close_ret = ::close(Pipefds[0]);//关闭读通道
                if(close_ret < 0) return CloseError;

                channels.emplace_back(Pipefds[1] , id);
    
            }
        }
        return OK;
    
}
void DebugProcesspool(std::vector<channel>& channels)
{
    for( auto &c :channels)
    {
        std::cout<< c.Name() <<std::endl;
    }

}



void Usage(std::string process)
{
    std::cout<<"Uasge:"<< process <<"process-num"<<std::endl;
}

//我们自己是master
int main(int argc ,char* argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return UsageError;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归—码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值