• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Obtaining Exit Information of Native Child Processes
2<!--Kit: Ability Kit-->
3<!--Subsystem: Ability-->
4<!--Owner: @SKY2001-->
5<!--Designer: @jsjzju-->
6<!--Tester: @lixueqing513-->
7<!--Adviser: @huipeizi-->
8
9## When to Use
10
11Starting from API version 20, parent processes can register a callback function to monitor child processes and obtain their abnormal exit information, facilitating further optimizations by the parent process. The child processes to be monitored must be created by calling [OH_Ability_StartNativeChildProcess](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_startnativechildprocess) or [startNativeChildProcess](../reference/apis-ability-kit/js-apis-app-ability-childProcessManager.md#childprocessmanagerstartnativechildprocess13).
12
13## Available APIs
14
15| Name                                                                                                                                                                                                                                                                                                                               | Description                                                                                   |
16| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
17| [Ability_NativeChildProcess_ErrCode](../reference/apis-ability-kit/capi-native-child-process-h.md#ability_nativechildprocess_errcode) [OH_Ability_RegisterNativeChildProcessExitCallback](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_registernativechildprocessexitcallback) ([OH_Ability_OnNativeChildProcessExit](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_onnativechildprocessexit) onProcessExit) | Registers a callback function for detecting abnormal exit of a native child process.|
18| [Ability_NativeChildProcess_ErrCode](../reference/apis-ability-kit/capi-native-child-process-h.md#ability_nativechildprocess_errcode) [OH_Ability_UnregisterNativeChildProcessExitCallback](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_unregisternativechildprocessexitcallback) ([OH_Ability_OnNativeChildProcessExit](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_onnativechildprocessexit) onProcessExit) | Unregisters the callback function used for detecting abnormal exit of a native child process.|
19
20## How to Develop
21
22
23**Linking Dynamic Libraries**
24
25```txt
26libchild_process.so
27```
28
29**Including Header Files**
30
31```c++
32#include <AbilityKit/native_child_process.h>
33```
34
351. (Main process) Register and unregister the callback for abnormal exit of native child processes.
36
37    Call [OH_Ability_RegisterNativeChildProcessExitCallback](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_registernativechildprocessexitcallback) to register a callback for detecting child process exits. If the return value is **NCP_NO_ERROR**, the registration is successful.
38    Call [OH_Ability_UnregisterNativeChildProcessExitCallback](../reference/apis-ability-kit/capi-native-child-process-h.md#oh_ability_unregisternativechildprocessexitcallback) to unregister the callback used for detecting child process exits. If the return value is **NCP_NO_ERROR**, the unregistration is successful.
39
40    ```c++
41    #include <AbilityKit/native_child_process.h>
42    void OnNativeChildProcessExit(int32_t pid, int32_t signal) {
43        OH_LOG_INFO("pid: %{public}d, signal: %{public}d", pid, signal);
44    }
45
46    void RegisterNativeChildProcessExitCallback()
47    {
48        Ability_NativeChildProcess_ErrCode ret = OH_Ability_RegisterNativeChildProcessExitCallback(OnNativeChildProcessExit);
49        if (ret != NCP_NO_ERROR) {
50            OH_LOG_ERROR("register failed.");
51        }
52    }
53
54    void UnregisterNativeChildProcessExitCallback()
55    {
56        Ability_NativeChildProcess_ErrCode ret = OH_Ability_UnregisterNativeChildProcessExitCallback(OnNativeChildProcessExit);
57        if (ret != NCP_NO_ERROR) {
58            OH_LOG_ERROR("unregister failed.");
59        }
60    }
61    ```
62
632. (Main process) Add build dependencies.
64
65    Modify the **CMaklist.txt** file to add the dependencies. The following assumes that the main process is implemented in the library file named **libmainprocesssample.so**. (The implementation of the main process and child processes can be compiled to the same dynamic link library file.)
66
67    ```txt
68    target_link_libraries(mainprocesssample PUBLIC
69        # Add the dependency of the dynamic link library of Ability Kit.
70        libchild_process.so
71
72        # Dependencies of other dynamic link libraries
73        # ...
74    )
75    ```
76