Linux--进程池

news/2024/10/23 5:38:51/

1.一个父进程生成五个子进程且分别建立与子进程管道

①用for循环,结束条件为<5 ②father父进程每次都要离开for循环,生成下一个子进程和管道

2.#include <cassert>和#include <assert.h>的区别

assert.h 是 C 标准库的头文件,cassert 是 C++ 标准库的头文件

3.子进程进行读取

那咱就关闭写端:close(pipefd[1]),保留读端

4.父进程进行写入

那咱就关闭读端:close(pipefd[0]),保留写端

5.让父进程实现:选择让哪个进程执行哪个命令

使用pair<pid_t,int>创建键值对,实现一一对应。

vector<pair<pid_t,int> > slots;创建各个进程与命令的对应表

slots.push_back(pair<pid_t,int>(id,pipefd[1]));将进程与命令建立连接

6.让子进程等待命令

waitCommand(pipefd[0]);//如果对方不发,我们就阻塞

int waitCommand(int waitFd)

{

    uint32_t command = 0;

    ssize_t s=read(waitFd,&command,sizeof(command));

    assert(s == sizeof(uint32_t));

    return command;

}

7.uint32_t对应几个字节

4个

8.typedef function<void()> func,以及它的等价写法

typedef function<void()> func表示定义函数对象,等价写法是using func=function<void()>

代码实现:

Makefile:

ProcessPool:ProcessPool.ccg++ -o $@ $^ -std=c++11 #-DDEBUG
.PHONY:clean
clean:rm -f ProcessPool

Task.hpp

#pragma once#include <iostream>
#include <string>
#include <unistd.h>
#include <functional>
#include <vector>
#include <unordered_map>using namespace std;
typedef function<void()> func;//等价于  using func=function<void()>vector<func> callbacks;
unordered_map<int,string> desc;
void readMySQL()
{cout<<"process["<<getpid()<<"]执行访问数据库的任务" <<endl;
}void execuleUrl()
{cout<<"process["<<getpid()<<"]执行url解析" <<endl;
}void cal()
{cout<<"process["<<getpid()<<"]执行加密任务" <<endl;
}void save()
{cout<<"process["<<getpid()<<"]执行数据持久化任务" <<endl;
}void load()
{desc.insert({callbacks.size(),"readMySQL:读取数据库"});callbacks.push_back(readMySQL);desc.insert({callbacks.size(),"execuleUrl:进行url解析"});callbacks.push_back(execuleUrl);desc.insert({callbacks.size(),"cal:进行加密计算"});callbacks.push_back(cal);desc.insert({callbacks.size(),"save:进行数据的文件保存"});callbacks.push_back(save);}void showHandler()
{for(const auto &iter:desc){cout<<iter.first<<"\t"<<iter.second<<endl;}
}int handlerSize()
{return callbacks.size();
}

processPool.cc

