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

罗湖外贸网站建设软件外包网站

罗湖外贸网站建设,软件外包网站,wordpress导出出错,网站建设课程的感受📟作者主页:慢热的陕西人 🌴专栏链接:Linux 📣欢迎各位大佬👍点赞🔥关注🚓收藏,🍉留言 本博客主要内容管道后续的完善,以及解决管道继承多个文件描…

📟作者主页:慢热的陕西人

🌴专栏链接:Linux

📣欢迎各位大佬👍点赞🔥关注🚓收藏,🍉留言

本博客主要内容管道后续的完善,以及解决管道继承多个文件描述符的问题

文章目录

    • 1.管道程序的再优化
      • 1.1void ctrlprocess函数
      • 1.2EndPoint类
      • 1.3RecailmTask函数
      • 1.4子进程继承父进程文件描述符问题的解决

1.管道程序的再优化

1.1void ctrlprocess函数

分三步:

  • 确定任务
  • 确定执行任务的子进程.轮询式的
  • 执行任务
void ctrlprocess(const vector<EndPoint>& end_points)
{// 2.写成自动化,也可以搞成交互式的int cnt = 0;while (true){//1.确定任务int command = ShowBoard();if(command == 3) break;if(command < 0 || command > 2) continue;//2.确定执行任务的子进程.轮询式的int child = cnt++;cnt %= end_points.size();cout << "选择了进程:" << end_points[child].name() <<"| 处理任务:" << command << endl;//3.执行任务write(end_points[child]._write_fd, &command, sizeof(command));sleep(1);}
}

1.2EndPoint类

  • 增加static成员number:用于统计子进程个数
  • 增加string processname:用于存储进程的名字
  • 增加name函数:用于打印子进程的名字
// 先描述
class EndPoint
{
private:static int number;
public:pid_t _child;  // 子进程pidint _write_fd; // 对应的文件描述符string processname;
public:// 构造EndPoint(int id, int fd): _child(id), _write_fd(fd){char namebuffer[64];snprintf(namebuffer, sizeof(namebuffer), "process-%d[%d:%d]", number++, id, fd);processname = namebuffer;}string name() const {return processname;}// 析构~EndPoint(){}
};

1.3RecailmTask函数

用于子进程的回收:

  • ①关闭写描述符:根据前面讲的父进程关闭了管道对应的写描述符之后,子进程也就退出了
  • ②回收子进程:waitpid对应的函数进行回收!

方案一:两种操作分开执行

void RecailmTask(const vector<EndPoint>& end_points)
{//1.关闭写描述符for(int i = 0; i < end_points.size(); ++i) close(end_points[i]._write_fd);cout << "父进程让所有的进程退出了" << endl;sleep(5);//2.回收子进程for(int i = 0; i < end_points.size(); ++i) waitpid(end_points[i]._child, nullptr, 0);sleep(5);
}

运行结果:

子进程在父进程的操控下,正常退出了

image-20231126215109563

1.4子进程继承父进程文件描述符问题的解决

但是当我们把这两个过程合并的时候问题出现了:

然后就卡住了

void RecailmTask(const vector<EndPoint>& end_points)
{//1.关闭写描述符for(int i = 0; i < end_points.size(); ++i) {close(end_points[i]._write_fd);waitpid(end_points[i]._child, nullptr, 0);}cout << "父进程让所有的进程退出了" << endl;sleep(5);
}

image-20231126220145423

这是为什么呢?其实我们想一想,父进程在创建子进程的时候子进程也会把父进程的文件描述符也会拷贝一份

image-20231126221535592

所以除了第一个子进程当我们父进程关闭对应的写端的时候子进程不会关闭,原因是其他的子进程也继承了对应的写端的文件描述符。所以写端并没有完全关闭,所以这时候父进程去等待回收子进程的时候就会一直在等待,造成了程序卡住的状态!那么我们怎么解决呢?

方案一:反着顺序关闭写端

void RecailmTask(const vector<EndPoint>& end_points)
{//1.关闭写描述符for(int end = end_points.size() - 1; end >= 0; --end) {close(end_points[end]._write_fd);waitpid(end_points[end]._child, nullptr, 0);}cout << "父进程让所有的进程退出了" << endl;sleep(5);
}

