Android architecture-software self-healing capability (2)

Source: Internet
Author: User

Special statement: this series of articlesLianlab.orgCopyright ownership. For more information, see the source. By @ song Baohua Barry

In the last book, we said that the init in Android will monitor the service started in init. RC and restart it as needed. In this book, we will talk about zygote and systemserver in Android.

Second dog: Dog Zhong Ba Gong

The world of the present is not prosperous, and the evil spirits are full. As a result, all kinds of hard-to-understand actions are endless. after the collision, there are those who use the Tianya Mingyue knife to kill, and after the collision, the female teacher is naked to block the rescue staff, what kind of extraordinary sadness is this? Respect for the right to life is actually the most fundamental human nature, killing the same kind of people, seeing the rescue of death and blocking, is actually the loss of the most fundamental human nature. Tears in the heart, merged into a river. We used to kill people in the name of an infinitely lofty doctrine, an extremely just revolution, and trample on the most basic dignity of people. The consequence of the tragedy is that the hot blood has become a thing of the past, and the passion is also indifferent, the doctrine is still vast, but humanity is ruined.

Human nature is exhausted, and dog nature is still like the wind in the Pacific Ocean. In this book, we say that the second dog is a loyal dog. On October 16, May 1925, the master of October 8 died of illness. However, October 8 still went to the Shibuya station every day to wait for the return of the master. Until the last day. October, October, died due to loose dermatitis. Ten years of life and death, not thinking, never forget. This is the case for loving people. In this world where weisan is popular, what kind of people still cherish the responsibility of marriage and their original vows?

In this book, we will talk about the coexistence of zygote and systemserver, holding the hand, and interpreting this loyal love with the old child, zygote and systemserver. When they die, if we have an afterlife, we will be together.

Zygote is the core of the Android system, the meaning of the fertilized eggs, the ancestor of the android framework family. What about her mother's surname? It is the init mentioned in the previous book. Zygote is a producer in the Java World and the android nvwa. She uses runselectloopmode () to continuously listen to startup requests from applications through activitymanagerservice and fork corresponding processes. Systemserver is the core value in the Android world. surfaceflinger and Java services, such as powermanagerservice, windowmanagerservice, and activitymanagerservice, are all started by him and become part of him, run in the systemserver process space. It can be said that the crash of systemserver basically means the crash of Android framework. Systemserver is the marshal of arms for Android. Basically, in the Android world, only the other party can match systemserver and zygote. After the battle of systemserver, zygote should be full of errors, but there is no need to live.

In Android, systemserver is forked by zygote. The relevant code is located in Dalvik/Vm/native/dalvik_system_zygote.c:

static void Dalvik_dalvik_system_Zygote_forkSystemServer(        const u4* args, JValue* pResult){    pid_t pid;    pid = forkAndSpecializeCommon(args, true);    /* The zygote process checks whether the child process has died or not. */    if (pid > 0) {        int status;        LOGI("System server process %d has been created", pid);        gDvm.systemServerPid = pid;        /* There is a slight window that the system server process has crashed         * but it went unnoticed because we haven't published its pid yet. So         * we recheck here just to make sure that all is well.         */        if (waitpid(pid, &status, WNOHANG) == pid) {            LOGE("System server process %d has died. Restarting Zygote!", pid);            kill(getpid(), SIGKILL);        }    }    RETURN_INT(pid);}

Among them, forkandspecializecommon () will fork out of systemserver, zygote will immediately use waitpid (PID, & status, wnohang) to wait for the systemserver to die, note that the parameter is wnohang, that is to say, wait until the system server exits, and waitpid () will not be blocked. If the systemserver is found dead, it will call kill (getpid (), sigkill) unconditionally. This instant wait is followed by comments to prevent the system server PID from being noticed before the code that really waits for the systemserver to die in a very small period of time window. Sigkill is a signal that cannot be ignored, which is different from the SIGINT corresponding to Ctrl + C.