#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <cassert>
#include <vector>
#include "Task.hpp"
#include <cstdlib>
#include <ctime>#define PROCESS_NUM 5using namespace std;int waitCommand(int waitFd,bool &quit)
{uint32_t command = 0;ssize_t s = read(waitFd, &command, sizeof(command));if(s == 0){quit=true;return -1;}assert(s == sizeof(uint32_t));return command;
}void sendAndWakeup(pid_t who, int fd, uint32_t command)
{write(fd,&command,sizeof(command));cout<<"call process"<<who<<"execute"<<desc[command]<<"through"<<fd<<endl;
}int main()
{load();// vector内放置pid:pipefd键值对vector<pair<pid_t, int>> slots;// 先创建多个进程for (int i = 0; i < PROCESS_NUM; i++){// 创建管道int pipefd[2] = {0};int n = pipe(pipefd);assert(n > 0);(void)n;pid_t id = fork();assert(id != -1);// 子进程我们让它进行读取if (id == 0){// 关闭写端close(pipefd[1]);// childwhile (true){// pipefd[0]// 等命令bool quit=false;int command = waitCommand(pipefd[0],quit); // 如果对方不发,我们就阻塞if(quit) break;// 执行对应的命令if (command >= 0 && command < handlerSize()){callbacks[command]();}else{cout << "非法command:" << command << endl;}}exit(1);}// father,进行写入,关闭读端close(pipefd[0]);slots.push_back(pair<pid_t, int>(id, pipefd[1]));}// 父进程派发任务srand(unsigned long)time(nullptr)^getpid()^23323123123L);//让数据更随机while (true){int select;int command;cout << "##############################################" << endl;cout << "#    1.show functions    2.send command      #" << endl;cout << "##############################################" << endl;cout << "Please Select>";cin >> select;if (select == 1)showHandler();else if (select == 2){cout << "Enter Your Command>";// 选择任务cin >> command;// 选择进程int choice = rand() % slots.size();// 把任务给指定的进程sendAndWakeup(slots[choice].first, slots[choice].second, command);}}// 关闭fd,所有的子进程都会退出for(const auto& slot:slots){close(slot.second);}//回收所有的子进程信息for(const auto & slot:slots){waitpid(slot.first,nullptr,0);}}


http://www.ppmy.cn/news/982086.html

相关文章

gin框架内容(二)

上一篇过于gin的内容 https://mp.csdn.net/mp_blog/creation/editor/131953861 CSDNhttps://mp.csdn.net/mp_blog/creation/editor/131953861 一、路由组 为了管理具有相同前缀的URL, 将拥有URL共同前缀的路由划分为一组 为了代码的阅读性&#xff0c;使用{}包裹相同组的路由…

【算法题】2681. 英雄的力量

题目&#xff1a; 给你一个下标从 0 开始的整数数组 nums &#xff0c;它表示英雄的能力值。如果我们选出一部分英雄&#xff0c;这组英雄的 力量 定义为&#xff1a; i0 &#xff0c;i1 &#xff0c;… ik 表示这组英雄在数组中的下标。那么这组英雄的力量为 max(nums[i0],n…

编写LED驱动,创建三个设备文件,每个设备文件绑定一个设备

驱动代码&#xff1a; #include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/io.h> #include <linux/device.h> #include <linux/uaccess.h> #include <linux/slab.h> #include <linux/cd…

[ 容器 ] Harbor 私有仓库的部署与管理

目录 一、什么是Harbor二、Harbor的特性三、Harbor的构成四、Harbor 部署五、关于 Harbor.cfg 配置文件中有两类参数&#xff1a;所需参数和可选参数六、维护管理Harbor 一、什么是Harbor Harbor 是 VMware 公司开源的企业级 Docker Registry 项目&#xff0c;其目标是帮助用户…

redis数据库

目录 1.关系型数据库与非关系型数据库 关系型数据库 非关系型数据库 区别 2.redis 3.安装redis 1.关系型数据库与非关系型数据库 关系型数据库 关系型数据库是一个结构化的数据库&#xff0c;创建在关系模型&#xff08;二维表格模型&#xff09;基础上&#xff0c;一般面向…

汽车分析,随时间变化的燃油效率

简述 今天我们来分析一个汽车数据。 数据集由以下列组成&#xff1a; 名称&#xff1a;每辆汽车的唯一标识符。MPG&#xff1a;燃油效率&#xff0c;以英里/加仑为单位。气缸数&#xff1a;发动机中的气缸数。排量&#xff1a;发动机排量&#xff0c;表示其大小或容量。马力&…

Python Web 开发及 Django 总结

title: Python Web 开发及 Django 总结 date: 2023-07-24 17:26:26 tags: PythonWeb categories:Python cover: https://cover.png feature: false Python 基础部分见&#xff1a;Python 基础总结 1. 创建项目 1.1 命令行 1、下载安装 Django 在终端输入 pip install djan…

JVM详解(超详细)

目录 JVM 的简介 JVM 执行流程 JVM 运行时数据区 由五部分组成 JVM 的类加载机制 类加载的过程(五个) 双亲委派模型 类加载器 双亲委派模型的优点 JVM 中的垃圾回收策略 GC GC 中主要分成两个阶段 死亡对象的判断算法 引用计数算法 可达性分析算法 垃圾回收算…