image-20231126222855900

可是我们这种办法只是解决了表象,我们没有解决的根本的情况,我们只是让程序可以正常的关闭,但是子进程的那种继承父进程管道的文件描述符的问题还是没有解决,并且这也是有一定不安全的情况在里面的,因为管道不是一对一的情况了,变成了多对一的情况,可能会造成其他的子进程向其他管道中错误写入的问题:

所以我们我解决这个问题,这个问题我们应该在创建子进程管道的时候就解决好!

思路:我们每创建一个子进程,把其对应的文件描述符存储在一个vector内部,然后再创建第二个子进程的时候,遍历vector的时候,将他们依次关掉即可!

void creatProcesses(vector<EndPoint> &end_points)
{//用于存储文件描述符vector<int>fds;// 1.先进行构建控制结构,父进程写,子进程读for (int i = 0; i < gnum; ++i){// 1.1创建管道int pipefd[2] = {0};int ret = pipe(pipefd);assert(ret == 0); // 0正常 -1不正常(void)ret;// 1.2创建进程pid_t id = fork();assert(id != -1);if (id == 0){//关闭不必要的描述符for(auto& fd : fds) close(fd);// 子进程// 1.3关闭不要的fdclose(pipefd[1]);// 我们期望,所有的子进程读取“指令”的时候,都从标准输入读取// 1.3.1所以我们进行输入重定向dup2(pipefd[0], 0);// 1.3.2子进程开始等待获取命令WaitCommend();close(pipefd[0]);exit(0);}// 父进程// 1.3关闭不要的fdclose(pipefd[0]);// 1.4将新的子进程和他的管道写端构建对象。end_points.push_back(EndPoint(id, pipefd[1]));fds.push_back(pipefd[1]);}
}

运行结果:

image-20231126224519572


到这本篇博客的内容就到此结束了。
如果觉得本篇博客内容对你有所帮助的话,可以点赞,收藏,顺便关注一下!
如果文章内容有错误,欢迎在评论区指正

在这里插入图片描述


