Daemons are a product of the black zone. Whether it is forked in Linux native or daemons in Java, it is not very friendly. According to many people, there are always some practical business scenarios where they want their applications to stay in the live state. One is to do it in native:

  • Multiprocess in Linux;

  • Unix Domain sockets for cross-process communication;

  • Linux signal processing;

  • The usage of exec function family;

Put them together to implement a two-process daemon. A few key points to implement a two-process daemon:

1. How does the parent monitor the death of the child?

  • Very simply, in Linux, when the child process terminates, SIG_CHLD is sent to the parent process, so we can install the signal handler and restart the create monitor process in this signal handler.

2. How does the child process monitor the death of the parent process?

  • When the parent dies, the child becomes an orphan and is adopted by Init, so we can read the parent PID in a loop, and when it becomes 1, the parent is dead, so we can restart the parent. The use of circulation here brings up the issue of power consumption mentioned earlier.

3. Communication between parent and child processes

  • There is a kind of method is to establish a communication channel between a parent and child process, and then get to know each other, by monitoring the channel that does not exist the aforementioned power consumption problems, in this paper, the implementation of, for the sake of simplicity, or using the polling the parent PID method, but still leave the process of parent-child communication channel, although not used temporarily, but can be a rainy day!

This way of native, reference links: dearseven.blog.163.com/blog/static…

Today I’ll take a complete example using two service daemons. For learning and communication purposes only. The two processes monitor each other and restart as soon as the other hangs! (Essentially, start another service during onDisconnected)

Suppose we have two services, A and B, enabled in our APP. If A guards B, then A should wake B up when B dies, and vice versa. That is to say, A and B should guard each other, no matter who is killed, the other will wake it up. Since two services are mentioned, they cannot be in the same process, or they will be double-killed. Obviously not in the same process, in Android we can usually use AIDL for IPC implementation.

Schematic diagram (simple version) :



Next, look at the code implementation

ServiceA.Java 

View pictures View pictures

ServiceB.Java

View pictures View pictures

MainActivity.java


View the image manifest.xml



Review images

IBridgeInterface.aidl 



Review images


Effect:



Review images



The complete code download link above: github.com/hejunlin201…

The first time to get blog updates reminder, and more Android dry goods, source code analysis, welcome to my wechat public number, scan the qr code below or long press to identify the QR code, you can pay attention to.

Review images