可以直接进入网站的代码,cms网站后台模版,看手机的网站,创建全国文明城市手抄报前言
之前我们学习了进程通过匿名管道进行通信#xff0c;实现了两个进程的数据传输。
如果我们管理的是很多个进程#xff0c;通过管道发送指令#xff0c;因为如果管道中没有数据#xff0c;读端必须等待#xff0c;也就是被管理的进程们都在等待我发送的指令#xf…前言
之前我们学习了进程通过匿名管道进行通信实现了两个进程的数据传输。
如果我们管理的是很多个进程通过管道发送指令因为如果管道中没有数据读端必须等待也就是被管理的进程们都在等待我发送的指令那么我们可以通过特定的通讯方式对进程实施控制也就是让进程根据传输的指令去完成相应的操作。
一、进程池概念
我们知道系统资源的获取是有成本的比如我们创建进程需要花一定的时间去完成比如现在我们有一些比较重要的任务需要处理如果等待任务到来再创建进程去处理任务时间上会慢一点如果我们提前将进程创建好任务到来我们直接对进程分派任务这样就能节省时间这些提前创建好并被管理的进程有任务来就分派执行我们可以称之为进程池。
打个比方比如说你喝娃哈哈矿泉水如果你感觉到口渴了才去外面超市买娃哈哈矿泉水这样成本是不是比较高有点浪费时间但是如果你提前在家里面放上一箱娃哈哈矿泉水渴了就喝顺手就拿的事情效率就提高了这也相当于把矿泉水进行池化了。
二、进程池实现
主要思路是先创建进程和信道再发布任务最后等待关闭父进程的写进程让子进程read读到0就退出子进程。最后父进程进行资源回收。
具体可以看代码注释这里就不多BB了
ProcessPool.cpp
#include iostream
#include unistd.h
#include cassert
#include cstdio
#include cstdlib
#include vector
#include string
#include sys/types.h
#include sys/wait.h
#include Task.hpp
using namespace std;#define NUM 5class channel
{
public:channel(string name, int fd, pid_t pid): _name(name), _fd(fd), _pid(pid){}public:string _name;int _fd;pid_t _pid;
};void Work()
{while (true){int code 0;ssize_t n read(0, code, sizeof(code));if (n sizeof(code)){if (!init.Check(code)){cout 任务码不合法执行失败 endl;continue;}init.RunTask(code);}else if (n 0)break;}
}void CreatProcess(vectorchannel v)
{vectorint vfd; // 存放父进程的写信道的fdfor (int i 0; i NUM; i){// 创建管道int pipefd[2];int n pipe(pipefd);assert(n 0);// 创建进程pid_t id fork();if (id 0){perror(fork);exit(1);}// 构建信道else if (id 0){// 子进程vfd.push_back(pipefd[1]);for (auto fd : vfd) // 关闭子进程继承的写信道{close(fd);}dup2(pipefd[0], 0);Work();exit(0);}// 父进程close(pipefd[0]);v.push_back(channel(to_string(i 1) 号信道, pipefd[1], id));}
}void PrintChannel(const vectorchannel v)
{for (auto c : v){cout c._name , c._fd , c._pid endl;}
}void SendTask(const vectorchannel v, bool flag, int num 1)
{int pos 0;while (true){int task init.SelectTask(); // 选择任务pos % v.size(); // 选择信道轮流执行任务channel c v[pos];cout发送信息init.ToDesc(task)给c._namepid是c._pidendl;write(c._fd, task, sizeof(task)); // 发送任务if (!flag){if (--num 0)break;}sleep(1);}sleep(1);cout 发送任务完成 endl;
}void WaitPorcess(vectorchannel v)
{for (auto c : v){close(c._fd);pid_t rid waitpid(c._fd, nullptr, 0);cout等待子进程c._pid成功endl;}
}int main()
{vectorchannel channels;// 创建进程和信道CreatProcess(channels);// PrintChannel(channels); //打印测试// 发送任务const bool always_loop true;SendTask(channels, !always_loop, 10);// 进程等待回收WaitPorcess(channels);return 0;
}
Task.hpp
#pragma once#include iostream
#include functional
#include vector
#include ctimeusing namespace std;typedef functionvoid() task_t;void Download()
{cout 我正在下载, pid: getpid() endl;
}void Print()
{cout 我正在打印, pid: getpid() endl;
}void PlayVideo()
{cout 我正在播放, pid: getpid() endl;
}class Init
{
public:Init(){tasks.push_back(Download);tasks.push_back(Print);tasks.push_back(PlayVideo);srand(time(nullptr));}void RunTask(int code){tasks[code]();}string ToDesc(int code){switch (code){case 0:return Download;case 1:return Print;case 2:return PlayVideo;default:return Unkonw;}}bool Check(int code){return code 0 code tasks.size();}int SelectTask(){return rand() % tasks.size();}public:vectortask_t tasks;// 任务码const int download_code 0;const int print_code 1;const int PlayVideo_code 2;
};Init init; 运行结果如下。