If you do not know about the fork mechanism, you can first look at this blog from a interview question to talk about the running mechanism of fork in Linux.
This article
Jollen
Output at 2017l 5, 2010 pm
When the Android operating system is turned on, init. RC will launch many external programs. One of the most important processes is zygote. Zygote is the monitor process of Android, which is mainly used by zookeeper:
1. Activate system server
2. Click the Android Application
"System server" is another process created by zygote. The system server creation method is typical.
Linux System Call-Fork (). After zygote successfully establishes the system server, it enters the socket
Listening mode. In this mode, zygote will listen to the "command" written by the socket (Listen), and update it according to the internal volume of the response command.
Android Application.
Zygote uses the fork () system provided by Linux kernel to activate external Android applications.
Call. Therefore, listening is performed on the socket and fork () is executed based on the prepare command, and external Android applications are published.
Mode 」.
An important process in Android system applications is zygote. All Java application processes are derived from zygote. The role of zygote is
Is a "son ". For more information about how an app comes out, see my previous article-Android app initialization and form event distribution.
First, you need to understand the basic knowledge of the initialization language:
A service is a program that is started during initialization and restarted upon exit (optional ). Services are as follows:
Service <Name> <pathname> [<argument>] *
<Option>
<Option>
Options is an option. For more information, see Linux service commands.
The zygote process is started through this file after Linux kernel startup. For details, see the section in init. RC:
Service zygote/system/bin/app_process-xzygote/system/bin -- zygote -- start-system-Server
Socket zygote stream 666
Onrestart write/sys/android_power/request_state wake
Onrestart write/sys/power/State on
A piece of analysis code:
Service zygote/system/bin/app_process-xzygote/system/bin -- zygote -- start-system-Server
The binfile path of the startup service name zygote./system/bin/app_process process, followed by the startup parameter-xzygote/system/bin -- zygote
-- Start-system-Server
. What is the function of this parameter? Check the code app_main.cpp.
If (I <argc ){
Arg = argv [I ++];
If (0 = strcmp ("-- zygote
", ARG )){
Bool startsystemserver = (I <argc )?
Strcmp (argv [I], "--
Start-system-Server
") = 0: false;
Setargv0 (argv0, "zygote ");
Set_process_name ("zygote ");
Runtime. Start
("Com. Android. Internal. OS. zygoteinit ",
Startsystemserver );
} Else {
Set_process_name (argv0 );
Runtime. mclassname = ARG;
// Remainder of argS get passed to startup class main ()
Runtime. margc = argc-I;
Runtime. margv = argv + I;
Logv ("app process is starting with PID = % d, class = % S./N ",
Getpid (), runtime. getclassname ());
Runtime. Start ();
}
}
-Xzygote
The significance of this parameter is to set gdvm. zygote = true in JVM. For the specific function of this parameter, let's look at the code. The process is
Androidruntime-> Start () call ---> jni_createjavavm () call
----> Dvmstartup () (Davy VM initialization) Call ----> dvmprocessoptions ().
/System/bin: Maybe the path of the system application. If you see it, let me know.
Onrestart write/sys/android_power/request_state wake:
If the service is restarted, open the/sys/android_power/request_state file and write the wake string.
Zygote Service
In this chapter, we will see the two words:
• Zygote [Creature] fertilized eggs, joints, and fit
• Spawn: spawning
With these two words, We can generally know what zygote is.
It's called the old hen. Different sub-"zygote" are generated through "zygote ". In terms of big architecture, zygote is a simple and typical C/S structure. Other processes
A client sends an "incubated" request to zygote. When zygote receives the command, it "incubated" an activity process.
Zygote System Code Composition and calling structure:
• Zygote. Java
Provides interfaces to access Dalvik "zygote. It is mainly used to package fork in Linux to create a new VM instance process.
• Zygoteconnection. Java
Connection Management of zygote sets of interfaces and Parameter Parsing. Other actvitiy Process Creation requests send command parameters to zygote through a set of interfaces.
• Zygoteinit. Java
The main function entry of zygote.
Zygote system code hierarchy call
Main ()
Startsystemserver ()...
Runselectloopmode ()
Accept socket connection
Conntecion. runonce ()
Read argument
Folkandspecialize
Folkandspecialize uses the native function dalvik_dalvik_system_zygote_forkandspecialize
// Obtain native
Dalvik/Vm/native
// Dalvik_system_zygote.c
Const dalviknativemethod dvm_dalvik_system_zygote [] = {
{"Fork", "() I ",
Dalvik_dalvik_system_zygote_fork },
{"Forkandspecialize", "(II [II [[I) I ",
Dalvik_dalvik_system_zygote_forkandspecialize },
{"Forksystemserver", "(II [II [[I) I ",
Dalvik_dalvik_system_zygote_forksystemserver },
{Null, null, null },
};
Here we have a full understanding of the zygote service and confirmed in the code that activity is essentially a Linux Process. However
Is it a simple Linux Process? After all, activity is valid in the Concept Space of andoid. In this concept space, the activity is encapsulated and displayed on the screen.
UI, the entire screen or window that the user sees. actvitiy is displayed in front of the user for the machine. From the analysis, we can see that android uses the Linux
Fork mechanism. In Linux, fork is very efficient.
An android activity is actually a Linux Process. The so-called process has the following elements,
A. a program can be shared by multiple processes to run a program.
B. Process-specific system stack space.
C. Process Control Block. in Linux, the specific implementation is task_struct.
D. Have independent storage space.
Fork
The created sub-process copies the resources of the parent process, including the memory content task_struct. During the replication process, the sub-process copies the task_struct of the parent process, and the system stack is empty.
When the sub-process changes the variable of the parent process, a new copy is created for the involved page by copy_on_write. So only when the sub-process has a variable change,
The sub-process creates a page to copy the content of the original page. The replication of basic resources is required. It seems like the Independent bucket of the parent process is also copied.
Let's take a look at the pictures of the Dalvik Virtual Machine explained by Google below. We have a basic understanding of the actual acting status of actvitiy In the Android system.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/maxleng/archive/2010/04/20/5508488.aspx