多进程失败拉起的demo
#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的更多相关文章
- 移动端上拉加载,下拉刷新效果Demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NodeJs多进程和socket.io通讯-DEMO
一.开启多进程 const os = require('os'); const cp = require('child_process'); const forkList = {}; const fo ...
- H5下拉刷新特效demo,动画流畅
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 一个多进程爬虫下载图片的demo
import os,re import pickle import requests import random import time from bs4 import BeautifulSoup f ...
- mint-ui下拉加载min和上拉刷新(demo实例)
<template> <div class="share"> <div class="header"> <div cl ...
- 移动应用拉起微信小程序
APP支持打开微信小程序了 最新微信文档 如何实现APP打开小程序 通过文档打开微信开放平台添加移动应用,然后关联小程序,这些步骤按照文档描述走. IOS开发示例参考 android开发示例参考 开发 ...
- ListView下拉刷新
本内容为复制代码: 一.自定义ListView控件: package com.xczl.smart.view; import java.util.Date; import com.suliang.R; ...
- Android UI之下拉刷新上拉刷新实现
在实际开发中我们经常要用到上拉刷新和下拉刷新,因此今天我写了一个上拉和下拉刷新的demo,有一个自定义的下拉刷新控件 只需要在布局文件中直接引用就可以使用,非常方便,非常使用,以下是源代码: 自定义的 ...
- IOS学习之路十二(UITableView下拉刷新页面)
今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...
随机推荐
- MyBatis框架的使用及源码分析(五) DefaultSqlSessionFactory和DefaultSqlSession
我们回顾<MyBatis框架中Mapper映射配置的使用及原理解析(一) 配置与使用> 一文的示例 private static SqlSessionFactory getSessionF ...
- bzoj4695 最假女选手
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4695 [题解] SegmentTree beats!(见jiry_2论文/营员交流) 考虑只 ...
- 【游记】NOIP 2017
时间:2017.11.11~2017.11.12 地点:广东省广州市第六中学 Day1 T1:看到题目,心想这种题目也能放在T1? 这个结论我之前遇到过至少3次,自己也简单证明过.初见是NOIP200 ...
- C++ Vector 中自定义对象的排序
需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...
- python初步学习-python数据类型-集合(set)
集合 在已经学过的数据类型中: 能够索引的,如list/str,其中的元素可以重复 可变的,如list/dict,即其中的元素/键值对可以原地修改 不可变的,如str/int,即不能进行原地修改 无索 ...
- oracle to_char 返回毫秒级
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff') time1, 关键在 systimestamp
- C后端设计开发 - 第7章-真气-遗失的网络IO
正文 第7章-真气-遗失的网络IO 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了. ボクらの冒険 : http://music.163.com/#/m/s ...
- 10 个打造 React.js App 的最佳 UI 框架
10 个打造 React.js App 的最佳 UI 框架 在本文中,我们将分享一些助你打造 React.js App 最佳的 UI 框架.它们具备你所需要的基本 React 组件,以及易用的 API ...
- Python调用shell
今天学习了Python通过子进程调用shell,感觉教程上讲的过于繁复,有一些根本没用的功能,比如重定向输入输出,这个shell本身就支持,没有必要用Python的api.决定自己总结下. 其实总的来 ...
- 听justjavac大神live前端的入门与进阶小笔记
代码规范 代码强壮,调试代码 少用变量,多用常量 少用for循环,why循环,多用函数式, 不要直接去使用框架 刷题 提高编程思维 用js去做c语音的问题 阅读别人代码,去看别人的代码 a+b> ...