Some may ask, a process kill itself inexplicably, and sometimes we forcibly use the kill command to kill the Linux Process. At this time, the originally applied memory or something, isn't it leaked? For example, I have malloc some memory and I am not free when you killed me. Isn't the memory exposed? I have already repeatedly said in speeches from various companies that memory leaks are divided into two realms: one is that people are dead, and the money is not enough, your malloc memory is dead before the process is released. We say that this problem does not exist in Linux, and the process is a resource encapsulation unit. When the process fails, resources will be released by the kernel, and there will be only a zombie in the dead. The second realm is that people are alive and money is gone. This is what Linux really worried about. A multi-threaded program repeatedly applies for and releases memory during the running process, however, if the released memory does not match the applied memory, the memory consumption curve of the process gradually increases until the memory is exhausted. Therefore, in the Linux world, we don't have to worry about the endless cost of money.
The party will help us deal with it. What we need to worry about is that people are living and money is not enough.

The HTC Android mobile phone is claimed to be able to start quickly in one second. According to our tracking, the process is killed when the phone is shut down, suspend, and then resume is back, because the previous processes are killed, so you see a clean desktop. Therefore, Shenma is a cloud. If you withdraw Huangyan Island, the oil price will rise. They will say that we have spent money to withdraw it.

Just now, you said that zygote and systemserver live and live together. Why did you wait? Isn't it responsible for a lifetime? Let's go back to forkandspecializecommon () and continue mining:

static pid_t forkAndSpecializeCommon(const u4* args, bool isSystemServer){   …    setSignalHandler();    dvmDumpLoaderStats("zygote");    pid = fork();    if (pid == 0) {        int err;        /* The child process */        …    }    return pid;}

The setsignalhandler () sets the sigchld signal processing function, and this signal processing function determines whether the systemserver is dead. If yes, it will commit suicide:

static void setSignalHandler(){    int err;    struct sigaction sa;    memset(&sa, 0, sizeof(sa));    sa.sa_handler = sigchldHandler;    err = sigaction (SIGCHLD, &sa, NULL);    if (err < 0) {        LOGW("Error setting SIGCHLD handler: %s", strerror(errno));    }}static void sigchldHandler(int s){    pid_t pid;    int status;    while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {        …        /*         * If the just-crashed process is the system_server, bring down zygote         * so that it is restarted by init and system server will be restarted         * from there.         */        if (pid == gDvm.systemServerPid) {            LOG(LOG_INFO, ZYGOTE_LOG_TAG,                "Exit zygote because system server (%d) has terminated\n",                (int) pid);            kill(getpid(), SIGKILL);        }    }    …}

When systemserver died, zygote's Sedan Chair passed by in front of its grave. When zygote walked out of the sedan chair, it took off the red dress and went slowly to the grave, kneeling down and crying, the thunder was so loud that the grave was split. zygote seemed to have seen the gentle face of systemserver, and she jumped in with a smile. Then there was a loud bang, and the grave was closed. In this time, the wind is scattered, and the rain is fine, and all kinds of wild flowers are gently swaying in the wind. A pair of beautiful butterflies fly out of the grave and dance freely in the sun.

When zygote also dies, zygote has a registered account, and the first dog in the last book will shoulder its mission and restart zygote. As a result, systemserver will restart with zygote, so there will be no separation in the future.

It should be particularly noted that when zygote dies, wait_for_one_process () in the init in the previous book will send sigkill to "-zygote_pid" to kill the process group corresponding to zygote, therefore, the entire Java World has ended:

static int wait_for_one_process(int block){    ...    while ( (pid = waitpid(-1, &status, block ? 0 : WNOHANG)) == -1 && errno == EINTR );    ...    if (!(svc->flags & SVC_ONESHOT)) {        kill(-pid, SIGKILL);        NOTICE("process '%s' killing any children in process group\n", svc->name);    }   ...}

Almost all Java applications depend on services in systemserver. If systemserver crashes, zygote will not die and will not cause the whole Java World to die, in fact, the system has no way to restore the status to the status before the systemserver crash, so the status of each service in the systemserver seen by each APK cannot be restored, therefore, the entire Java World is the only choice when it dies and restarts.

This is what we will talk about here. Next we will talk about the dog of systemserver. Let's take a look at the future.

I would like to dedicate this time to all lovers in the world. The infinite pursuit of money and material leads us to lose our way. The true happiness is the constant life of your lover.

 

Lovesickness is like a sea, old things are like a sky.
Thousands of lines in tears make people break down.
If you have no reason to see it, it will be hard to see it.
If there is no reason for the previous generation, it will be closed and the future will be ready.

-- Song Baohua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.