Linux 进程池小程序

main.cpp

cpp 复制代码
#include"Task.hpp"
#include<iostream>
#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
const int num=10;
vector<task_t> tasks;
class channel
{
public:
    int _cmdfd;           //发送任务的文件符
    pid_t _childpid;      //子进程的pid
    string _process_name;  //子进程的名字
public:
    channel(int cmdfd,pid_t childpid,string process_name)
    :_cmdfd(cmdfd)
    ,_childpid(_childpid)
    ,_process_name(process_name)
    {}
};

void child()//子进程接收任务并执行任务
{
    while(true)
    {
        int taskcode=0;
        ssize_t ret=read(0,&taskcode,sizeof(int));//读取任务编号
        if(ret==sizeof(int))
        {
            cout<<"child alread get a task: "<<getpid()<<" : taskcode : "<< taskcode<<endl;
            tasks[taskcode]();//根据任务编号执行任务
        }
        if(ret==0)
        {
            break;
        }
    }
}
void Initprocesspool(vector<channel> *channels)
{
    vector<int> v;//存储父进程的写端
    for(int i=0;i<num;i++)
    {
        int pipefd[2];
        int ret=pipe(pipefd);
        pid_t id=fork();
        if(id==0)
        {
            //child
            for(auto &it:v) close(it);//当创建子进程时,此时会继承父进程和上一个子进程的管道写端,需要关闭对上一个子进程的写端
            close(pipefd[1]);//关闭写端
            dup2(pipefd[0],0);//将0号文件描述符重定向到管道文件,从0号文件描述符写的信息会被重定向到管道文件
            child();//子进程去执行任务
            close(pipefd[0]);//关闭读端
            exit(0);
        }
        close(pipefd[0]);//关闭读端
        string name="process"+to_string(i);
        channels->push_back(channel(pipefd[1],id,name));//存储子进程的信息
        v.push_back(pipefd[1]);//将父进程的写端插入
    }
}
void Debug(vector<channel> &channels)//打印所有子进程的信息
{
    for(auto it:channels)
    {
        cout<<it._childpid<<" "<<it._cmdfd<<" "<<it._process_name<<" "<<endl;
    }
}
void menu()
{
    cout<<"###############################################"<<endl;
    cout<<"1. 打篮球                               2.踢足球"<<endl;
    cout<<"3. 打羽毛球                            4.打乒乓球"<<endl;
    cout<<"                                       0.退出   "<<endl;
    cout<<"###############################################"<<endl;
}
void ctrlchild(vector<channel>&channels)
{
    while(true)
    {
        int select=0;
        menu();
        cout<<"please choose: ";
        cin>>select;//输入选择的任务序号
        int processpos=0;//进程编号
        int taskcode=select-1;//选择的序号减一才是存储任务的指针数组的下标
        if(select>0&&select<=4)
        {
            cout<<"father say "<<"taskcode: "
            <<taskcode<<"already send to: "<<channels[processpos]._childpid
            <<"process name: "<<channels[processpos]._process_name<<endl;
            ssize_t n=write(channels[processpos]._cmdfd,&taskcode,sizeof(taskcode));//将存储任务的指针数组的下标写入指定的管道文件
        }
        else
        {
            break;
        }
    }
}
void quitprocess(vector<channel>&channels)
{
    for(const auto &it:channels)
    {
        close(it._cmdfd);//关闭所有子进程的读端
        waitpid(it._childpid,nullptr,0);//回收子进程
    }
}
int main()
{
    Loadtask(&tasks);
    vector<channel> channels;
    //初始化
    srand(time(nullptr));
    Initprocesspool(&channels);
    Debug(channels);
    //开始控制子进程
    ctrlchild(channels);
    quitprocess(channels);
    return 0;
}

Task.hpp

cpp 复制代码
#pragma once
#include<iostream>
#include<vector>
using namespace std;
typedef void (*task_t)();
void task1()
{
    cout<<"打篮球"<<endl;
}
void task2()
{
    cout<<"踢足球"<<endl;
}
void task3()
{
    cout<<"打羽毛球"<<endl;
}
void task4()
{
    cout<<"打乒乓球"<<endl;
}
void Loadtask(vector<task_t>*tasks)
{
    tasks->push_back(task1);
    tasks->push_back(task2);
    tasks->push_back(task3);
    tasks->push_back(task4);
}

运行结果:

相关推荐
lzx_0022 小时前
Linux 进程(二) 深入理解进程
linux
沫璃染墨3 小时前
从零入门计算机网络系列(十二):HTTP长连接与会话管理详解——从Connection到Cookie与Session
linux·服务器·c++·tcp/ip·ubuntu·http·交互
温暖小土3 小时前
在 CentOS 上安装 Redis Stack
linux·redis·centos
xiaoye-duck4 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(下):流式响应实现和测试
开发语言·网络·c++·语言模型
GeW5 小时前
国产化替代进入深水区之后Linux 与数据库为什么必须走向重建
linux
旖旎夜光5 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习
小超人诞生日记6 小时前
C++ 智能指针的使用及其原理详解
c++·算法
akinggw7 小时前
libagentcore - 一个基于c++的AI多agent系统升级到2.0版本
开发语言·jvm·c++
繁星蓝雨8 小时前
C++设计原理———多重继承(虚基类、菱形继承、委托、重命名、基类和成员初始化)
c++·委托·多重继承·mi·显式初始化列·虚基类·对象布局
女神下凡8 小时前
芯参谋(85):Linux_PPI_NAND_驱动开发指南
linux·运维·arm开发·驱动开发·单片机·嵌入式硬件·设计规范