ViV0X20有什么手机支持0TG功能么?

2354人阅读
&本文首先发表在&&.
原文地址:&
Linux 中创建子进程是相当方便的,通过fork调用即可。当子进程退出时,要给父进程发送SIG_CHLD信号,是为了父进程回收子进程的资源,方便管理的目的。 但是当父进程退出后,一般是不会通知子进程的,父进程会将自己的所有子进程过继给init进程。
但是,在实际的项目中,我们有这样的需求: 如果父进程退出后,希望能通知子进程退出。
我们知道,可以利用进程间通信机制,在父进程退出前主动发送信号或pipe或其他手段告知子进程自己退出了。
但是,当父进程意外退出时,比如coredump了,根本没机会主动发送信号或pipe等消息的。
这时怎么办呢?
我们发现 prctl 这个调用, 通过man prctl:
PR_SET_PDEATHSIG (since Linux 2.1.57)& & & & & Set &the parent process death signal of the calling process to arg2 & &(either a signal value in the range& & & & & 1..maxsig, or 0 to clear). &This is the signal that the calling process& & & & & will get when its parent &dies.
据此, 我们可以在子进程中给父进程设置当父进程退出时子进程收到的信号。
prctl(PR_SET_PDEATHSIG, SIGHUP);
代码如下:
& #include &stdio.h&& #include &stdlib.h&& #include &string.h&& #include &sys/prctl.h&& #include &signal.h&& static int do_abort = 0;& void handle_signal(int signo)& {& & & if (signo == SIGHUP)& & & { & & & & & & & & & & & & & printf(&child recv SIGHUP..\n&);& & & & & do_abort = 1;& & & }& }& int main(void)& {& & & pid_t pid;& & & pid = fork();& & & char *p = NULL;& & & if (pid == 0) // child& & & {& & & & & signal(SIGHUP, handle_signal);& & & & & prctl(PR_SET_PDEATHSIG, SIGHUP);& & & & & while(!do_abort) {& & & & & & & sleep(1);& & & & & & & printf(&in child...\n&);& & & & & }& & & & & printf(&child exit...\n&);& & & & & return 0;& & & }& & & else // parent& & & {& & & & & int times = 5;& & & & & while(times -- & 0)& & & & & {& & & & & & & sleep(1);& & & & & & & if (times == 3)& & & & & & & {& & & & & & & & & printf(&memcpy ...\n&);& & & & & & & & & memcpy(p, &Hello&, 5);& & & & & & & }& & & & & & & printf(&in parent.\n&);& & & & & }& & & & & printf(&parent exit..\n&);& & & & & return 0;& & & }& & & return 0;& }
通过测试,如果去掉 prctl调用,当父进程发生段错误退出后,子进程依然继续运行。 如果去掉signal调用,即子进程不捕获SIG_HUP信号,当父进程退出后依然会退出,只是退出的有些不优雅罢了。
原创文章,版权归属.转载请标明出处.
本文地址:&
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:116226次
积分:1615
积分:1615
排名:千里之外
原创:26篇
转载:100篇
评论:13条
(1)(3)(1)(4)(3)(3)(2)(1)(4)(3)(7)(2)(3)(2)(1)(2)(1)(1)(2)(1)(1)(7)(11)(1)(1)(4)(1)(9)(9)(7)(2)(8)(8)(5)(2)(3)(1)2013年6月 Linux/Unix社区大版内专家分月排行榜第二2013年5月 Linux/Unix社区大版内专家分月排行榜第二2013年3月 Linux/Unix社区大版内专家分月排行榜第二2013年1月 Linux/Unix社区大版内专家分月排行榜第二2012年12月 Linux/Unix社区大版内专家分月排行榜第二2012年8月 Linux/Unix社区大版内专家分月排行榜第二2011年12月 Linux/Unix社区大版内专家分月排行榜第二2011年10月 C/C++大版内专家分月排行榜第二2011年10月 Linux/Unix社区大版内专家分月排行榜第二
2012年6月 C/C++大版内专家分月排行榜第三2012年6月 PHP大版内专家分月排行榜第三2012年5月 C/C++大版内专家分月排行榜第三2012年3月 Linux/Unix社区大版内专家分月排行榜第三2012年2月 Linux/Unix社区大版内专家分月排行榜第三2011年11月 C/C++大版内专家分月排行榜第三
2011年9月 C/C++大版内专家分月排行榜第二2011年4月 C/C++大版内专家分月排行榜第二2010年11月 C/C++大版内专家分月排行榜第二
2011年6月 C/C++大版内专家分月排行榜第三
2011年9月 C/C++大版内专家分月排行榜第二2011年4月 C/C++大版内专家分月排行榜第二2010年11月 C/C++大版内专家分月排行榜第二
2011年6月 C/C++大版内专家分月排行榜第三
2011年9月 C/C++大版内专家分月排行榜第二2011年4月 C/C++大版内专家分月排行榜第二2010年11月 C/C++大版内专家分月排行榜第二
2011年6月 C/C++大版内专家分月排行榜第三
2011年9月 C/C++大版内专家分月排行榜第二2011年4月 C/C++大版内专家分月排行榜第二2010年11月 C/C++大版内专家分月排行榜第二
2011年6月 C/C++大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。android(87)
在App开发中,经常会遇到单独启动一个进程,用于在后台的任务操作,由于是单独的一个进程,所以主进程和子进程之间互不影响,这样有利有弊!利处肯定很多,比如存活周期不受影响,后台默默处理等等,但也有弊端!
一个明显的弊端就是:app的异常捕获问题!
由于是单独的进程,所以通常操作,在Application中进行捕获异常只能对于主进程的异常有影响,但是子进程的异常就无法捕获了,所以必须特别为子进程进行单独处理异常捕获的问题!
这里是我写的一个Service的进程单独进行异常捕获!关键代码如下:
public class CpuLocationService extends Service implements Thread.UncaughtExceptionHandler
public void onCreate()
super.onCreate();
int i=9/0;
LcbLog.e("i:"+i);
public void uncaughtException(Thread thread, Throwable ex) {
MobclickAgent.reportError(getApplicationContext(),ex);
因为我是用Umeng进行错误统计的,所以在该进程报错的时候,用umeng进行错误上报的,这样就捕获到了子进程的异常日志了!注意:主进程的错误捕获还是必不可少的,这里只作为一个补充!
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:184620次
积分:3030
积分:3030
排名:第10409名
原创:100篇
转载:62篇
评论:37条
阅读:16156
(4)(1)(2)(2)(3)(5)(4)(7)(7)(3)(3)(13)(10)(6)(3)(7)(4)(2)(6)(4)(4)(3)(9)(4)(3)(1)(1)(3)(4)(5)(2)(1)(6)(3)(6)(5)(1)(10)(1)使用代码验证linux子进程与父进程的关系
字体:[ ] 类型:转载 时间:
Linux下父进程可以使用fork 函数创建子进程,但是当父进程先退出后,子进程会不会也退出呢?通过下面这个小实验,我们能够很好的看出来
代码如下:/********& basic.c ********/#include "basic.h"
pid_t Fork(void){&&& pid_t pid = fork();&&& if (pid & 0) {&&&&&&& fprintf(stderr, "Fork error: %s\n", strerror(errno));&&&&&&& exit(0);&&& }
代码如下:**********& basic.h& ***********
#ifndef __CSAPP_BASIC_H#define __CSAPP_BASIC_H
#include &stdio.h&#include &errno.h&#include &stdlib.h&#include &signal.h&#include &unistd.h&#include &string.h&/* function definition concerned with basic.c */pid_t Fork();
代码如下:*******& fork.c& *********
#include "basic.h"
int main(){&&& int pid = Fork();&&& int x = 2;
&&& if (pid == 0) {&&&&&&& printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x);&&&&&&& sleep(3);&&&&&&& printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x);&&&&&&& exit(0);&&& }
&&& printf("parent: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), --x);
通过 gcc fork.c basic.c -o fork 编译即可的 fork 程序。& 运行 ./fork
可以看出父进程首先退出,退出前child的PPID为12256, 退出后子进程的PPID变为了 1.说明父进程退出后的子进程由 init 超级进程1领养。而该进程是不绝不会退出的。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 什么手机支持0TG功能 的文章

 

随机推荐