ImageVerifierCode 换一换
格式:DOCX , 页数:27 ,大小:63.42KB ,
资源ID:8960002      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-8960002.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Android Zygote系统进程启动过程分析Android N.docx)为本站会员(b****8)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

Android Zygote系统进程启动过程分析Android N.docx

1、Android Zygote系统进程启动过程分析Android NAndroid Zygote系统进程启动过程分析(Android N) 在Android系统中,所有的应用程序进程以及系统服务进程SystemServer都是由Zygote进程孕育(fork)出来的,因为Android系统是基于Linux内核的,而在Linux系统中,所有的进程都是init进程的子孙进程,也就是说,所有的进程都是直接或者间接地由init进程fork出来的。Zygote进程也不例外,它是在系统启动的过程,由init进程创建的,在系统启动脚本system/core/rootdir/init.rc文件中,我们可以看到启

2、动Zygote进程的脚本命令:cpp view plain copy 在CODE上查看代码片派生到我的代码片import /init.$ro.zygote.rc 从上面的代码可以看出,在system/core/rootdir目录下系统不止一个zygote*.rc文件每个文件里的启动zygote方式差不多,下面就以init.zygote64_32.rc为例看下里面的代码cpp view plain copy 在CODE上查看代码片派生到我的代码片service zygote /system/bin/app_process64 -Xzygote /system/bin -zygote -start

3、-system-server -socket-name=zygote class main socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart audioserver onrestart restart cameraserver onrestart restart media onrestart restart netd writepid /dev/cpuse

4、t/foreground/tasks service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin -zygote -socket-name=zygote_secondary class main socket zygote_secondary stream 660 root system onrestart restart zygote writepid /dev/cpuset/foreground/tasks 看到上面的代码估计你也能猜出来另外三个zygote*.rc是怎样的,具体选择哪个文件和编译时定义的r

5、o.zygote值有关。前面的关键字service告诉init进程创建一个名为zygote的进程,这个zygote进程要执行的程序是/system/bin/app_process64,后面是要传给app_process64的参数。 接下来的class main表示执行system/bin/app_process64后调用main方法,socket关键字表示这个zygote进程需要一个名称为zygote的socket资源,这样,系统启动后,我们就可以在/dev/socket目录下看到有一个名为zygote的文件,onrestart关键字表示这个zygote进程重启时需要执行的命令,最后一个wri

6、tepid关键字表示需要重写系统pid。 通过上面我们知道Zygote进程要执行的程序便是app_process64了,它位于frameworks/base/cmds/app_process/app_main.cpp文件中,入口函数是main。(app_process64 or app_process32 都是通过frameworks/base/cmds/app_process编译出来的,只是由编译环境来决定生成那个文件),在分析zygote进程启动之前,来看看它启动的时序图下面就看看从app_process64到Zygote启动具体流程:1、在app_main.cpp的main函数中利用An

