子进程system_service创建后while一直判断子进程状态是否died,如果会则zygote也kill自己
2013-11-14 16:01
721 查看
基于:http://blog.csdn.net/jianguo_liao19840726/article/details/16116859
我们进入到pid = Zygote.forkSystemServer(
来看看这个native方法的注释:
这个注释很好解释,特别是the child process will cause zygote to exit ,就是由zygote fork 出的system_service进程如果死了,则zygote进程也退出,我们还是进入其cpp中的方法来看一下吧,在dalvik.system.Zygote.c中,代码如下:
从上面我们找到forkSystemServer对应的方法是Dalvik_dalvik_system_Zygote_forkSystemServer根据如下:
上面贴出了Dalvik_dalvik_system_Zygote_forkAndSpecialize方法,其中涉及到
1、pid = forkAndSpecializeCommon(args, true); 主要用来fork 子进程system_service
2、waitpid(pid, &status, WNOHANG) == pid 检查,如果子进程died则 Restarting Zygote!
我们进入pid = forkAndSpecializeCommon(args, true);
从上面我们看到了fork,但是我们也看到了,如果子进程died则会进入设置的信号处理函数
进入信号处理函数
信号处理函数可以看出一直在while,一直在查看子进程状态,如果子进程一旦died,则kill掉zygote
我们进入到pid = Zygote.forkSystemServer(
native public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
来看看这个native方法的注释:
/** * Special method to start the system server process. In addition to the * common actions performed in forkAndSpecialize, the pid of the child * process is recorded such that the death of the child process will cause * zygote to exit.
这个注释很好解释,特别是the child process will cause zygote to exit ,就是由zygote fork 出的system_service进程如果死了,则zygote进程也退出,我们还是进入其cpp中的方法来看一下吧,在dalvik.system.Zygote.c中,代码如下:
/* native public static int forkSystemServer(int uid, int gid, * int[] gids, int debugFlags, long permittedCapabilities, * long effectiveCapabilities); */ static void Dalvik_dalvik_system_Zygote_forkSystemServer( const u4* args, JValue* pResult) { pid_t pid; pid = forkAndSpecializeCommon(args, true); /* The zygote process checks whether the child process has died or not. */ if (pid > 0) { int status; LOGI("System server process %d has been created", pid); gDvm.systemServerPid = pid; /* There is a slight window that the system server process has crashed * but it went unnoticed because we haven't published its pid yet. So * we recheck here just to make sure that all is well. */ if (waitpid(pid, &status, WNOHANG) == pid) { LOGE("System server process %d has died. Restarting Zygote!", pid); kill(getpid(), SIGKILL); } } RETURN_INT(pid); } const DalvikNativeMethod dvm_dalvik_system_Zygote[] = { { "fork", "()I", Dalvik_dalvik_system_Zygote_fork }, { "forkAndSpecialize", "(II[II[[I)I", Dalvik_dalvik_system_Zygote_forkAndSpecialize }, { "forkSystemServer", "(II[II[[IJJ)I", Dalvik_dalvik_system_Zygote_forkSystemServer }, { NULL, NULL, NULL }, };
从上面我们找到forkSystemServer对应的方法是Dalvik_dalvik_system_Zygote_forkSystemServer根据如下:
{ "forkSystemServer", "(II[II[[IJJ)I", Dalvik_dalvik_system_Zygote_forkSystemServer }
上面贴出了Dalvik_dalvik_system_Zygote_forkAndSpecialize方法,其中涉及到
1、pid = forkAndSpecializeCommon(args, true); 主要用来fork 子进程system_service
2、waitpid(pid, &status, WNOHANG) == pid 检查,如果子进程died则 Restarting Zygote!
if (waitpid(pid, &status, WNOHANG) == pid) { LOGE("System server process %d has died. Restarting Zygote!", pid); kill(getpid(), SIGKILL); }
我们进入pid = forkAndSpecializeCommon(args, true);
static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer) { setSignalHandler(); dvmDumpLoaderStats("zygote"); pid = fork(); if (pid == 0) { Thread* thread = dvmThreadSelf(); thread->systemTid = dvmGetSysThreadId(); unsetSignalHandler(); } else if (pid > 0) { /* the parent process */ } return pid; }
从上面我们看到了fork,但是我们也看到了,如果子进程died则会进入设置的信号处理函数
/* * configure sigchld handler for the zygote process * This is configured very late, because earlier in the dalvik lifecycle * we can fork() and exec() for the verifier/optimizer, and we * want to waitpid() for those rather than have them be harvested immediately. * * This ends up being called repeatedly before each fork(), but there's * no real harm in that. */ static void setSignalHandler() { int err; struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = sigchldHandler; err = sigaction (SIGCHLD, &sa, NULL); if (err < 0) { LOGW("Error setting SIGCHLD handler: %s", strerror(errno)); } }
进入信号处理函数
/* * This signal handler is for zygote mode, since the zygote * must reap its children */ static void sigchldHandler(int s) { while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { /* Log process-death status that we care about. In general it is not safe to call LOG(...) from a signal handler because of possible reentrancy. However, we know a priori that the current implementation of LOG() is safe to call from a SIGCHLD handler in the zygote process. If the LOG() implementation changes its locking strategy or its use of syscalls within the lazy-init critical section, its use here may become unsafe. */ /* * If the just-crashed process is the system_server, bring down zygote * so that it is restarted by init and system server will be restarted * from there. */ if (pid == gDvm.systemServerPid) { LOG(LOG_INFO, ZYGOTE_LOG_TAG, "Exit zygote because system server (%d) has terminated\n", (int) pid); kill(getpid(), SIGKILL); } } }
信号处理函数可以看出一直在while,一直在查看子进程状态,如果子进程一旦died,则kill掉zygote
相关文章推荐
- linux的父进程向子进程发kill信号例子以及对子进程的状态进行判断
- oracle和mysql数据库创建表之前判断表是否存在,如果存在则删除已有表,以及在这两个库中创建表
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- python 判断目录是否存在,如果不存在则创建
- Linux 简单shell创建自己的守护进程,自动重启,纪录进程运行状态,日志切割压缩
- shell判断一个进程是否存在,如果不存在重启该进程
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查 HKEY_LOCAL_MACHINE/SYSTEM/Curre
- mysql数据库表中判断字段是否存在,如果不存在则创建该字段
- 关于判断CreateProcess创建的子进程何时退出和CRichEditCtrl::FindText()一直返回-1的问题解决。
- 【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件
- system_service 进程由zygote进程fork来
- MySQL存储过程----创建索引前,先判断索引是否已经存在,如果存在,则不添加,如果不存在,添加
- mysql判断表中字段或者索引是否存在,如果不存在则创建
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- 创建表之前判断表是否存在,如果存在则删除已有表
- oracle创建表之前判断表是否存在,如果存在则删除已有表
- 【Android】判断应用Application、Activity、Service是否处于活动状态
- 多线程操作中为什么使用while而不是if来做判断状态是否就绪
- oracle创建表之前判断表是否存在,如果存在则删除已有表
- 通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件