Android zygote进程启动流程

news/2024/3/28 18:35:58/文章来源:https://blog.csdn.net/qq_36063677/article/details/129235187

zygote启动过程中涉及到以下模块:

  • app_process
  • zygote
    • USAP
    • socket
    • FileDescriptor (FD)
  • AndroidRuntime
  • AppRuntime (定义于app_process模块,继承自AndroidRuntime。)

init进程启动zygote进程:

#init.zygote32_64.rc
service zygote /system/bin/app_process32 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygoteclass mainpriority -20user rootgroup root readproc reserved_disksocket zygote stream 660 root systemsocket usap_pool_primary stream 660 root systemonrestart exec_background - system system -- /system/bin/vdc volume abort_fuseonrestart write /sys/power/state ononrestart restart audioserveronrestart restart cameraserveronrestart restart mediaonrestart restart netdonrestart restart wificondwritepid /dev/cpuset/foreground/tasksservice zygote_secondary /system/bin/app_process64 -Xzygote /system/bin --zygote --socket-name=zygote_secondaryclass mainpriority -20user rootgroup root readproc reserved_disksocket zygote_secondary stream 660 root systemsocket usap_pool_secondary stream 660 root systemonrestart restart zygotewritepid /dev/cpuset/foreground/tasks

一、绑定socket

init.zygote32_64.rc脚本中分别定义了32位(primary)和64位(secondary)zygote进程的启动参数,在/dev/socket/下创建UNIX域的socket,并把socket的fd传给对应的进程。

在这里插入图片描述

“ANDROID_SOCKET_zygote”,"ANDROID_SOCKET_zygote_secondary"分别为两个zygote的socket在环境变量中对应的key值,通过key获取对应的value,创建FileDescriptor对象。通过 “ls -l /proc/${pid}/fd” 命令可查看对应进程的所有fd指向的符号链接。

    static LocalServerSocket createManagedSocketFromInitSocket(String socketName) {int fileDesc;final String fullSocketName = ANDROID_SOCKET_PREFIX + socketName;	// ANDROID_SOCKET_zygotetry {String env = System.getenv(fullSocketName);fileDesc = Integer.parseInt(env);			// env="20"} catch (RuntimeException ex) {throw new RuntimeException("Socket unset or invalid: " + fullSocketName, ex);}try {FileDescriptor fd = new FileDescriptor();fd.setInt$(fileDesc);return new LocalServerSocket(fd);} catch (IOException ex) {throw new RuntimeException("Error building socket from file descriptor: " + fileDesc, ex);}}

二、zygote启动

app_process进程:

//app_main.cpp
int main(int argc, char* const argv[])
{//...if (zygote) {runtime.start("com.android.internal.os.ZygoteInit", args, zygote);} else if (className) {runtime.start("com.android.internal.os.RuntimeInit", args, zygote);} 
}

变量runtime是AppRuntime对象,调用AndoridRuntime对象start()方法:

//AndroidRuntime.cpp
/** Start the Android runtime.  This involves starting the virtual machine* and calling the "static void main(String[] args)" method in the class* named by "className".** Passes the main function two arguments, the class name and the specified* options string.*/
void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{//.../* start the virtual machine * 1.启动Java虚拟机。*/JniInvocation jni_invocation;jni_invocation.Init(NULL);JNIEnv* env;if (startVm(&mJavaVM, &env, zygote, primary_zygote) != 0) {return;}onVmCreated(env);/** Register android functions.* 2.注册JNI方法。*/if (startReg(env) < 0) {ALOGE("Unable to register all android natives\n");return;}/** Start VM.  This thread becomes the main thread of the VM, and will* not return until the VM exits.* 3.启动对应类的静态 main()。*/jmethodID startMeth = env->GetStaticMethodID(startClass, "main","([Ljava/lang/String;)V");env->CallStaticVoidMethod(startClass, startMeth, strArray);
}

启动zygote时className变量是"com.android.internal.os.ZygoteInit",查看其main()方法:

//ZygoteInit.java
public static void main(String[] argv) {//.../** 1. 执行预加载。*/if (!enableLazyPreload) {preload(bootTimingsTraceLog);}/** 2. 实例化ZygoteServer()对象,*   1).初始化 Zygote server socket。*   2).初始化 USAP pool server socket。*   3).初始化 USAP pool event FD。*/zygoteServer = new ZygoteServer(isPrimaryZygote);/** 3. 启动 system_server进程,*/if (startSystemServer) {Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);// {@code r == null} in the parent (zygote) process, and {@code r != null} in the// child (system_server) process.if (r != null) {r.run();return;}}/**	4. 循环接受处理 socket 事件。*/// The select loop returns early in the child process after a fork and// loops forever in the zygote.caller = zygoteServer.runSelectLoop(abiList);
}

三、处理socket消息

ZygoteServer接收到socket消息后,调用processOneCommand方法解析消息参数。

//ZygoteServer.javaZygoteConnection connection = peers.get(pollIndex);final Runnable command = connection.processOneCommand(this);

最终在Zygote类的forkAndSpecialize方法中调用nativeForkAndSpecialize()方法fork新进程:

//Zygote.java
static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags,int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir,boolean isTopApp, String[] pkgDataInfoList, String[] whitelistedDataInfoList,boolean bindMountAppDataDirs, boolean bindMountAppStorageDirs) {ZygoteHooks.preFork();int pid = nativeForkAndSpecialize(uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,fdsToIgnore, startChildZygote, instructionSet, appDataDir, isTopApp,pkgDataInfoList, whitelistedDataInfoList, bindMountAppDataDirs,bindMountAppStorageDirs);ZygoteHooks.postForkCommon();return pid;}

