#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. 在Linux系统里运行shutdown.sh命令关闭Tomcat时出现错误提示

    服务器:linnux 5.5 64位,已安装好 jdk: Tomcat版本:apache-tomcat-7.0.53 操作软件:Xshell 4(Free for Home / School) 刚开始 ...

  2. CentOS7安装Memcached 三步曲

    1.yum 安装 yum clean allyum -y updateyum -y install memcached 2.Memcached 运行 memcached -h //查看考号修改配置vi ...

  3. python dlib 面部轮廓实时检测

    1.dlib 实现动态人脸检测及面部轮廓检测 模型下载连接 : http://dlib.net/files/ # coding:utf-8 import cv2 import os import dl ...

  4. 代码合并:Merge、Rebase 的选择

    图解 Git 命令 基本用法 上面的四条命令在工作目录.stage 缓存(也叫做索引)和 commit 历史之间复制文件. git add files 把工作目录中的文件加入 stage 缓存 git ...

  5. 去掉每行的特定字符py脚本

    百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...

  6. mount/umount命令【转】

    转自:http://www.cnblogs.com/qq78292959/archive/2012/03/06/2382334.html 如果想在运行的Linux下访问其它文件系统中的资源的话,就要用 ...

  7. java多线程以及Android多线程

    Java 多线程 线程和进程的区别 线程和进程的本质:由CPU进行调度的并发式执行任务,多个任务被快速轮换执行,使得宏观上具有多个线程或者进程同时执行的效果. 进程:在操作系统来说,一个运行的程序或者 ...

  8. java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast

    严重: Exception starting filter encodingFilterjava.lang.ClassCastException: org.springframework.web.fi ...

  9. css3属性书写的时候带的一些前缀的意思

    使用css3属性时,大部分都要带这些识别前缀,早期点的浏览器才能识别.现在最新版的浏览器基本都支持css3 基本都不用写前缀 ,写前缀是为了向前兼容老版本的浏览器而已. -ms-transform: ...

  10. cf786a

    title: CodeForces 786A Berzerk data: 2018-3-3 10:29:40 tags: 博弈论 bfs 无限 with draw copyright: true ca ...