How to start a new process for Android?
来源:互联网 发布:非遗文化知多少 编辑:程序博客网 时间:2024/06/04 23:36
We need start a new process when we tap application launcher or start a new service which is in a different process. This artical will describe how a new process is created but no matter whoever the caller is.
1.startProcessLocked in ActivityManagerService.java
Ignore rest of the function and focus on below code.
According to above code,we can find that another process created with a nice name "app.processName" or NULL where the first args is the first class started by the new process.
Now, we look into the start function of the Process class.
2.startViaZygote in Process.java
The first sentence means that we need to init runtime when create this Process, the purpose of this initialization will be discussed later.
We should know the communication between ActivityManagerService and zygote relies on socket, AM writes the arguments of the new process into the buffer for zygote socket.
Then the starter's work is done, let's turn over into the zygote at looked at the other socket communication side.
3.
The zygote process will be in a loop in order to detect any connect into the zygote socket and fork the request process after being started by init deamon process, all this work is running in function runSelectLoopMode in ZygoteInit.java and calls runOnce function in ZygoteConnection.java to fork new process.
4.runOnce in ZygoteConnection.java
Above code forks a new process, It is very easy to understand and unnecessary to look into.
As we know, parent process and child process will execute the code simultaneously after fork operation, therefore, the parent process will get the real pid of child process and call handleParentProc method, meanwhile, the child process will get a zero pid value and call handleChildProc.
We ignore the handleParentProc in which there is nothing important but cleanup of parent process.
The belowing operations are in the new process.
5.handleChildProc in ZygoteConnection.java
Function handleChildProc will check if the process starter needs the runtime initialization which is set in step 2. Here need to init runtime while every process is being created.
6.zygoteInit in RuntimeInit.java
6.1 zygoteInitNative
This function is a native function which spawns a pool thread to detect binder IPCs. Its prototype in JNI layer is underlying:
gCurRuntime is a global variable which is initialized when app_main starts. We can find this process in AndroidRuntime constructor. So we confirm that the gCurRuntime is an AppRuntime instance and class AppRuntime extends AndroidRuntime.According to all the facts, we can conclude the onZygoteInit function belongs to class AppRuntime.
6.2 invokeStaticMain
After creating process and corresponding pool thread for binder IPC, the last job here is to call the "main" method of the process's first class. It should be "android.app.ActivityThread" for AM to start a new activity or service of different processes.
ActivityThread instance is the main thread of the new process.
7.main method in ActivityThread.java
In above code, we can not find any sentence create the new activity or service. where is the operation hiding?
The implementation is very complicated, the above code has a sentence "thread.attach(false)" where all the stuff is hiding.
I will discuss how activity starts in later artical.
- How to start a new process for Android?
- How to start a new process for Android
- How to start a new process for Android?
- How to start a new process for Android?
- How to create a new process
- how to start a new goal
- android start new process
- How to Start a New Cocos2d-x Game for version 3.0
- How to Start a New Cocos2d-x Game
- How to new an emulator for Android?
- Beginner's Guide For How To Start A Startup
- How to spawn a process
- A new start for me
- How to tune a new closeloop vcm, for instance, LC898214?
- How to add a new keycode in android
- How to add a new keycode in android
- How to add a new key on android
- How to set up a multi-domain SSO demonstration for a quick start with CAS.
- 绿色软件联盟:杀毒软件已经进入空前弱智与混沌状态
- sicily 1818 成绩转换
- 判断字符串是否包含另一个字符串
- Tcpdpriv,你让我情何以堪?!
- C# 中 post ,get实例,不错,很详细
- How to start a new process for Android?
- 世界名牌大学的开放教育链接资源
- 数据结构
- ural 1010. Discrete Function
- CEDIT 背景色
- C语言注释
- csdn是个好平台
- cvLoadImage 与 CvvImage.load
- ural 1136. Parliament