重点查看ZygoteConnection类的processOneCommand()方法:

在nativeForkAndSpecialize()方法fork新进程后,通过判断返回的pid是否大于0区分母子进程。

​ fork是类Unix操作系统上创建进程的主要方法。fork用于创建子进程(等同于当前进程的副本),新的进程要通过老的进程复制自身得到,这就是fork!

​ fork作为一个函数被调用。这个函数会有两次返回,将子进程的PID返回给父进程,0返回给子进程。(如果小于0,则说明创建子进程失败)。

//ZygoteConnection.java
Runnable processOneCommand(ZygoteServer zygoteServer) {//...pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids,parsedArgs.mRuntimeFlags, rlimits, parsedArgs.mMountExternal, parsedArgs.mSeInfo,parsedArgs.mNiceName, fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote,parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mIsTopApp,parsedArgs.mPkgDataInfoList, parsedArgs.mWhitelistedDataInfoList,parsedArgs.mBindMountAppDataDirs, parsedArgs.mBindMountAppStorageDirs);try {if (pid == 0) {// in childzygoteServer.setForkChild();zygoteServer.closeServerSocket();IoUtils.closeQuietly(serverPipeFd);serverPipeFd = null;return handleChildProc(parsedArgs, childPipeFd, parsedArgs.mStartChildZygote);} else {// In the parent. A pid < 0 indicates a failure and will be handled in// handleParentProc.IoUtils.closeQuietly(childPipeFd);childPipeFd = null;handleParentProc(pid, serverPipeFd);return null;}} finally {IoUtils.closeQuietly(childPipeFd);IoUtils.closeQuietly(serverPipeFd);}
}

所以可以看到handleChildProc()和handleParentProc()分别处理的是子进程,母进程的逻辑。

最终子进程执行zygoteInit()方法完成一个Android 应用程序的启动(初始化)

    public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,String[] argv, ClassLoader classLoader) {if (RuntimeInit.DEBUG) {Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");}Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");RuntimeInit.redirectLogStreams();RuntimeInit.commonInit();ZygoteInit.nativeZygoteInit();return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,classLoader);}

ZygoteInit.nativeZygoteInit(); 使支持Binder进程间通信机制。

RuntimeInit.applicationInit(); 调用ActivityThread.main()方法,启动UI线程。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_74809.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端开发_快应用开发

