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.
分享到:
相关推荐
VTK 12 How to Write a Process__ Object.pdf VTK 12 How to Write a Process__ Object.pdf VTK 12 How to Write a Process__ Object.pdf
Using Gradle makes it easy for Android developers to manage dependencies and set up the entire build process., This book begins by taking you through the basics of Gradle and how it works with ...
VB 以其他身份(管理员)启动程序 This demo will show you how to start a process as a different user. This demo will only work with WinNT 4.0/2k and Later
这是一篇关于如何找到一份体面的工作的英文作文,里面语法非常丰富,是作者的呕心之作
How to Write a Good Scientific Paper by Chris A. Mack is a valuable resource for all students and faculty in science and engineering. SPIE is pleased to make it available here at no charge as a PDF ...
How to Write a plugin for JMeter
How to Design a Business Process Diagram.pdf
42 Reasons To Start a Business Analyst Career
How to compile Amalgamation for android
As there are a lot questions about "how to disable home button in android?" on Stack Overflow, such as how to disable home button in android? Android - Is It possible to disable the click of home ...
How to Find Information - A guide for researchers
How To Automate Excel to Create And Format a New Workbook Using C++ .NET and MFC
How to come up with new ideas 一位计算机图形学和图像处理领域的牛人做的关于寻找新想法的报告
Learn how to use the Processing programming language and environment to create Android applications with ease. This book covers the basics of the Processing language, allowing users to effectively ...
Think Java: How to Think Like a Computer Scientist by Allen B. Downey, Chris Mayfield 2016 | ISBN: 1491929561 Currently used at many colleges, universities, and high schools, this hands-on ...
Strengthens your existing skills of writing applications and shows you how to transfer your talents to building Android apps with Mono for Android and .NET/C# Dives into working with data, REST, SOAP,...
In cryptography, a shared secret is a piece of data, known only to the parties involved, in a secure communication. The shared secret can be a password, a passphrase, a big number or an array of ...
Now updated to include both Android 4.4 and the new Android L, Android Apps for Absolute Beginners, Third Edition takes you through the process of getting your first Android apps up and running using...