1# @ohos.resourceschedule.backgroundProcessManager (Background Child Process Management) 2 3<!--Kit: Background Tasks Kit--> 4<!--Subsystem: Resourceschedule--> 5<!--Owner: @hongjianfeng--> 6<!--Designer: @zhouben25--> 7<!--Tester: @fenglili18--> 8<!--Adviser: @Brilliantry_Rui--> 9 10The **backgroundProcessManager** module provides APIs for background child process management. You can use these APIs to suppress and unsuppress child processes to prevent child processes from occupying too many system resources and causing system stuttering. The APIs take effect only for the child processes created through [OH_Ability_StartNativeChildProcess](../apis-ability-kit/capi-native-child-process-h.md#oh_ability_startnativechildprocess). 11 12> **NOTE** 13> 14> The initial APIs of this module are supported since API version 17. Newly added APIs will be marked with a superscript to indicate their earliest API version. 15 16## Modules to Import 17 18```ts 19import { backgroundProcessManager } from '@kit.BackgroundTasksKit'; 20``` 21 22## backgroundProcessManager.setProcessPriority 23 24setProcessPriority(pid: number, priority: ProcessPriority): Promise<void> 25 26Sets the child process priority. After a child process is suppressed, the CPU resources that can be obtained will be limited. If the scheduling policy of the main process changes, for example, from the background to the foreground, the child process changes with the main process. To suppress the child process, call this API again. 27 28**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33|----------|-------------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------| 34| pid | number | Yes | ID of the child process to be suppressed, which is the **pid** parameter after the child process is created through the [OH_Ability_StartNativeChildProcess](../apis-ability-kit/capi-native-child-process-h.md#oh_ability_startnativechildprocess) API.| 35| priority | [ProcessPriority](#processpriority) | Yes | Suppression priority. | 36 37**Return value** 38 39| Type | Description | 40| -------------- | ---------------- | 41| Promise\<void> | Promise that returns no value.| 42 43**Error codes** 44 45For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 46 47| ID | Error Message | 48|----------|------------------| 49| 401 | Parameter error. Possible causes: priority is out of range. | 50 51**Example** 52 53```ts 54import { BusinessError } from '@kit.BasicServicesKit'; 55import { backgroundProcessManager } from '@kit.BackgroundTasksKit'; 56 57let childProcessPid = 33333; 58try { 59 backgroundProcessManager.setProcessPriority(childProcessPid, 60 backgroundProcessManager.ProcessPriority.PROCESS_INACTIVE); 61} catch (error) { 62 console.error(`setProcessPriority failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 63} 64``` 65 66## backgroundProcessManager.resetProcessPriority 67 68resetProcessPriority(pid: number): Promise<void> 69 70Unsuppresses the child process. In this case, the child process follows the scheduling policy of the main process. If the scheduling policy of the main process changes, for example, from the background to the foreground, the child process changes with the main process. The effect is the same as calling **resetProcessPriority**. 71 72**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 73 74**Parameters** 75 76| Name | Type | Mandatory | Description | 77|----------|--------------------| ---- |---------------------------------------------------------------------------------------------------------------------------| 78| pid | number | Yes | ID of the child process, which is the **pid** parameter of the [OH_Ability_StartNativeChildProcess](../apis-ability-kit/capi-native-child-process-h.md#oh_ability_startnativechildprocess) API.| 79 80**Return value** 81 82| Type | Description | 83| -------------- | ---------------- | 84| Promise\<void> | Promise that returns no value.| 85 86**Example** 87 88```ts 89import { BusinessError } from '@kit.BasicServicesKit'; 90import { backgroundProcessManager } from '@kit.BackgroundTasksKit'; 91 92let childProcessPid = 33333; 93try { 94 backgroundProcessManager.resetProcessPriority(childProcessPid); 95} catch (error) { 96 console.error(`setProcessPriority failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 97} 98``` 99 100## backgroundProcessManager.setPowerSaveMode<sup>20+</sup> 101 102setPowerSaveMode(pid: number, powerSaveMode: PowerSaveMode): Promise<void> 103 104Sets the power saving mode for a process. This API uses a promise to return the result. This API takes effect only on PCs/2-in-1 devices. 105 106You can set to enter the power saving mode when: 107- The application is not focused, and there are no audio operations or UI updates. 108- The application cannot obtain the power lock through the system framework. 109- The application needs to perform time-consuming computing tasks, such as compression, decompression, and compilation, which are significantly restricted by CPU resources. (In this case, the power saving mode will be enabled forcibly.) 110 111**Required permissions**: ohos.permission.BACKGROUND_MANAGER_POWER_SAVE_MODE 112 113**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 114 115**Parameters** 116 117| Name | Type | Mandatory | Description | 118|-------------|-----------|-----------|-----------| 119| pid | number | Yes | Process ID. | 120| powerSaveMode | [PowerSaveMode](#powersavemode20) | Yes| Power saving mode.| 121 122**Return value** 123 124| Type | Description | 125| -------------- | ---------------- | 126| Promise\<void> | Promise that returns no value.| 127 128**Error codes** 129 130For details about the error codes, see [backgroundProcessManager Error Codes](errorcode-backgroundProcessManager.md) and [Universal Error Codes](../errorcode-universal.md). 131 132| ID | Error Message | 133|----------|------------------| 134| 201 | Permission denied. | 135| 801 | Capability not supported. | 136| 31800002 | Parameter error. Possible causes: <br> 1. Mandatory parameters are left unspecified; <br> 2. Incorrect parameter types; 3. PowerSaveMode status is out of range. | 137| 31800003 | Setup error, This setting is overridden by setting in Task Manager. | 138| 31800004 | The setting failed due to system scheduling reasons. | 139 140**Example** 141 142```ts 143import { BusinessError } from '@kit.BasicServicesKit'; 144import { backgroundProcessManager } from '@kit.BackgroundTasksKit'; 145 146let pid = 33333; 147try { 148 backgroundProcessManager.setPowerSaveMode(pid, backgroundProcessManager.PowerSaveMode.EFFICIENCY_MODE); 149} catch (error) { 150 console.error(`setPowerSaveMode failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 151} 152``` 153 154## backgroundProcessManager.isPowerSaveMode<sup>20+</sup> 155 156isPowerSaveMode(pid: number): Promise<boolean> 157 158Queries whether the process is in power saving mode. This API uses a promise to return the result. This API takes effect only on PCs/2-in-1 devices. 159 160**Required permissions**: ohos.permission.BACKGROUND_MANAGER_POWER_SAVE_MODE 161 162**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 163 164**Parameters** 165 166| Name | Type | Mandatory | Description | 167|-------------|-----------|-----------|-----------| 168| pid | number | Yes | Process ID. | 169 170**Return value** 171 172| Type | Description | 173| -------------- | ---------------- | 174| Promise\<boolean> | Promise used to return the query result. The value **true** means that the process is in power saving mode; the value **false** means the opposite.| 175 176**Error codes** 177 178For details about the error codes, see [backgroundProcessManager Error Codes](errorcode-backgroundProcessManager.md) and [Universal Error Codes](../errorcode-universal.md). 179 180| ID | Error Message | 181|----------|------------------| 182| 201 | Permission denied. | 183| 801 | Capability not supported. | 184| 31800002 | Parameter error. Possible causes: <br> 1. Mandatory parameters are left unspecified; <br> 2. Incorrect parameter types. | 185 186**Example** 187 188```ts 189import { BusinessError } from '@kit.BasicServicesKit'; 190import { backgroundProcessManager } from '@kit.BackgroundTasksKit'; 191 192let pid = 33333; 193try { 194 backgroundProcessManager.isPowerSaveMode(pid).then((result: boolean) => { 195 console.info("isPowerSaveMode: " + result.toString()); 196 }); 197} catch (error) { 198 console.error(`isPowerSaveMode failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`); 199} 200``` 201 202## ProcessPriority 203 204Specifies the child process priority. 205 206**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 207 208| Name | Value | Description | 209|----------------------| -------- |--------------------------------------------------------------------------------| 210| PROCESS_BACKGROUND | 1 | Compared with **PROCESS_INACTIVE**, this priority has a more obvious suppression effect. Child processes can obtain less CPU resources. You are advised to set this priority when executing background child processes that cannot be perceived by users, such as background image-text pages. | 211| PROCESS_INACTIVE | 2 | You are advised to set this priority when executing background child processes that can be perceived by users, such as audio playback and navigation. | 212 213## PowerSaveMode<sup>20+</sup> 214 215Specifies the power saving mode. 216 217**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager 218 219| Name| Value| Description| 220|-----|----|-------| 221| EFFICIENCY_MODE | 1 | Efficiency mode. Applications set to this mode will not enter the power saving mode, where fewer CPU resources are available.| 222| DEFAULT_MODE | 2 | Default mode. Applications set to this mode may follow the system to enter the power saving mode.| 223 224<!--no_check-->