目录快应用官网真机调试组件组件嵌套问题tab组件list组件web组件css 样式问题[1]选择器[2]盒模型[3]样式布局-弹性布局[4-1]样式切换 - 类名的动态切换[4-2] 样式切换 - 行内样式动态切换[5]background[6]overflow[7]border-radius[8]盒子阴影[9] 单位系统接口[1] 检查某app是否…

redis五种数据结构

redis五种数据结构1. redis 其他相关1.1 redis 的安装1.2 redis 的持久化1.3 redis 配置文件2. redis 常见命令2.1 key2.2 设置 key 的生存时间或过期时间3. redis的5种常见的数据结构3.1 String3.2 list3.3 hash3.4 set3.5 zset&#xff08;SortedSet&#xff08;有序集合&…

2023年“网络安全”赛项浙江省金华市选拔赛 任务书

2023年“网络安全”赛项浙江省金华市选拔赛 任务书 任务书 一、竞赛时间 共计3小时。 二、竞赛阶段 竞赛阶段 任务阶段 竞赛任务 竞赛时间 分值 第一阶段单兵模式系统渗透测试 任务一 Windows操作系统渗透测试 任务二 Linux操作系统渗透测试 任务三 网页渗透 任务四 Linux系统…

《爆肝整理》保姆级系列教程python接口自动化(二十四)--unittest断言——中(详解)

简介 上一篇通过简单的案例给小伙伴们介绍了一下unittest断言&#xff0c;这篇我们将通过结合和围绕实际的工作来进行unittest的断言。这里以获取城市天气预报的接口为例&#xff0c;设计了 2 个用例&#xff0c;一个是查询北京的天气&#xff0c;一个是查询 南京为例&#xf…

继电器的工作原理、构成和功能介绍

随着电力应用的不断发展&#xff0c;电气设备已经深入到我们的日常生活中&#xff0c;电气自动化技术大量使用在电力系统和生产型企业中&#xff0c;人们在享受电带来方便的同时要注意用电保护。继电器就是为了保护电路而生的&#xff0c;可以提高电路可靠性&#xff0c;保障用…

RabbitMQ实现死信队列

目录死信队列是什么怎样实现一个死信队列说明实现过程导入依赖添加配置编写mq配置类添加业务队列的消费者添加死信队列的消费者添加消息发送者添加消息测试类测试死信队列的应用场景总结死信队列是什么 “死信”是RabbitMQ中的一种消息机制&#xff0c;当你在消费消息时&#…

wafw00f 防火墙探测

kali机器自带防火墙探测工具wafw00&#xff0c;它可以通过发送正常以及不正常甚至包含恶意代码的HTTP请求&#xff0c;来探测网站是否存在防火墙&#xff0c;并识别防火墙的厂商及类型。安装&#xff1a;git clone https://github.com/EnableSecurity/wafw00f.git python setup…

Windows如何查看某个端口被占用的情况?

在工作中&#xff0c;有时会发现端口被占用的情况&#xff0c;导致软件报错或者服务无法启动等问题。在不知道具体哪个进程占用该端口号的情况下&#xff0c;我们可以用下面方法来查找。 举例&#xff1a;我现在发现8090端口被占用了&#xff0c;我现在需要找到并杀掉该进程。…

TCP状态转换

欢迎关注博主 Mindtechnist 或加入【Linux C/C/Python社区】一起探讨和分享Linux C/C/Python/Shell编程、机器人技术、机器学习、机器视觉、嵌入式AI相关领域的知识和技术。 TCP状态转换专栏&#xff1a;《Linux从小白到大神》《网络编程》 TCP状态转换示意图如下 针对上面的示…

高并发之读多写少的场景设计(用户中心)

用户中心是一个典型的读多写少系统&#xff0c;可以说我们大部分的系统都属于这种类型&#xff0c;而这类系统通过缓存就能获得很好的性能提升。并且在流量增大后&#xff0c;用户中心通常是系统改造中第一个要优化的模块&#xff0c;因为它常常和多个系统重度耦合&#xff0c;…

消息队列介绍和RabbitMQ的安装

