1e41f4b71Sopenharmony_ci# Native Child Process Development (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci>**NOTE**
4e41f4b71Sopenharmony_ci>
5e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. They depend on [IPC Kit](../ipc/ipc-capi-development-guideline.md).
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## When to Use
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciThis topic describes how to create a native child process in the main process and establish an IPC channel between the main process and child process. It makes multi-process programming at the native layer easier.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci## Available APIs
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci| Name                                                                                                                                                                                                                                                                                                                               | Description                                                                                   |
14e41f4b71Sopenharmony_ci| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
15e41f4b71Sopenharmony_ci| int [OH_Ability_CreateNativeChildProcess](../reference/apis-ability-kit/c-apis-ability-childprocess.md#oh_ability_createnativechildprocess) (const char *libName, [OH_Ability_OnNativeChildProcessStarted](../reference/apis-ability-kit/c-apis-ability-childprocess.md#oh_ability_onnativechildprocessstarted) onProcessStarted) | Creates a child process, loads a specified dynamic link library, and returns the startup result asynchronously through a callback parameter. An independent thread is used to execute the callback function. When implementing the callback function, pay attention to thread synchronization issues and avoid performing time-consuming operations to prevent extended blocking.|
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci> **NOTE**
18e41f4b71Sopenharmony_ci>
19e41f4b71Sopenharmony_ci> Currently, only 2-in-1 devices are supported, and only one native child process can be started for a process.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## How to Develop
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciThis section describes how to use the C APIs provided by Ability Kit to create a native child process and establish an IPC channel between the main process and child process based on an existing native application development project.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**Dynamic Link Libraries**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci```txt
28e41f4b71Sopenharmony_cilibipc_capi.so
29e41f4b71Sopenharmony_cilibchild_process.so
30e41f4b71Sopenharmony_ci```
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**Header Files**
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci```c++
35e41f4b71Sopenharmony_ci#include <IPCKit/ipc_kit.h>
36e41f4b71Sopenharmony_ci#include <AbilityKit/native_child_process.h>
37e41f4b71Sopenharmony_ci```
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci### 1. Child Process - Implementing Necessary Export Functions
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciIn the child process, implement and export the functions **NativeChildProcess_OnConnect** and **NativeChildProcess_MainProc**. (It is assumed that the code file is named **ChildProcessSample.cpp**.) The **OHIPCRemoteStub** object returned by **NativeChildProcess_OnConnect** is responsible for IPC of the main process. For details, see [IPC Development (C/C++)](../ipc/ipc-capi-development-guideline.md). After the child process is started, **NativeChildProcess_OnConnect** is invoked to obtain an IPC stub object, and then **NativeChildProcess_MainProc** is called to transfer the control right of the main thread. After the second function is returned, the child process exits.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci```c++
44e41f4b71Sopenharmony_ci#include <IPCKit/ipc_kit.h>
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ciextern "C" {
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciOHIPCRemoteStub* NativeChildProcess_OnConnect()
49e41f4b71Sopenharmony_ci{
50e41f4b71Sopenharmony_ci    // ipcRemoteStub points to the IPC stub object implemented by the child process. The object is used to receive and respond to IPC messages from the main process.
51e41f4b71Sopenharmony_ci    // The child process controls its lifecycle according to the service logic.
52e41f4b71Sopenharmony_ci    return ipcRemoteStub;
53e41f4b71Sopenharmony_ci}
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_civoid NativeChildProcess_MainProc()
56e41f4b71Sopenharmony_ci{
57e41f4b71Sopenharmony_ci    // Equivalent to the Main function of the child process. It implements the service logic of the child process.
58e41f4b71Sopenharmony_ci    // ...
59e41f4b71Sopenharmony_ci    // After the function is returned, the child process exits.
60e41f4b71Sopenharmony_ci}
61e41f4b71Sopenharmony_ci  
62e41f4b71Sopenharmony_ci} // extern "C"
63e41f4b71Sopenharmony_ci```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci### 2. Child Process - Compiled as a Dynamic Link Library
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ciModify the **CMakeList.txt** file, compile the file into a dynamic link library (named **libchildprocesssample.so** in this example), and add the dependency of the IPC dynamic link library.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci```txt
70e41f4b71Sopenharmony_ciadd_library(childprocesssample SHARED
71e41f4b71Sopenharmony_ci    # Source code file that implements the necessary export functions
72e41f4b71Sopenharmony_ci    ChildProcessSample.cpp
73e41f4b71Sopenharmony_ci    
74e41f4b71Sopenharmony_ci    # Other source code files
75e41f4b71Sopenharmony_ci    # ...
76e41f4b71Sopenharmony_ci)
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_citarget_link_libraries(childprocesssample PUBLIC
79e41f4b71Sopenharmony_ci    # Add the dependency of the IPC dynamic link library.
80e41f4b71Sopenharmony_ci    libipc_capi.so
81e41f4b71Sopenharmony_ci    
82e41f4b71Sopenharmony_ci    # Dependencies of other dynamic link libraries
83e41f4b71Sopenharmony_ci    # ...
84e41f4b71Sopenharmony_ci)
85e41f4b71Sopenharmony_ci```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci### 3. Main Process - Implementing the Child Process Startup Result Callback
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci```c++
90e41f4b71Sopenharmony_ci#include <IPCKit/ipc_kit.h>
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_cistatic void OnNativeChildProcessStarted(int errCode, OHIPCRemoteProxy *remoteProxy)
93e41f4b71Sopenharmony_ci{
94e41f4b71Sopenharmony_ci    if (errCode != NCP_NO_ERROR) {
95e41f4b71Sopenharmony_ci	    // Exception handling when the child process is not started normally.
96e41f4b71Sopenharmony_ci	    // ...
97e41f4b71Sopenharmony_ci	    return;
98e41f4b71Sopenharmony_ci    }
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci    // Save the remoteProxy object for IPC with the child process based on the APIs provided by IPC Kit.
101e41f4b71Sopenharmony_ci    // You are advised to transfer time-consuming operations to an independent thread to avoid blocking the callback thread for a long time.
102e41f4b71Sopenharmony_ci    // When the IPC object is no longer needed, call OH_IPCRemoteProxy_Destroy to release it.
103e41f4b71Sopenharmony_ci    // ...
104e41f4b71Sopenharmony_ci}
105e41f4b71Sopenharmony_ci```
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciThe second parameter **OHIPCRemoteProxy** in the callback function is used to establish an IPC channel with the **OHIPCRemoteStub** object returned by the **NativeChildProcess_OnConnect** method implemented by the child process. For details, see [IPC Development (C/C++)](../ipc/ipc-capi-development-guideline.md). When the **OHIPCRemoteProxy** object is no longer needed, call [OH_IPCRemoteProxy_Destory](../reference/apis-ipc-kit/_o_h_i_p_c_remote_object.md#oh_ipcremoteproxy_destroy) to release it.
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci### 4. Main Process - Starting the Native Child Process
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ciCall the API to start the native child process. Note that the return value **NCP_NO_ERROR** only indicates that the native child process startup logic is successfully called. The actual startup result is asynchronously notified through the callback function specified in the second parameter. **A child process can be created only in the main process.**
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci```c++
114e41f4b71Sopenharmony_ci#include <AbilityKit/native_child_process.h>
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci// The first parameter libchildprocesssample.so is the name of the dynamic link library that implements the necessary export functions of the child process.
117e41f4b71Sopenharmony_ciint32_t ret = OH_Ability_CreateNativeChildProcess("libchildprocesssample.so", OnNativeChildProcessStarted);
118e41f4b71Sopenharmony_ciif (ret != NCP_NO_ERROR) {
119e41f4b71Sopenharmony_ci    // Exception handling when the child process is not started normally.
120e41f4b71Sopenharmony_ci    // ...
121e41f4b71Sopenharmony_ci}
122e41f4b71Sopenharmony_ci```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci### 5. Main Process - Adding Build Dependencies
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ciModify 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.)
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci```txt
129e41f4b71Sopenharmony_citarget_link_libraries(mainprocesssample PUBLIC
130e41f4b71Sopenharmony_ci    # Add dependencies of the IPC and ability dynamic link library.
131e41f4b71Sopenharmony_ci    libipc_capi.so
132e41f4b71Sopenharmony_ci    libchild_process.so
133e41f4b71Sopenharmony_ci    
134e41f4b71Sopenharmony_ci    # Dependencies of other dynamic link libraries
135e41f4b71Sopenharmony_ci    # ...
136e41f4b71Sopenharmony_ci)
137e41f4b71Sopenharmony_ci```
138