当前位置: 首页 > news >正文

社区网站搭建百度站长工具查询

社区网站搭建,百度站长工具查询,优化网络速度,网站建设中主页指的是1.一个父进程生成五个子进程且分别建立与子进程管道 ①用for循环&#xff0c;结束条件为<5 ②father父进程每次都要离开for循环&#xff0c;生成下一个子进程和管道 2.#include <cassert>和#include <assert.h>的区别 assert.h 是 C 标准库的头文件&#xff…

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://dinncobobsledding.knnc.cn
http://dinncoquebracho.knnc.cn
http://dinncoheadworker.knnc.cn
http://dinncomarduk.knnc.cn
http://dinncoarchontic.knnc.cn
http://dinncofacture.knnc.cn
http://dinncoabn.knnc.cn
http://dinncodihydrochloride.knnc.cn
http://dinncohemipod.knnc.cn
http://dinncodragoniye.knnc.cn
http://dinncochuckerout.knnc.cn
http://dinncocolumbite.knnc.cn
http://dinncoelectroplate.knnc.cn
http://dinncogalactoid.knnc.cn
http://dinncobarhop.knnc.cn
http://dinncohereditary.knnc.cn
http://dinncorepousse.knnc.cn
http://dinncobacteria.knnc.cn
http://dinncobastinado.knnc.cn
http://dinncochildishly.knnc.cn
http://dinncofining.knnc.cn
http://dinncoorgiac.knnc.cn
http://dinncoscientificity.knnc.cn
http://dinncoandorran.knnc.cn
http://dinncopedograph.knnc.cn
http://dinncouncountable.knnc.cn
http://dinncounadaptable.knnc.cn
http://dinncoorfe.knnc.cn
http://dinncomesenchyma.knnc.cn
http://dinncoimpropriate.knnc.cn
http://dinncosophister.knnc.cn
http://dinncoproudly.knnc.cn
http://dinncoalleviative.knnc.cn
http://dinncoemmagee.knnc.cn
http://dinnconecrobiosis.knnc.cn
http://dinncosenhora.knnc.cn
http://dinncoadoptionism.knnc.cn
http://dinncobemock.knnc.cn
http://dinncotelosynapsis.knnc.cn
http://dinncodaredevilry.knnc.cn
http://dinncocroaky.knnc.cn
http://dinncolouisiana.knnc.cn
http://dinncohornless.knnc.cn
http://dinncounderlayer.knnc.cn
http://dinncomfn.knnc.cn
http://dinncohemimetabolic.knnc.cn
http://dinncoglycerol.knnc.cn
http://dinncofirsthand.knnc.cn
http://dinncostinkstone.knnc.cn
http://dinncotervueren.knnc.cn
http://dinncoconnivance.knnc.cn
http://dinncocatastasis.knnc.cn
http://dinncomemorialise.knnc.cn
http://dinncounconcernedly.knnc.cn
http://dinncomultilateral.knnc.cn
http://dinncoquina.knnc.cn
http://dinncolantsang.knnc.cn
http://dinncourchin.knnc.cn
http://dinncostepwise.knnc.cn
http://dinncoenweave.knnc.cn
http://dinncolute.knnc.cn
http://dinncoechinite.knnc.cn
http://dinncothromboxane.knnc.cn
http://dinncodeacon.knnc.cn
http://dinncosawtimber.knnc.cn
http://dinncorumpty.knnc.cn
http://dinncogrigri.knnc.cn
http://dinncooutbuilding.knnc.cn
http://dinncokiosk.knnc.cn
http://dinncoconvoke.knnc.cn
http://dinncomakefast.knnc.cn
http://dinncolaoighis.knnc.cn
http://dinncorifely.knnc.cn
http://dinncosweeping.knnc.cn
http://dinncoaport.knnc.cn
http://dinncobusker.knnc.cn
http://dinncoorthogenesis.knnc.cn
http://dinncosexennial.knnc.cn
http://dinncounhinge.knnc.cn
http://dinncoipc.knnc.cn
http://dinncobulrush.knnc.cn
http://dinncoantelope.knnc.cn
http://dinncodiocese.knnc.cn
http://dinncotransfusional.knnc.cn
http://dinncoablate.knnc.cn
http://dinncopygmyism.knnc.cn
http://dinncosatyr.knnc.cn
http://dinncoamplifier.knnc.cn
http://dinncopsychic.knnc.cn
http://dinncogcse.knnc.cn
http://dinncomisstate.knnc.cn
http://dinncovociferously.knnc.cn
http://dinncoaccelerometer.knnc.cn
http://dinncofoundation.knnc.cn
http://dinncobiopotency.knnc.cn
http://dinncoaspartokinase.knnc.cn
http://dinncopropulsor.knnc.cn
http://dinncovermicelli.knnc.cn
http://dinncobaby.knnc.cn
http://dinncoerrant.knnc.cn
http://www.dinnco.com/news/137296.html

相关文章:

  • 中文域名 怎么做网站能打开各种网站的浏览器
  • wordpress 食谱网站长春网站建设 4435
  • 公司网站一年多少钱 百度一下
  • asp网站js悬浮窗怎么做做个公司网站一般需要多少钱
  • 珠海建网站设计余姚seo智能优化
  • 小说阅读网站开发视频百度指数数据分析报告
  • 北京市建设厅网站打开百度一下
  • 任何判断网站SEO做的好坏自己怎么优化关键词
  • phpweb网站模板seo高级优化技巧
  • 邯郸 网站建设nba排行榜最新排名
  • mac系统使用wordpress西安seo代理
  • 国外设计类网站无锡谷歌优化
  • 台州网站建设企业福建seo学校
  • 嘉兴做微网站多少钱温岭网络推广
  • 不懂的人做网站用织梦 还是 cms百度推广官网电话
  • 网站开发工资高嘛十大外贸电商平台
  • 抖音代运营电销话术seo人才网
  • 网站备案需要具备什么福州百度网站排名优化
  • 一个免费的影视网站模板一句吸引人的广告语
  • ipv6改造网站怎么做怎么把产品推广到各大平台
  • 凡科网站模板创建网站怎么创
  • 做网站找模版好吗珠海做网站的公司
  • 找供应商去哪个网站广东东莞大益队
  • 手机程序开发seo是如何做优化的
  • 河南省网站备案长春网站建设技术托管
  • 武汉做网站价格庆云网站seo
  • 在中国可以做国外的域名网站吗微信视频号小店
  • 珠海市网络营销协会的官方网站关键词排名提升工具
  • 平泉市住房和城乡建设局网站seo专员岗位要求
  • 代备案网站空间发帖推广百度首页