#include <iostream>
#include <vector>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <errno.h> void RunChild(int idx) {
for (int i = ; i < ; i++) {
printf("worker %d %d\n", getpid(), idx);
sleep();
}
} int main() {
int worker_cnt = ;
std::vector<int> worker_pids(worker_cnt);
for (int i = ; i < worker_cnt; i++) {
int pid = fork();
if(pid != ) {
//no process < 0 ?
worker_pids[i] = pid;
continue;
} RunChild(i);
exit();
} //avoid some unkown action for SIGCHLD
signal(SIGCHLD, SIG_DFL);
while (true) {
//1.get exit child
//2.get worker by pid
//3.fork it
//4.child run //1.get exit child
int pid = -;
while((pid = wait(NULL)) == -) {
if (errno == EINTR) {
printf("wait pid err %d %s\n", errno, strerror(errno));
continue;
}
else {
break;
}
}
if(pid == -) {
printf("wait pid err %d %s\n", errno, strerror(errno));
//sleep( 1 );
continue;
}
printf("wait pid %d\n", pid); //2.get worker by pid
int worker_id = -;
for (int i = ; i < worker_cnt; i++) {
if (worker_pids[i] == pid) {
worker_id = i;
break;
}
}
printf("worker pid %d id %d exit\n", pid, worker_id);
if( worker_id == - ) {
printf("master wait pid %d not worker\n", pid);
continue;
} //3.fork it
usleep();
pid = fork();
if (pid != ) {
// no process pid < 0 ?
printf("%s new worker id %d\n", __func__, pid);
worker_pids[worker_id] = pid;
continue;
} //4.child run
RunChild(worker_id);
exit();
}
return ;
}

这里有个问题,父进程退出了,子进程还没退出。解决方案是在RunChild中调用:

 void RunChild(int idx) {
prctl(PR_SET_PDEATHSIG, SIGHUP);
for (;true;) {
printf("worker %d %d\n", getpid(), idx);
sleep();
}
}

可以查看下man 2 prctl。

https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits/284443

多进程失败拉起的demo的更多相关文章

  1. 移动端上拉加载,下拉刷新效果Demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. NodeJs多进程和socket.io通讯-DEMO

    一.开启多进程 const os = require('os'); const cp = require('child_process'); const forkList = {}; const fo ...

  3. H5下拉刷新特效demo,动画流畅

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. 一个多进程爬虫下载图片的demo

    import os,re import pickle import requests import random import time from bs4 import BeautifulSoup f ...

  5. mint-ui下拉加载min和上拉刷新(demo实例)

    <template> <div class="share"> <div class="header"> <div cl ...

  6. 移动应用拉起微信小程序

    APP支持打开微信小程序了 最新微信文档 如何实现APP打开小程序 通过文档打开微信开放平台添加移动应用,然后关联小程序,这些步骤按照文档描述走. IOS开发示例参考 android开发示例参考 开发 ...

  7. ListView下拉刷新

    本内容为复制代码: 一.自定义ListView控件: package com.xczl.smart.view; import java.util.Date; import com.suliang.R; ...

  8. Android UI之下拉刷新上拉刷新实现

    在实际开发中我们经常要用到上拉刷新和下拉刷新,因此今天我写了一个上拉和下拉刷新的demo,有一个自定义的下拉刷新控件 只需要在布局文件中直接引用就可以使用,非常方便,非常使用,以下是源代码: 自定义的 ...

  9. IOS学习之路十二(UITableView下拉刷新页面)

    今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...

随机推荐

  1. 文件字节大小显示成M,G和K

    //字节大小,K,M,G public static final long KB = 1024; public static final long MB = KB * 1024; public sta ...

  2. gridveiw的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  3. Grunt构建工具

    Grunt是javascript的构建工具,对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等,自动化工具可以简化工作.Grunt生态系统非常庞大.你可以利 ...

  4. 【Matlab】使用Matlab运行Windows命令

    可以使用Matlab的一些命令来帮助程序运行.比如说 ! calc % 打开计算器 ! mspaint % 打开画图 dos calc % 打开计算器 比如一个程序要运行很长时间,而我们又不能一直守在 ...

  5. Linux内核的架构

    GNU/Linux操作系统架构 备注:IPC进程间通.IPC(Inter-Process Communication)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道 ...

  6. python实战===短信验证码的小项目

    项目地址 https://www.shiyanlou.com/courses/609/labs/2007/document flask的中文文档 http://docs.jinkan.org/docs ...

  7. python基础===jieba模块,Python 中文分词组件

    api参考地址:https://github.com/fxsjy/jieba/blob/master/README.md 安装自行百度 基本用法: import jieba #全模式 word = j ...

  8. linux===给新手的 10 个有用 Linux 命令行技巧(转)

    本文转自:http://www.codeceo.com/article/10-linux-useful-command.html?ref=myread 仅用作学习交流使用.如有侵权,立删 我记得我第一 ...

  9. 64_g2

    gettext-libs-0.19.8.1-9.fc26.x86_64.rpm 15-Mar-2017 14:15 305038 gf2x-1.1-9.fc26.i686.rpm 11-Feb-201 ...

  10. const 引用的分析

    const 引用: 在初始化常量引用时,允许用任意表达式作为初始值,只要该表达式的结果能转换成引用的类型即可.尤其,允许为一个常量引用绑定非常量的对象.字面值,甚至是一个表达式.我们来看 const ...