• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.resourceschedule.backgroundProcessManager (Background Child Process Management)
2
3The **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/c-apis-ability-childprocess.md#oh_ability_startnativechildprocess).
4
5>  **NOTE**
6>
7> The initial APIs of this module are supported since API version 15. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import { backgroundProcessManager } from '@kit.BackgroundTasksKit';
13```
14
15## backgroundProcessManager.setProcessPriority
16
17setProcessPriority(pid: number, priority: ProcessPriority): Promise<void>
18
19Sets 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.
20
21**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager
22
23**Parameters**
24
25| Name     | Type                                 | Mandatory  | Description                                                                                                                           |
26|----------|-------------------------------------| ---- |-------------------------------------------------------------------------------------------------------------------------------|
27| pid      | number                              | Yes   | ID of the child process to be suppressed, which is returned when the child process is created through [OH_Ability_StartNativeChildProcess](../apis-ability-kit/c-apis-ability-childprocess.md#oh_ability_startnativechildprocess).|
28| priority | [ProcessPriority](#processpriority) | Yes   | Suppression priority.                                                                                                                        |
29
30
31
32**Error codes**
33
34For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [backgroundProcessManager Error Codes](errorcode-backgroundProcessManager.md).
35
36| ID   | Error Message            |
37|----------|------------------|
38| 401      | Parameter error. Possible causes: priority is out of range. |
39| 31800001 | remote error. Possible causes: remote is not work.    |
40
41**Example**
42
43```ts
44import { BusinessError } from '@kit.BasicServicesKit';
45import { backgroundProcessManager } from '@kit.BackgroundTasksKit';
46
47let childProcessPid = 33333;
48try {
49    backgroundProcessManager.setProcessPriority(childProcessPid,
50        backgroundProcessManager.ProcessPriority.PROCESS_INACTIVE);
51} catch (error) {
52    console.error(`setProcessPriority failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
53}
54```
55
56## backgroundProcessManager.resetProcessPriority
57
58resetProcessPriority(pid: number): Promise<void>
59
60Unsuppresses 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**.
61
62**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager
63
64**Parameters**
65
66| Name     | Type                | Mandatory  | Description                                                                                                                       |
67|----------|--------------------| ---- |---------------------------------------------------------------------------------------------------------------------------|
68| pid      | number             | Yes   | Child process ID, which is returned when the child process is created through [OH_Ability_StartNativeChildProcess](../apis-ability-kit/c-apis-ability-childprocess.md#oh_ability_startnativechildprocess).|
69
70**Error codes**
71
72For details about the following error codes, see [BackgroundProcessManager Error Codes](errorcode-backgroundProcessManager.md).
73
74| ID   | Error Message                                                                                                          |
75|----------|----------------------------------------------------------------------------------------------------------------|
76| 31800001 | remote error. Possible causes: remote is not work.                                                             |
77
78**Example**
79
80```ts
81import { BusinessError } from '@kit.BasicServicesKit';
82import { backgroundProcessManager } from '@kit.BackgroundTasksKit';
83
84let childProcessPid = 33333;
85try {
86    backgroundProcessManager.resetProcessPriority(childProcessPid);
87} catch (error) {
88    console.error(`setProcessPriority failed, errCode: ${(error as BusinessError).code}, message: ${(error as BusinessError).message}`);
89}
90```
91
92## ProcessPriority
93
94Specifies the child process priority.
95
96**System capability**: SystemCapability.Resourceschedule.BackgroundProcessManager
97
98| Name                  |  Value     | Description                                                                            |
99|----------------------| -------- |--------------------------------------------------------------------------------|
100| 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. |
101| 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.                                            |
102