Wednesday, July 30, 2014

Zygote,Activity Manager,Dalvik VM

Zygote (Dalvik VM Launcher)
Zygote is a daemon whose only goal is to launch Apps. It is started by app_process. Here is the sequence used to start such special processLets see the init.rc file for the start of Zygote:

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-serverclass mainsocket zygote stream 660 root systemonrestart write /sys/android_power/request_state wakeonrestart write /sys/power/state ononrestart restart mediaonrestart restart netd

When app_process launches Zygote, it creates the first 'Dalvik VM' and calls Zygote’s main () method. Once Zygote starts, it preloads all necessary Java classes and resources, starts System Server (system_server) as mentioned in the previous section and opens a socket '/dev/socket/zygote' to listen for requests for starting applications. System Server is a complete detached process from it’s parent. 

How a New App is started?
Zygote receives a request to launch an App through '/dev/socket/zygote'. Zygote then trigger a fork() creating a clone of it self in another memory space. Now in the main Zygote all the initialization (Loading Dalvik and loading java classess) is done so this clone is effecient and fast. This makes the process of creating a VM and load resources pretty efficiently. Actually during the fork or clone call linux implements Copy on Write principle. So in reality no different memory is actually copied to the other memory space. The memory is shared and marked Copy on Write. When there is a write request to the memory only then the memory is copied. So this process is fast. Now can you see the benefit of using Linux kernel? We get all these implements by default. 

Activity Manager
Zygote will launch an application when it is requested. Like when we tap an application then it launches it on the onClick() event. At this point the application launcher contacts the Activity Manager. This is done through a Remote Procedure Call (RPC) mechanism called 'Binder' and calles startActivity(). On request for startActivity() the Activity Manager request the Zygote to fork itself and start the new VM as we discussed in the last section. This is done via startViaZygote() method which will open a connection to the Zygote socket (/dev/socket/zygote).Apart from this activity manager is responsible for other tasks as intent broadcasting, launching the "Application Not Responding" message box.To know more about the activity manager you can refer the code.Now that we know briefly about the android internals we will talk about the important aspect of android. This is the Dalvik VM.

Dalvik VM
Dalvik is Android’s Java virtual machine. It allows Android to run the byte-code generated from Java-based apps and exports android components and jni hooks to native libraries and the rest of the native user-space. Dalvik was design for embedded systems. Those systems would have a small amount of RAM, slow CPU and run an OS without swap space and limited battery. As we discussed earlier Dalvik translates the 'class' files to 'dex' files and executes. These 'dex' files can be upto half the size of the corresponding jar files.There is also difference in the internal structure of the dalvik vm from the JVM. JVM is a stakc based VM but Dalvik is a register based VM. Memory footprint in Dalvik is saved via few techniques as minimal repetition, implicit typing and implicit labeling. The Dalvik VM relies on the Linux kernel for underlying functionality such as threading and low-level memory management. As of android 2.2 Dalvik also has a Just in Time compiler.

GREP,PGREP,EGREP and FGREP

