#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. Zyan 一个通信框架

    原文地址 本文示例 Zyan是一个简单直观的分布式应用程序开发框架. 以下是Zyan项目的简要概述. 架构图: Zyan一般由客户端和服务端组成.服务端(以组件的形式)提供服务,客户端远程调用服务端的 ...

  2. Large Class--过大的类--要重构的信号

    如果想利用单个类做太多事情,其内往往就会出现太多实例变量.一旦如此,Duplicated Code也就接踵而至.     解决方法:     1.将类内彼此相关的变量,将它们放在一起.使用Extrac ...

  3. 《Applying Deep Learning to Answer Selection: A Study And an Open Task》文章理解小结

    本篇论文是2015年的IBM watson团队的. 论文地址: 这是一篇关于QA问题的一篇论文: 相关论文讲解1.https://www.jianshu.com/p/48024e9f7bb22.htt ...

  4. Spring Cloud Eureka服务注册源码分析

    Eureka是怎么work的 那eureka client如何将本地服务的注册信息发送到远端的注册服务器eureka server上.通过下面的源码分析,看出Eureka Client的定时任务调用E ...

  5. hasOwnProperty()方法与in操作符

    1.hasOwnProperty() 该方法检测属性存在于实例,还是存在于原型,对于存在于实例中的属性则返回true 2.in 使用该操作符时只要通过对象能够访问到的属性都会返回true

  6. 纠结于arch+xfce还是xubuntu

    现在用的是ubuntu gnome版 http://www.tuicool.com/articles/6r22eyU 现在纠结于arch+xfce还是xubuntu,因为不想在gnome下面搞什么美化 ...

  7. python实战===百度文字识别sdk

    http://ai.baidu.com/docs#/OCR-Python-SDK/top

  8. 用vue实现登录页面

    vue和mui一起完成登录页面(在hbuilder编辑器) <!DOCTYPE html> <html> <head> <meta charset=" ...

  9. goreplay HTTP-HTTPS流量复制工具

    goreplay相比tcpcopy只能复制HTTP和HTTPS的流量 goreplay编译很麻烦,就直接使用编译好的版本 gor_0.10.1_x64.tar.gz 支持centos5,测试的是cen ...

  10. mui页面跳转

    $('.mui-title').on('click',function(){ mui.openWindow({ //跳转到指导信息页面 url:"/index.php?m=mobile&am ...