#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. Java反射中method.isBridge() 桥接方法

    桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法.我们可以通过Method.isBridge()方法来判断一个方法是 ...

  2. MyBatis框架的使用及源码分析(十一) StatementHandler

    我们回忆一下<MyBatis框架的使用及源码分析(十) CacheExecutor,SimpleExecutor,BatchExecutor ,ReuseExecutor> , 这4个Ex ...

  3. 【BZOJ】1710: [Usaco2007 Open]Cheappal 廉价回文

    [算法]区间DP [题解]回文问题的套路做法:区间DP. f[i][j]表示区间i~j回文的最小代价,则有f[i][j]=min{①②③}. ①f[i+1][j]+min(a[s[i]],b[s[i] ...

  4. hdu 1241Oil Deposits(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  5. 【转】关于Scapy

    关于Scapy Scapy的是一个强大的交互式数据包处理程序(使用python编写).它能够伪造 或者解码大量的网络协议数据包,能够发送.捕捉.匹配请求和回复包等等.它可以很容易地处理一些典型操作,比 ...

  6. LeetCode 19 Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. 简谈const限定符

    const修饰的数据类型是常量类型,常量类型的对象和变量在定义初始化后是不能被更新的.其实只用记住这一个概念,就可以明白const操作对象的方法. 1)定义const常量 最简单的: const in ...

  8. handle_level_irq 与handle_edge_irq 的区别【转】

    转自:http://blog.csdn.net/xavierxiao/article/details/6087277 版权声明:本文为博主原创文章,未经博主允许不得转载. Linux 里, handl ...

  9. AJAX 核心 —— XMLHTTPRequest 对象回顾

    一.AJAX概述 不使用 AJAX 的网页,如果要更新内容,需要重载整个页面. AJAX ( Asynchronous Javascript And XML ,异步 Javascript 和 XML) ...

  10. 自动化测试===Httprunner测试框架介绍

    项目地址: https://github.com/HttpRunner/HttpRunner 中文手册: http://cn.httprunner.org/ 首先是环境搭建: pip install ...