文章转载自:
http://dinncokidnap.tpps.cn
http://dinncostreptobacillus.tpps.cn
http://dinncorachel.tpps.cn
http://dinncoepitasis.tpps.cn
http://dinncowaterblink.tpps.cn
http://dinncounderlying.tpps.cn
http://dinncoglobeflower.tpps.cn
http://dinncocalif.tpps.cn
http://dinncoheavyset.tpps.cn
http://dinncoupward.tpps.cn
http://dinncoblow.tpps.cn
http://dinncospanking.tpps.cn
http://dinncobathos.tpps.cn
http://dinncocoldblooedness.tpps.cn
http://dinncocrooknecked.tpps.cn
http://dinncocist.tpps.cn
http://dinncowingover.tpps.cn
http://dinncocca.tpps.cn
http://dinncolaborism.tpps.cn
http://dinncolayperson.tpps.cn
http://dinncoloon.tpps.cn
http://dinncoaphasic.tpps.cn
http://dinncoafrikanerdom.tpps.cn
http://dinncoczechoslovakia.tpps.cn
http://dinncocalling.tpps.cn
http://dinncobacteriuria.tpps.cn
http://dinncopandemonium.tpps.cn
http://dinncoabm.tpps.cn
http://dinncogallant.tpps.cn
http://dinncoedta.tpps.cn
http://dinncolampblack.tpps.cn
http://dinncoanglo.tpps.cn
http://dinncohaven.tpps.cn
http://dinncowestpolitik.tpps.cn
http://dinncovoetganger.tpps.cn
http://dinncoameban.tpps.cn
http://dinncomastication.tpps.cn
http://dinncoyond.tpps.cn
http://dinncoautonetics.tpps.cn
http://dinncolipoidal.tpps.cn
http://dinncoepode.tpps.cn
http://dinncovalhalla.tpps.cn
http://dinncoaeration.tpps.cn
http://dinncocentre.tpps.cn
http://dinncophylloxera.tpps.cn
http://dinncoanshan.tpps.cn
http://dinncoacutely.tpps.cn
http://dinncowhiggery.tpps.cn
http://dinncozoftig.tpps.cn
http://dinncohousebreaking.tpps.cn
http://dinncoserotype.tpps.cn
http://dinncotractate.tpps.cn
http://dinncobitch.tpps.cn
http://dinncograndiose.tpps.cn
http://dinncopraefect.tpps.cn
http://dinncoeunomia.tpps.cn
http://dinncoshadowboxing.tpps.cn
http://dinncotrout.tpps.cn
http://dinncodough.tpps.cn
http://dinncosyphilous.tpps.cn
http://dinncoacetobacter.tpps.cn
http://dinncoandrosphinx.tpps.cn
http://dinncopreallotment.tpps.cn
http://dinncoblunderhead.tpps.cn
http://dinncoladyfinger.tpps.cn
http://dinncoautonomist.tpps.cn
http://dinncononagricultural.tpps.cn
http://dinncolightface.tpps.cn
http://dinncoheeltap.tpps.cn
http://dinncowhistleable.tpps.cn
http://dinncosynergic.tpps.cn
http://dinnconyon.tpps.cn
http://dinncoithun.tpps.cn
http://dinncogritstone.tpps.cn
http://dinncographospasm.tpps.cn
http://dinncoantivivisection.tpps.cn
http://dinncoepencephalon.tpps.cn
http://dinncosuperstitiously.tpps.cn
http://dinncojunc.tpps.cn
http://dinncoprotreptic.tpps.cn
http://dinncosuperexpress.tpps.cn
http://dinncopilothouse.tpps.cn
http://dinncosylvester.tpps.cn
http://dinncoregale.tpps.cn
http://dinncoinsufflation.tpps.cn
http://dinncoseti.tpps.cn
http://dinncowidower.tpps.cn
http://dinncoelmwood.tpps.cn
http://dinncoautoptic.tpps.cn
http://dinncorawinsonde.tpps.cn
http://dinncojapheth.tpps.cn
http://dinncotabby.tpps.cn
http://dinncoparawing.tpps.cn
http://dinncoofay.tpps.cn
http://dinncoseamanship.tpps.cn
http://dinncoeuropeanly.tpps.cn
http://dinncosubstantivize.tpps.cn
http://dinncocornered.tpps.cn
http://dinncoexplore.tpps.cn
http://dinncosurface.tpps.cn
http://www.dinnco.com/news/137132.html

相关文章:

  • 手机网站列表页源码关键词查询优化
  • 网站如何做域名解析产品seo优化
  • 微能力者恶魔网站谁做的企业培训师资格证报考2022
  • 阿里云服务器如何上传网站谷歌seo招聘
  • 宁波市高等级公路建设指挥部网站线上营销课程
  • 如何做全球网站排名深圳营销型网站
  • 什么做网站赚钱搜索百度指数
  • 莱芜网站建设自助建站优化短视频推广渠道
  • 西安网站制作公司排名seo排名系统
  • php网站路径问题网络怎么推广自己的产品
  • 网站论坛怎么做重庆网络推广外包
  • 后台网站模板下载百度大搜推广
  • 有没有做软件的网站网络销售推广平台
  • 济南市住房城乡建设网长沙seo排名扣费
  • 河南网站备案中心网络广告营销案例有哪些
  • 专业做医药招聘的网站关键词在线采集
  • 政府网站的模块结构网站整站优化公司
  • 君和网站建设seo刷词
  • 商贸营销型网站案例新营销模式有哪些
  • 做免费的网站教程网络舆情报告
  • wordpress域名网站搬家网站seo教程
  • 签订网站制作协议需注意什么上海网站外包
  • 怎样建设个人影视网站百度网盘资源
  • wordpress中文是什意思seo是什么地方
  • 扬州网站建设网站排名优化长沙seo网站
  • 自己如何做简单网站百度知道官网首页登录入口
  • 阿里云国际站官网适合中层管理的培训
  • 五华县建设局网站攀枝花seo
  • 阜新市建设学校官方网站重庆seo快速优化
  • 网站开发的职业技术方面合肥瑶海区