1.消息队列 1.1 MQ的相关概念 1.1.1 什么是MQ MQ(message queue)&#xff0c;从字面意思上看&#xff0c;本质是个队列&#xff0c;FIFO 先入先出&#xff0c;只不过队列中存放的内容是message 而已&#xff0c;还是一种跨进程的通信机制&#xff0c;用于上下游传递消息。在…

高阶人生从在职读研开始——中国社科院与美国杜兰大学金融管理硕士

说到学历&#xff0c;好多人都不太在意&#xff0c;感觉学历没什么用。等升职学历被卡时&#xff0c;等你想考公学历达不到时&#xff0c;当你想跳槽更大的平台时&#xff0c;学历会显得尤其重要。当机会来临时&#xff0c;我们应该做好全足的准备&#xff0c;而不是眼瞅着机会…

SpringBoot相关操作

01-今日内容 Spring概述、快速入门SpringBoot配置SpringBoot整合 02-SpringBoot概述 SpringBoot提供了一种快速使用Spring的方式&#xff0c;基于约定优于配置的思想&#xff0c;可以让开发人员不必在配置与逻辑业务之间进行思维的切换&#xff0c;全身心的投入到逻辑业务的…

金融信创步入快车道,应“需”而生的监控易运维方案为国产化助力

在我国“28N”信创三步走战略中&#xff0c;金融信创赫然名列其中&#xff0c;成为最早践行信创理论与实操的行业之一。截止到目前&#xff0c;金融信创渗透率业已仅次于党政部门&#xff0c;位列“8”大重点行业之首。超快的发展速度&#xff0c;让金融信创较早的步入“买方市…

浅谈模型评估选择及重要性

作者&#xff1a;王同学 来源&#xff1a;投稿 编辑&#xff1a;学姐 模型评估作为机器学习领域一项不可分割的部分&#xff0c;却常常被大家忽略&#xff0c;其实在机器学习领域中重要的不仅仅是模型结构和参数量&#xff0c;对模型的评估也是至关重要的&#xff0c;只有选择那…

前端开发:JS的节流与防抖

前言 在前端实际开发中&#xff0c;有关JS原生的节流和防抖处理也是很重要的点&#xff0c;关于底层和原理的掌握使用&#xff0c;尤其是在性能优化方面甚为重要。作为前端开发的进阶内容&#xff0c;在实际开发过程中节流和防抖通常都是项目优化的必要手段&#xff0c;而且也是…

机器学习笔记之近似推断(二)推断的核心思想

机器学习笔记之近似推断——推断的核心思想引言回顾&#xff1a;推断的目的与困难推断的目的推断的困难推断的核心思想——优化引言 上一节介绍了从深度学习的角度介绍了推断&#xff0c;并介绍了推断的目的和困难。本节将继续介绍推断的核心思想。 回顾&#xff1a;推断的目…

写给交互设计新手的信息架构全方位指南

目录什么是信息架构&#xff1f;通用方法日常工作可以关注的大神常用工具相关书籍什么是信息架构&#xff1f;信息架构是一个比众多其他领域更难定义的领域。内容策划由内容策划师来完成&#xff0c;交互设计由设计师来完成&#xff0c;而信息架构的完成与它们不同&#xff0c;…

达梦数据库(DM8)集成使用 Geotools(27.2)

达梦数据库&#xff08;DM8&#xff09;集成使用 Geotools&#xff08;27.2&#xff09;系统环境版本达梦 8 集成 Geotools 环境安装达梦8&#xff0c;请参照项目 pom.xml 添加 geotools 配置项目 pom.xml 添加达梦数据库驱动包Geotools 使用示例Geotools 连接数据库Geotools 空…

CLion Remote Debug CrossCompile

CLion远程Docker调试ROS(交叉编译)的设置步骤 准备一个好用的docker&#xff0c;运行起来&#xff08;Docker Image一定可以跑cuda和图形界面的&#xff0c;否则启动不了CLion&#xff0c;可以不用浪费时间看本教程了&#xff09; 在docker镜像中配置好ssh和rsync&#xff0c;…