在线学习课程,课程咨询答疑和新课信息:QQ交流群:422901085进行课程讨论
转自于:https://blog.csdn.net/learnframework/article/details/116244848
课程答疑和新课信息:QQ交流群:422901085进行课程讨论
FrameWork入门课视频链接:https://edu.csdn.net/course/detail/30298
FrameWork实战课1视频链接:https://edu.csdn.net/course/detail/30275
专题博客系列:
Android 8.1 zygote 启动过程源码
Android Framework实战视频--Zygote的fork进程篇
Android Framework实战视频--SystemServer启动篇
Android Framework实战视频--SystemServer启动FallbackHome篇
Android Framework实战视频--FallbackHome进程启动及Activity启动篇
Android Framework实战视频--FallbackHome结束启动Launcher篇
直接上代码体验fork桌面创建新的子进程
//fork.c
#include <unistd.h> #include <stdio.h> int main(void) { int pid; int count2=0; printf("main current process pid == %d \n",getpid()); pid=fork(); if (pid == 0) {//子进程,其中getpid()代表获取当前进程号,getppid()表示获取当前父进程号 printf("child process pid == %d ppid == %d \n",getpid(),getppid()); } else { printf("this process current pid == %d pid = %d ppid == %d \n",getpid(),pid,getppid()); } while(1); return 0; }
因为属于linux的代码,只需要在ubuntu上运行既可以:
gcc fork.c -o fork
然后执行:./fork
输出如下:
main current process pid == 5319 this process current pid == 5319 pid = 5320 ppid == 5308 child process pid == 5320 ppid == 5319
可以看出调用fork以前只有一个进程 pid = 5319
但是在fork执行后:多了一个进程号 5320
就会有2个进程的打印,即代码中的if和else都执行了,因为fork执行后就有两个进程分别执行了。
其中fork的返回值如果为:0说明当前就是子进程执行,为子进程的pid就说明是原来的父进程
frameworks/base/core/java/com/android/internal/os/ZygoteInit.java中main方法里面的如下代码
if (startSystemServer) { Runnable r = forkSystemServer(abiList, socketName, 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; } }
核心就是forkSystemServer方法
private static Runnable forkSystemServer(String abiList, String socketName, ZygoteServer zygoteServer) { //省略。。 int pid; try { parsedArgs = new ZygoteConnection.Arguments(args); //省略。。 /* Request to fork the system server process */ //核心方法 pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } /* For child process */ if (pid == 0) { if (hasSecondZygote(abiList)) { waitForSecondaryZygote(socketName); } zygoteServer.closeServerSocket(); return handleSystemServerProcess(parsedArgs); } return null; }
分析发现主要调用:
Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities);
这个从名字看相对也贴近刚开始学的fork了。继续看Zygote.forkSystemServer
public static int forkSystemServer(int uid, int gid, int[] gids, int debugFlags, int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { //省略 int pid = nativeForkSystemServer( uid, gid, gids, debugFlags, rlimits, permittedCapabilities, effectiveCapabilities); //省略 return pid; }
这里调用的又是nativeForkSystemServer,nativeForkSystemServer属于jni调用:
代码路径:
frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
static jint com_android_internal_os_Zygote_nativeForkSystemServer( JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities, jlong effectiveCapabilities) { pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, permittedCapabilities, effectiveCapabilities, MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL, NULL, NULL, NULL); //省略 return pid; }
调用到了ForkAndSpecializeCommon方法:
// Utility routine to fork zygote and specialize the child process. static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, jint debug_flags, jobjectArray javaRlimits, jlong permittedCapabilities, jlong effectiveCapabilities, jint mount_external, jstring java_se_info, jstring java_se_name, bool is_system_server, jintArray fdsToClose, jintArray fdsToIgnore, jstring instructionSet, jstring dataDir) { //省略 pid_t pid = fork(); if (pid == 0) { //省略 } else if (pid > 0) { //省略 } return pid; }
ForkAndSpecializeCommon方法本身非常长,但是看到这里有一个 pid_t pid = fork();
大家心里有没有非常非常激动啊
这里就是真正创建子进程的地方,和刚开始一样讲解实战的fork程序是不是一样啊
好了,那到这里相信大家已经清楚了zygote怎么一步步的创建出新的进程