Android FrameWork之SytemServer进程fork示例

来自:网络
时间:2023-07-25
阅读:
目录

1、Linux的fork

在Linux平台我们可以通过fork系统调用来创建一个新的进程,这个新的进程将会拥有原始进程的一份副本,包括代码、数据、内存等等。唯一的区别是新的进程拥有一个新的ID,使得它成为一个独立的进程,运行自己的代码。

fork()系统调用会返回两次,在原始进程中会返回进程ID,在新的进程中会返回0。两个进程可以执行相同的任务,也可以按照需要执行不同的代码。

fork的例子

#include <stdio.h>
#include <unistd.h>
int main(){
  printf("main start \n");
  int result = fork();
  pid_t pid = getpid();
  printf("pid is %d and result is %d\n",pid,result );
  return 0;
}

main start 
pid is 11647 and result is 11648
pid is 11648 and result is 0

我们看到main start只打印了一次,子进程从fork之后开始分叉执行。

第二行当前pid是11647 ,fork的结果是11648,表示当前在父进程执行

第三行当前pid是11648,恰好是父进程fork返回的进程ID,result的结果是0,也说明了本次的打印是在子进程中执行的。

2、SystemServer进程的fork

接着分析上次的ZygoteInit的源码。

frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

  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.
              //如果是在system_server进程中执行,则返回Runnable ,并执行r.run函数
                if (r != null) {
                    r.run();
                    return;
                }
            }
    /**
     * Prepare the arguments and forks for the system server process.
     *
     * Returns an {@code Runnable} that provides an entrypoint into system_server code in the
     * child process, and {@code null} in the parent.
     */
private static Runnable forkSystemServer(String abiList, String socketName,
            ZygoteServer zygoteServer) {
       int pid;
        try {
            parsedArgs = new ZygoteConnection.Arguments(args);
            ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
            ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
            /* Request to fork the system server process */
            //在这里请求fork  system server  进程
            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 */
      //如果pid为0,说明是在子进程中执行,因此关掉zygote进程继承下来的Socket
        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }
            zygoteServer.closeServerSocket();
            return handleSystemServerProcess(parsedArgs);
        }

调用Zygote类的forkSystemServe函数请求fork子进程,如果pid为0,说明是在子进程中执行,因此关掉zygote进程继承下来的Socket。

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;
    }
  native private static int nativeForkSystemServer(int uid, int gid, int[] gids, int debugFlags,
           int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);

在forkSystemServer函数中又调用了nativeForkSystemServer方法,这里要通过JNI调用Native函数

frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
jint com_android_internal_os_Zygote_nativeForkSystemServer

ForkAndSpecializeCommon

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);
  if (pid > 0) {
    .....
  }
  return pid;
}
// 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();
.....
}

在ForkAndSpecializeCommon函数中我们看到了fork()的系统调用,这样system server进程就会被fork出来了。

而在最开始的代码我们看到最终调用forkSystemServer之后返回了一个Runnable r对象。

      Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
          // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
                // child (system_server) process.
              //如果是在system_server进程中执行,则返回Runnable ,并执行r.run函数
                if (r != null) {
                    r.run();
                    return;
                }

如果是在system_server进程中执行的话会返回一个Runnable r对象,并执行r.run()函数,也就是之后system_server进程的执行主要流程就在Runnable 的run函数中

以上就是Android FrameWork之SytemServer进程fork示例的详细内容,更多关于Android FrameWork SytemServer的资料请关注其它相关文章!

返回顶部
顶部