1e41f4b71Sopenharmony_ci# @ohos.app.ability.childProcessManager (childProcessManager) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_cichildProcessManager模块提供子进程管理能力,支持子进程启动操作。当前仅支持2in1、tablet设备。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci创建的子进程不支持UI界面,也不支持Context相关的接口调用。通过此模块(非SELF_FORK模式)和[ChildProcess](c-apis-ability-childprocess.md)启动的子进程总数最大为512个。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 导入模块 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci```ts 16e41f4b71Sopenharmony_ciimport { childProcessManager } from '@kit.AbilityKit'; 17e41f4b71Sopenharmony_ci``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## StartMode 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci子进程启动模式枚举。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 26e41f4b71Sopenharmony_ci| -------- | ----------------- | ----------------- | 27e41f4b71Sopenharmony_ci| SELF_FORK | 0 | 从App自身进程Fork子进程。以该模式启动的子进程中不能进行Binder IPC调用,会导致子进程Crash。不支持异步ArkTS API调用。 | 28e41f4b71Sopenharmony_ci| APP_SPAWN_FORK | 1 | 从AppSpawn Fork子进程。以该模式启动的子进程不会继承父进程资源,且没有ApplicationContext,子进程中不支持依赖ApplicationContext的API调用。 | 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci## childProcessManager.startChildProcess 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_cistartChildProcess(srcEntry: string, startMode: StartMode): Promise<number> 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci启动子进程,并调用子进程的入口方法。使用Promise异步回调。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci创建子进程成功会返回子进程pid,但并不代表入口方法调用成功,具体结果以[ChildProcess.onStart](js-apis-app-ability-childProcess.md#childprocessonstart)方法是否调用成功为准。子进程中不支持再次创建子进程。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**参数:** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci | 参数名 | 类型 | 必填 | 说明 | 43e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 44e41f4b71Sopenharmony_ci | srcEntry | string | 是 | 子进程源文件路径,只支持源文件放在entry类型的模块中,以src/main为根目录。例如子进程文件在entry模块下src/main/ets/process/DemoProcess.ets,则srcEntry为"./ets/process/DemoProcess.ets"。<br/>另外,需要确保子进程源文件被其它文件引用到,防止被构建工具优化掉。(详见下方示例代码) | 45e41f4b71Sopenharmony_ci | startMode | [StartMode](#startmode) | 是 | 子进程启动模式。 | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**返回值:** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci | 类型 | 说明 | 50e41f4b71Sopenharmony_ci | -------- | -------- | 51e41f4b71Sopenharmony_ci | Promise<number> | Promise对象,返回子进程pid。 | 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**错误码**: 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 58e41f4b71Sopenharmony_ci| ------- | -------- | 59e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 60e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 61e41f4b71Sopenharmony_ci| 16000061 | Operation not supported. | 62e41f4b71Sopenharmony_ci| 16000062 | The number of child processes exceeds the upper limit. | 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**示例:** 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci```ts 67e41f4b71Sopenharmony_ci// 在entry模块的src/main/ets/process下创建DemoProcess.ets子进程类: 68e41f4b71Sopenharmony_ci// entry/src/main/ets/process/DemoProcess.ets 69e41f4b71Sopenharmony_ciimport { ChildProcess } from '@kit.AbilityKit'; 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciexport default class DemoProcess extends ChildProcess { 72e41f4b71Sopenharmony_ci onStart() { 73e41f4b71Sopenharmony_ci console.log("DemoProcess OnStart() called"); 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci} 76e41f4b71Sopenharmony_ci``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci<!--code_no_check--> 79e41f4b71Sopenharmony_ci```ts 80e41f4b71Sopenharmony_ci// 使用childProcessManager.startChildProcess方法启动子进程: 81e41f4b71Sopenharmony_ci// entry/src/main/ets/tool/Tool.ets 82e41f4b71Sopenharmony_ciimport { childProcessManager } from '@kit.AbilityKit'; 83e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 84e41f4b71Sopenharmony_ciimport DemoProcess from '../process/DemoProcess'; 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_citry { 87e41f4b71Sopenharmony_ci DemoProcess.toString(); // 这里要调用下DemoProcess类的任意方法,防止没有引用到而被构建工具优化掉 88e41f4b71Sopenharmony_ci childProcessManager.startChildProcess("./ets/process/DemoProcess.ets", childProcessManager.StartMode.SELF_FORK) 89e41f4b71Sopenharmony_ci .then((data) => { 90e41f4b71Sopenharmony_ci console.log(`startChildProcess success, pid: ${data}`); 91e41f4b71Sopenharmony_ci }, (err: BusinessError) => { 92e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${err.code}`); 93e41f4b71Sopenharmony_ci }) 94e41f4b71Sopenharmony_ci} catch (err) { 95e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${(err as BusinessError).code}`); 96e41f4b71Sopenharmony_ci} 97e41f4b71Sopenharmony_ci``` 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci## childProcessManager.startChildProcess 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_cistartChildProcess(srcEntry: string, startMode: StartMode, callback: AsyncCallback<number>): void 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci启动子进程,并调用子进程的入口方法。使用callback异步回调。 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci创建子进程成功会返回子进程pid,但并不代表入口方法调用成功,具体结果以[ChildProcess.onStart](js-apis-app-ability-childProcess.md#childprocessonstart)方法是否调用成功为准。子进程中不支持再次创建子进程。 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**参数:** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci | 参数名 | 类型 | 必填 | 说明 | 112e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 113e41f4b71Sopenharmony_ci | srcEntry | string | 是 | 子进程源文件路径,只支持源文件放在entry类型的模块中,以src/main为根目录。例如子进程文件在entry模块下src/main/ets/process/DemoProcess.ets,则srcEntry为"./ets/process/DemoProcess.ets"。<br/>另外,需要确保子进程源文件被其它文件引用到,防止被构建工具优化掉。(详见下方示例代码) | 114e41f4b71Sopenharmony_ci | startMode | [StartMode](#startmode) | 是 | 子进程启动模式。 | 115e41f4b71Sopenharmony_ci | callback | AsyncCallback<number> | 是 | 回调函数。当子进程启动成功,err为undefined,data为获取到的子进程pid;否则为错误对象。 | 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci**错误码**: 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 122e41f4b71Sopenharmony_ci| ------- | -------- | 123e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 124e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 125e41f4b71Sopenharmony_ci| 16000061 | Operation not supported. | 126e41f4b71Sopenharmony_ci| 16000062 | The number of child processes exceeds the upper limit. | 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**示例:** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci```ts 131e41f4b71Sopenharmony_ci// 在entry模块的src/main/ets/process下创建DemoProcess.ets子进程类: 132e41f4b71Sopenharmony_ci// entry/src/main/ets/process/DemoProcess.ets 133e41f4b71Sopenharmony_ciimport { ChildProcess } from '@kit.AbilityKit'; 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciexport default class DemoProcess extends ChildProcess { 136e41f4b71Sopenharmony_ci onStart() { 137e41f4b71Sopenharmony_ci console.log("DemoProcess OnStart() called"); 138e41f4b71Sopenharmony_ci } 139e41f4b71Sopenharmony_ci} 140e41f4b71Sopenharmony_ci``` 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci<!--code_no_check--> 143e41f4b71Sopenharmony_ci```ts 144e41f4b71Sopenharmony_ci// 使用childProcessManager.startChildProcess方法启动子进程: 145e41f4b71Sopenharmony_ci// entry/src/main/ets/tool/Tool.ets 146e41f4b71Sopenharmony_ciimport { childProcessManager } from '@kit.AbilityKit'; 147e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 148e41f4b71Sopenharmony_ciimport DemoProcess from '../process/DemoProcess'; 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_citry { 151e41f4b71Sopenharmony_ci DemoProcess.toString(); // 这里要调用下DemoProcess类的任意方法,防止没有引用到而被构建工具优化掉 152e41f4b71Sopenharmony_ci childProcessManager.startChildProcess("./ets/process/DemoProcess.ets", childProcessManager.StartMode.SELF_FORK, (err, data) => { 153e41f4b71Sopenharmony_ci if (data) { 154e41f4b71Sopenharmony_ci console.log(`startChildProcess success, pid: ${data}`); 155e41f4b71Sopenharmony_ci } else { 156e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${err.code}`); 157e41f4b71Sopenharmony_ci } 158e41f4b71Sopenharmony_ci }); 159e41f4b71Sopenharmony_ci} catch (err) { 160e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${(err as BusinessError).code}`); 161e41f4b71Sopenharmony_ci} 162e41f4b71Sopenharmony_ci``` 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci## childProcessManager.startArkChildProcess<sup>12+</sup> 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_cistartArkChildProcess(srcEntry: string, args: ChildProcessArgs, options?: ChildProcessOptions): Promise<number> 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci启动子进程,并调用子进程的入口方法。使用Promise异步回调。 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci子进程不会继承父进程资源。创建子进程成功会返回子进程pid,但并不代表入口方法调用成功,具体结果以[ChildProcess.onStart](js-apis-app-ability-childProcess.md#childprocessonstart)方法是否调用成功为准。子进程中不支持再次创建子进程。 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci子进程支持传参和异步ArkTS API调用(部分依赖ApplicationContext的API除外)。[ChildProcess.onStart](js-apis-app-ability-childProcess.md#childprocessonstart)方法执行完后子进程不会自动销毁,需要子进程调用[process.abort](../apis-arkts/js-apis-process.md#processabort)销毁。主进程销毁后子进程也会一并销毁。 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci**参数:** 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci | 参数名 | 类型 | 必填 | 说明 | 179e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 180e41f4b71Sopenharmony_ci | srcEntry | string | 是 | 子进程源文件路径,不支持源文件放在HAR类型的模块中。由“模块名” + “/” + “文件路径”组成,文件路径以src/main为根目录。例如子进程文件在module1模块下src/main/ets/process/DemoProcess.ets,则srcEntry为"module1/./ets/process/DemoProcess.ets"。<br/>另外,需要确保子进程源文件被其它文件引用到,防止被构建工具优化掉。(详见下方示例代码) | 181e41f4b71Sopenharmony_ci | args | [ChildProcessArgs](js-apis-app-ability-childProcessArgs.md) | 是 | 传递到子进程的参数。 | 182e41f4b71Sopenharmony_ci | options | [ChildProcessOptions](js-apis-app-ability-childProcessOptions.md) | 否 | 子进程的启动配置选项。| 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**返回值:** 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci | 类型 | 说明 | 187e41f4b71Sopenharmony_ci | -------- | -------- | 188e41f4b71Sopenharmony_ci | Promise<number> | Promise对象,返回子进程pid。 | 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**错误码**: 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 195e41f4b71Sopenharmony_ci| ------- | -------- | 196e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 197e41f4b71Sopenharmony_ci| 801 | Capability not supported. | 198e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 199e41f4b71Sopenharmony_ci| 16000061 | Operation not supported. The API cannot be called in a child process. | 200e41f4b71Sopenharmony_ci| 16000062 | The number of child processes exceeds the upper limit. | 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**示例:** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci子进程部分: 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci```ts 207e41f4b71Sopenharmony_ci// 在module1模块的src/main/ets/process下创建DemoProcess.ets子进程类: 208e41f4b71Sopenharmony_ci// module1/src/main/ets/process/DemoProcess.ets 209e41f4b71Sopenharmony_ciimport { ChildProcess, ChildProcessArgs } from '@kit.AbilityKit'; 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ciexport default class DemoProcess extends ChildProcess { 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci onStart(args?: ChildProcessArgs) { 214e41f4b71Sopenharmony_ci let entryParams = args?.entryParams; 215e41f4b71Sopenharmony_ci let fd = args?.fds?.key1; 216e41f4b71Sopenharmony_ci // .. 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci} 219e41f4b71Sopenharmony_ci``` 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci主进程部分,示例中的context的获取方式请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息): 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci<!--code_no_check--> 224e41f4b71Sopenharmony_ci```ts 225e41f4b71Sopenharmony_ci// 使用childProcessManager.startArkChildProcess方法启动子进程: 226e41f4b71Sopenharmony_ci// module1/src/main/ets/tool/Tool.ets 227e41f4b71Sopenharmony_ciimport { common, ChildProcessArgs, ChildProcessOptions, childProcessManager } from '@kit.AbilityKit'; 228e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 229e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 230e41f4b71Sopenharmony_ciimport DemoProcess from '../process/DemoProcess'; 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_citry { 233e41f4b71Sopenharmony_ci DemoProcess.toString(); // 这里要调用下DemoProcess类的任意方法,防止没有引用到而被构建工具优化掉 234e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; 235e41f4b71Sopenharmony_ci let path = context.filesDir + "/test.txt"; 236e41f4b71Sopenharmony_ci let file = fs.openSync(path, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE); 237e41f4b71Sopenharmony_ci let args: ChildProcessArgs = { 238e41f4b71Sopenharmony_ci entryParams: "testParam", 239e41f4b71Sopenharmony_ci fds: { 240e41f4b71Sopenharmony_ci "key1": file.fd 241e41f4b71Sopenharmony_ci } 242e41f4b71Sopenharmony_ci }; 243e41f4b71Sopenharmony_ci let options: ChildProcessOptions = { 244e41f4b71Sopenharmony_ci isolationMode: false 245e41f4b71Sopenharmony_ci }; 246e41f4b71Sopenharmony_ci childProcessManager.startArkChildProcess("module1/./ets/process/DemoProcess.ets", args, options) 247e41f4b71Sopenharmony_ci .then((pid) => { 248e41f4b71Sopenharmony_ci console.info(`startChildProcess success, pid: ${pid}`); 249e41f4b71Sopenharmony_ci }) 250e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 251e41f4b71Sopenharmony_ci console.error(`startChildProcess business error, errorCode: ${err.code}, errorMsg:${err.message}`); 252e41f4b71Sopenharmony_ci }) 253e41f4b71Sopenharmony_ci} catch (err) { 254e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${err.code}, errorMsg:${err.message}`); 255e41f4b71Sopenharmony_ci} 256e41f4b71Sopenharmony_ci``` 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci## childProcessManager.startNativeChildProcess<sup>13+</sup> 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_cistartNativeChildProcess(entryPoint: string, args: ChildProcessArgs, options?: ChildProcessOptions): Promise<number> 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci启动Native子进程,加载参数中指定的动态链接库文件并调用入口函数。使用Promise异步回调。 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci子进程不会继承父进程资源。创建子进程成功会返回子进程pid,但并不代表入口函数调用成功,具体结果以子进程的入口函数是否调用成功为准。子进程中不支持再次创建子进程,且不支持创建ArkTS基础运行时环境。 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci入口函数执行完后子进程会自动销毁。主进程销毁后子进程也会一并销毁。 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci**参数:** 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci | 参数名 | 类型 | 必填 | 说明 | 273e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 274e41f4b71Sopenharmony_ci | entryPoint | string | 是 | 子进程中调用动态库的符号和入口函数,中间用“:”隔开(例如“libentry.so:Main”)。 | 275e41f4b71Sopenharmony_ci | args | [ChildProcessArgs](js-apis-app-ability-childProcessArgs.md) | 是 | 传递到子进程的参数。 | 276e41f4b71Sopenharmony_ci | options | [ChildProcessOptions](js-apis-app-ability-childProcessOptions.md) | 否 | 子进程的启动配置选项。| 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci**返回值:** 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci | 类型 | 说明 | 281e41f4b71Sopenharmony_ci | -------- | -------- | 282e41f4b71Sopenharmony_ci | Promise<number> | Promise对象,返回子进程pid。 | 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci**错误码**: 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci 以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 289e41f4b71Sopenharmony_ci| ------- | -------- | 290e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 291e41f4b71Sopenharmony_ci| 801 | Capability not supported. Capability not supported. Failed to call the API due to limited device capabilities. | 292e41f4b71Sopenharmony_ci| 16000050 | Internal error. | 293e41f4b71Sopenharmony_ci| 16000061 | Operation not supported. The API cannot be called in a child process. | 294e41f4b71Sopenharmony_ci| 16000062 | The number of child processes exceeds the upper limit. | 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci**示例:** 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci子进程部分,详见[Native子进程开发指导(C/C++)- 创建支持参数传递的子进程](../../application-models/capi_nativechildprocess_development_guideline.md#创建支持参数传递的子进程): 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci```c++ 301e41f4b71Sopenharmony_ci#include <AbilityKit/native_child_process.h> 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ciextern "C" { 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci/** 306e41f4b71Sopenharmony_ci * 子进程的入口函数,实现子进程的业务逻辑 307e41f4b71Sopenharmony_ci * 函数名称可以自定义,在主进程调用OH_Ability_StartNativeChildProcess方法时指定,此示例中为Main 308e41f4b71Sopenharmony_ci * 函数返回后子进程退出 309e41f4b71Sopenharmony_ci */ 310e41f4b71Sopenharmony_civoid Main(NativeChildProcess_Args args) 311e41f4b71Sopenharmony_ci{ 312e41f4b71Sopenharmony_ci // 获取传入的entryPrams 313e41f4b71Sopenharmony_ci char *entryParams = args.entryParams; 314e41f4b71Sopenharmony_ci // 获取传入的fd列表,对应ChildProcessArgs中的args.fds 315e41f4b71Sopenharmony_ci NativeChildProcess_Fd *current = args.fdList.head; 316e41f4b71Sopenharmony_ci while (current != nullptr) { 317e41f4b71Sopenharmony_ci char *fdName = current->fdName; 318e41f4b71Sopenharmony_ci int32_t fd = current->fd; 319e41f4b71Sopenharmony_ci current = current->next; 320e41f4b71Sopenharmony_ci // 业务逻辑.. 321e41f4b71Sopenharmony_ci } 322e41f4b71Sopenharmony_ci} 323e41f4b71Sopenharmony_ci} // extern "C" 324e41f4b71Sopenharmony_ci``` 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci主进程部分,示例中的context的获取方式请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息): 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci```ts 329e41f4b71Sopenharmony_ci// 主进程: 330e41f4b71Sopenharmony_ci// 使用childProcessManager.startNativeChildProcess方法启动子进程: 331e41f4b71Sopenharmony_ciimport { common, ChildProcessArgs, ChildProcessOptions, childProcessManager } from '@kit.AbilityKit'; 332e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 333e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_citry { 336e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; 337e41f4b71Sopenharmony_ci let path = context.filesDir + "/test.txt"; 338e41f4b71Sopenharmony_ci let file = fs.openSync(path, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE); 339e41f4b71Sopenharmony_ci let args: ChildProcessArgs = { 340e41f4b71Sopenharmony_ci entryParams: "testParam", 341e41f4b71Sopenharmony_ci fds: { 342e41f4b71Sopenharmony_ci "key1": file.fd 343e41f4b71Sopenharmony_ci } 344e41f4b71Sopenharmony_ci }; 345e41f4b71Sopenharmony_ci let options: ChildProcessOptions = { 346e41f4b71Sopenharmony_ci isolationMode: false 347e41f4b71Sopenharmony_ci }; 348e41f4b71Sopenharmony_ci childProcessManager.startNativeChildProcess("libentry.so:Main", args, options) 349e41f4b71Sopenharmony_ci .then((pid) => { 350e41f4b71Sopenharmony_ci console.info(`startChildProcess success, pid: ${pid}`); 351e41f4b71Sopenharmony_ci }) 352e41f4b71Sopenharmony_ci .catch((err: BusinessError) => { 353e41f4b71Sopenharmony_ci console.error(`startChildProcess business error, errorCode: ${err.code}, errorMsg:${err.message}`); 354e41f4b71Sopenharmony_ci }) 355e41f4b71Sopenharmony_ci} catch (err) { 356e41f4b71Sopenharmony_ci console.error(`startChildProcess error, errorCode: ${err.code}, errorMsg:${err.message}`); 357e41f4b71Sopenharmony_ci} 358e41f4b71Sopenharmony_ci```