grep
grep is an acronym that stands for "Global Regular Expressions Print". grep is a program which scans a specified file or files line by line, returning lines that contain a pattern. A pattern is an expression that specifies a set of strings by interpreting characters as meta-characters. For example the asterisk meta character (*) is interpreted as meaning "zero or more of the preceding element". This enables users to type a short series of characters and meta characters into a grep command to have the computer show us what lines in which files match.The standard grep command looks like:grep  '' grep prints the search results to the screen (stdout) and returns the following exit values:0    A match was found.1    No match was found.>1   A syntax error was found or a file was inaccessible      (even if matches were found).Some common flags are: -c for counting the number of successful matches and not printing the actual matches, -i to make the search case insensitive, -n to print the line number before each match printout, -v to take the complement of the regular expression (i.e. return the lines which don't match), and -l to print the file names of files with lines which match the expression.

egrep
egrep is an acronym that stands for "Extended Global Regular Expressions Print".The 'E' in egrep means treat the pattern as a regular expression. "Extended Regular Expressions" abbreviated 'ERE' is enabled in egrep. egrep (which is the same as grep -E) treats +, ?, |, (, and ) as meta-characters.In basic regular expressions (with grep), the meta-characters ?, +, {, |, (, and ) lose their special meaning. If you want grep to treat these characters as meta-characters, escape them \?, \+,\{, \|, \(, and \).For example, here grep uses basic regular expressions where the plus is treated literally, any line with a plus in it is returned.grep "+" myfile.txtegrep on the other hand treats the "+" as a meta character and returns every line because plus is interpreted as "one or more times".egrep "+" myfile.txtHere every line is returned because the + was treated by egrep as a meta character. normal grep would have searched only for lines with a literal +.

fgrep
fgrep is an acronym that stands for "Fixed-string Global Regular Expressions Print".fgrep (which is the same as grep -F) is fixed or fast grep and behaves as grep but does NOT recognize any regular expression meta-characters as being special. The search will complete faster because it only processes a simple string rather than a complex pattern.For example, if I wanted to search my .bash_profile for a literal dot (.) then using grep would be difficult because I would have to escape the dot because dot is a meta character that means 'wild-card, any single character':grep "." myfile.txtThe above command returns every line of myfile.txt. Do this instead:fgrep "." myfile.txtThen only the lines that have a literal '.' in them are returned. fgrep helps us not bother escaping our meta characters.

pgrep
pgrep is an acronym that stands for "Process-ID Global Regular Expressions Print".pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. pgrep is handy when all you want to know is the process id integer of a process. For example, if I wanted to know only the process ID of my mysql process I would use the command pgrep mysql which would return a process ID like 7312.

GREP ?

The grep utilities are a family of Unix tools, including grep, egrep, and fgrep, that perform repetitive searching tasks. The tools in the grep family are very similar, and all are used for searching the contents of files for information that matches particular criteria. For most purposes, you'll want to use fgrep, since it's generally the fastest.

The general syntax of the grep commands is:
  grep [-options] pattern [filename]
You can use fgrep to find all the lines of a file that contain a particular word. For example, to list all the lines of a file named myfile in the current directory that contain the word "dog", enter at the Unix prompt:
  fgrep dog myfile
This will also return lines where "dog" is embedded in larger words, such as "dogma" or "dogged". You can use the -w option with the grep command to return only lines where "dog" is included as a separate word:
  grep -w dog myfile
To search for several words separated by spaces, enclose the whole search string in quotes, for example:
  fgrep "dog named Checkers" myfile
The fgrep command is case sensitive; specifying "dog" will not match "Dog" or "DOG". You can use the -i option with the grep command to match both upper- and lowercase letters:
  grep -i dog myfile
To list the lines of myfile that do not contain "dog", use the -v option:
  fgrep -v dog myfile
If you want to search for lines that contain any of several different words, you can create a second file (named secondfile in the following example) that contains those words, and then use the -f option:
  fgrep -f secondfile myfile
You can also use wildcards to instruct fgrep to search any files that match a particular pattern. For example, if you wanted to find lines containing "dog" in any of the files in your directory with names beginning with "my", you could enter:
  fgrep dog my*
This command would search files with names such as myfile, my.hw1, and mystuffin the current directory. Each line returned will be prefaced with the name of the file where the match was found.

By using pipes and/or redirection, you can use the output from any of these commands with other Unix tools, such as more, sort, and cut. For example, to print the fifth word of every line of myfile containing "dog", sort the words alphabetically, and then filter the output through the more command for easy reading, you would enter at the Unix prompt:
  fgrep dog myfile | cut -f5 -d" " | sort | more
If you want to save the output in a file in the current directory named newfile, enter:
  fgrep dog myfile | cut -f5 -d" " | sort > newfile

Interface ?

Interface: It is a "existing entity" layer between the functionality  and consumer of that functionality. An, interface by itself is doesn't do anything. It just invokes the functionality lying behind.
Now depending on who the user is there are different type of interfaces.
Command Line Interface(CLI) commands are the existing entities, consumer is the user and functionality lies behind.
functionality: my software functionality which solves some purpose to which we are describing this interface.
existing entities: commands
consumer: user
Graphical User Interface(GUI) window,buttons etc.. are the existing entities, again consumer is the user and functionality lies behind.
functionality: my software functionality which solves some purpose to which we are describing this interface.
existing entities: window,buttons etc..
consumer: user
Application Programming Interface(API) functions or to be more correct, interfaces (in interfaced based programming) are the existing entities, consumer here is another program not a user. and again functionality lies behind this layer.
functionality:  my software functionality which solves some purpose to which we are describing this interface.
existing entities: functions, Interfaces(array of functions).
consumer: another program/application.