7、droidRuntime启动Zygotejava view plain copy 在CODE上查看代码片派生到我的代码片int main(int argc, char* const argv) if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) 0) / Older kernels dont understand PR_SET_NO_NEW_PRIVS and return / EINVAL. Dont die on such kernels. if (errno != EINVAL) LOG_ALWAYS_FATAL(PR_SET_NO_NEW_PRIVS

8、failed: %s, strerror(errno); return 12; AppRuntime runtime(argv0, computeArgBlockSize(argc, argv); / Process command line arguments / ignore argv0 argc-; argv+; / Everything up to - or first non - arg goes to the vm. / / The first argument after the VM args is the parent dir, which / is currently un

9、used. / / After the parent dir, we expect one or more the following internal / arguments : / / -zygote : Start in zygote mode / -start-system-server : Start the system server. / -application : Start in application (stand alone, non zygote) mode. / -nice-name : The nice name for this process. / / For

10、 non zygote starts, these arguments will be followed by / the main class name. All remaining arguments are passed to / the main method of this class. / / For zygote starts, all remaining arguments are passed to the zygote. / main function. / / Note that we must copy argument string values since we w

11、ill rewrite the / entire argument block when we apply the nice name to argv0. int i; for (i = 0; i argc; i+) if (argvi0 != -) break; if (argvi1 = - & argvi2 = 0) +i; / Skip -. break; runtime.addOption(strdup(argvi); / Parse runtime arguments. Stop at first unrecognized option. bool zygote = false; b

12、ool startSystemServer = false; bool application = false; String8 niceName; String8 className; +i; / Skip unused parent dir argument. while (i argc) const char* arg = argvi+; if (strcmp(arg, -zygote) = 0) zygote = true; niceName = ZYGOTE_NICE_NAME; else if (strcmp(arg, -start-system-server) = 0) star

13、tSystemServer = true; else if (strcmp(arg, -application) = 0) application = true; else if (strncmp(arg, -nice-name=, 12) = 0) niceName.setTo(arg + 12); else if (strncmp(arg, -, 2) != 0) className.setTo(arg); break; else -i; break; Vector args; if (!className.isEmpty() / Were not in zygote mode, the

14、only argument we need to pass / to RuntimeInit is the application argument. / / The Remainder of args get passed to startup class main(). Make / copies of them before we overwrite them with the process name. args.add(application ? String8(application) : String8(tool); runtime.setClassNameAndArgs(cla

15、ssName, argc - i, argv + i); else / Were in zygote mode. maybeCreateDalvikCache(); if (startSystemServer) args.add(String8(start-system-server); char propPROP_VALUE_MAX; if (property_get(ABI_LIST_PROPERTY, prop, NULL) = 0) LOG_ALWAYS_FATAL(app_process: Unable to determine ABI list from property %s.,

16、 ABI_LIST_PROPERTY); return 11; String8 abiFlag(-abi-list=); abiFlag.append(prop); args.add(abiFlag); / In zygote mode, pass all remaining arguments to the zygote / main() method. for (; i argc; +i) args.add(String8(argvi); if (!niceName.isEmpty() runtime.setArgv0(niceName.string(); set_process_name

17、(niceName.string(); if (zygote) runtime.start(com.android.internal.os.ZygoteInit, args, zygote);/启动ZygoteInit else if (className) runtime.start(com.android.internal.os.RuntimeInit, args, zygote); else fprintf(stderr, Error: no class name or -zygote supplied.n); app_usage(); LOG_ALWAYS_FATAL(app_proc

18、ess: no class name or -zygote supplied.); return 10; main函数的主要作用就是创建一个AppRuntime变量,然后调用它的start成员函数,AppRuntime这个类同样是在frameworks/base/cmds/app_process/app_main.cpp文件中定义:java view plain copy 在CODE上查看代码片派生到我的代码片class AppRuntime : public AndroidRuntime public: AppRuntime(char* argBlockStart, const size_t

19、 argBlockLength) : AndroidRuntime(argBlockStart, argBlockLength) , mClass(NULL) void setClassNameAndArgs(const String8& className, int argc, char * const *argv) mClassName = className; for (int i = 0; i FindClass(slashClassName); if (mClass = NULL) ALOGE(ERROR: could not find class %sn, mClassName.s

20、tring(); free(slashClassName); mClass = reinterpret_cast(env-NewGlobalRef(mClass); virtual void onStarted() sp proc = ProcessState:self(); ALOGV(App process: starting thread pool.n); proc-startThreadPool(); AndroidRuntime* ar = AndroidRuntime:getRuntime(); ar-callMain(mClassName, mClass, mArgs); IPC

21、ThreadState:self()-stopProcess(); virtual void onZygoteInit() sp proc = ProcessState:self(); ALOGV(App process: starting thread pool.n); proc-startThreadPool(); virtual void onExit(int code) if (mClassName.isEmpty() / if zygote IPCThreadState:self()-stopProcess(); AndroidRuntime:onExit(code); String

22、8 mClassName; Vector mArgs; jclass mClass; ; 回到上面的main函数中,由于我们在init.zygote64_32.rc文件中,设置了app_process启动参数-zygote和-start-system-server,因此,在main函数里面,最终会执行下面语句:java view plain copy 在CODE上查看代码片派生到我的代码片runtime.start(com.android.internal.os.ZygoteInit, args, zygote); args参数,在上面main函数里定义的,从main函数可以看出还需要启动st

23、art-system-server,这个方法的实现具体如下java view plain copy 在CODE上查看代码片派生到我的代码片frameworks/base/core/jni/AndroidRuntime.cpp void AndroidRuntime:start(const char* className, const Vector& options, bool zygote) ALOGD( START %s uid %d n, className != NULL ? className : (unknown), getuid(); static const String8 st

24、artSystemServer(start-system-server); /* * startSystemServer = true means runtime is obsolete and not run from * init.rc anymore, so we print out the boot start event here. */ for (size_t i = 0; i options.size(); +i) if (optionsi = startSystemServer) /* track our progress through the boot sequence *

25、/ const int LOG_BOOT_PROGRESS_START = 3000; LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC); const char* rootDir = getenv(ANDROID_ROOT); if (rootDir = NULL) rootDir = /system; if (!hasDir(/system) LOG_FATAL(No root directory specified, and /android does not exist.); r

26、eturn; setenv(ANDROID_ROOT, rootDir, 1); /const char* kernelHack = getenv(LD_ASSUME_KERNEL); /ALOGD(Found LD_ASSUME_KERNEL=%sn, kernelHack); /* start the virtual machine */ JniInvocation jni_invocation; jni_invocation.Init(NULL); JNIEnv* env; if (startVm(&mJavaVM, &env, zygote) != 0) return; onVmCreated(env); /* * Register android functions. */ if (startReg(env) FindClass(java/lang/String); assert(stringClass != NULL); strArray = env-NewObjectArray(options.size() + 1, stringClass, NULL); assert(strArray != NULL); classNameStr = env-NewStringUTF(className); assert(classNameStr !=

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2