js-apis-process.md 11.7 KB
Newer Older
1
# @ohos.process (Obtaining Process Information)
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9 10 11 12 13

```
import process from '@ohos.process';
```

W
wusongqing 已提交
14 15 16

## Attributes

W
wusongqing 已提交
17 18
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
19 20
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
G
Gloria 已提交
21 22 23
| uid | number | Yes| No| User identifier (UID) of the process.|
| pid | number | Yes| No| Process ID (PID) of the process.|
| tid<sup>8+</sup> | number | Yes| No| Thread ID (TID) of the thread.|
W
wusongqing 已提交
24 25


G
Gloria 已提交
26
## EventListener
G
Gloria 已提交
27

G
Gloria 已提交
28
**System capability**: SystemCapability.Utils.Lang
G
Gloria 已提交
29

G
Gloria 已提交
30 31 32
| Name| Description|
| -------- | -------- |
| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|
G
Gloria 已提交
33 34


G
Gloria 已提交
35
## process.isIsolatedProcess<sup>8+</sup>
G
Gloria 已提交
36

G
Gloria 已提交
37
isIsolatedProcess(): boolean
G
Gloria 已提交
38

G
Gloria 已提交
39
Checks whether this process is isolated.
G
Gloria 已提交
40

G
Gloria 已提交
41
**System capability**: SystemCapability.Utils.Lang
G
Gloria 已提交
42 43 44 45 46

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
47
| boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
G
Gloria 已提交
48 49 50 51

**Example**

```js
G
Gloria 已提交
52
let result = process.isIsolatedProcess();
G
Gloria 已提交
53 54 55
```


G
Gloria 已提交
56
## process.is64Bit<sup>8+</sup>
G
Gloria 已提交
57

G
Gloria 已提交
58
is64Bit(): boolean
G
Gloria 已提交
59

G
Gloria 已提交
60
Checks whether this process is running in a 64-bit environment.
G
Gloria 已提交
61 62 63 64 65 66 67

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
68
| boolean | Returns **true** if the process is running in a 64-bit environment; returns **false** otherwise.|
G
Gloria 已提交
69 70 71 72

**Example**

```js
G
Gloria 已提交
73
let result = process.is64Bit();
G
Gloria 已提交
74 75 76
```


G
Gloria 已提交
77
## process.getStartRealtime<sup>8+</sup>
G
Gloria 已提交
78

G
Gloria 已提交
79
getStartRealtime(): number
G
Gloria 已提交
80

G
Gloria 已提交
81
Obtains the duration, in milliseconds, from the time the system starts to the time the process starts.
G
Gloria 已提交
82 83 84 85 86 87 88

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
89
| number | Duration obtained, in millisecond.|
G
Gloria 已提交
90 91 92 93

**Example**

```js
G
Gloria 已提交
94
let realtime = process.getStartRealtime();
G
Gloria 已提交
95 96
```

G
Gloria 已提交
97
## process.getPastCpuTime<sup>8+</sup>
G
Gloria 已提交
98

G
Gloria 已提交
99
getPastCpuTime(): number
G
Gloria 已提交
100

G
Gloria 已提交
101
Obtains the CPU time (in milliseconds) from the time the process starts to the current time.
G
Gloria 已提交
102 103 104 105 106 107 108

**System capability**: SystemCapability.Utils.Lang

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
109
| number | CPU time obtained, in millisecond.|
G
Gloria 已提交
110 111 112 113

**Example**

```js
G
Gloria 已提交
114
let result = process.getPastCpuTime() ;
G
Gloria 已提交
115 116 117
```


G
Gloria 已提交
118
## process.abort
G
Gloria 已提交
119

G
Gloria 已提交
120
abort(): void
G
Gloria 已提交
121

G
Gloria 已提交
122 123 124
Aborts a process and generates a core file. This method will cause a process to exit immediately. Exercise caution when using this method.

**System capability**: SystemCapability.Utils.Lang
G
Gloria 已提交
125

G
Gloria 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
**Example**

```js
process.abort();
```


## process.uptime

uptime(): number

Obtains the running time of this process.

**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
141
**Return value**
Z
zengyawen 已提交
142

W
wusongqing 已提交
143 144
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
145
| number | Running time of the process, in seconds.|
Z
zengyawen 已提交
146

W
wusongqing 已提交
147
**Example**
Z
zengyawen 已提交
148

149
```js
G
Gloria 已提交
150
let time = process.uptime();
W
wusongqing 已提交
151
```
Z
zengyawen 已提交
152 153


G
Gloria 已提交
154
## process.kill<sup>(deprecated)</sup>
Z
zengyawen 已提交
155

G
Gloria 已提交
156
kill(signal: number, pid: number): boolean
Z
zengyawen 已提交
157

G
Gloria 已提交
158
Sends a signal to the specified process to terminate it.
159

G
Gloria 已提交
160 161 162
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [kill<sup>9+</sup>](#kill9) instead.
W
wusongqing 已提交
163

W
wusongqing 已提交
164 165
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
166 167 168 169 170 171 172
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| pid | number | Yes| PID of the process, to which the signal will be sent.|
| signal | number | Yes| Signal to send.|

W
wusongqing 已提交
173
**Return value**
Z
zengyawen 已提交
174

W
wusongqing 已提交
175 176
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
177
| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
Z
zengyawen 已提交
178

W
wusongqing 已提交
179
**Example**
Z
zengyawen 已提交
180

181
```js
G
Gloria 已提交
182 183
let pres = process.pid
let result = process.kill(28, pres)
W
wusongqing 已提交
184
```
Z
zengyawen 已提交
185 186


G
Gloria 已提交
187
## process.exit<sup>(deprecated)</sup>
Z
zengyawen 已提交
188

G
Gloria 已提交
189
exit(code: number): void
Z
zengyawen 已提交
190

G
Gloria 已提交
191
Terminates this process.
Z
zengyawen 已提交
192

G
Gloria 已提交
193
Exercise caution when using this API. After this API is called, the application exits. If the input parameter is not 0, data loss or exceptions may occur.
194

G
Gloria 已提交
195 196 197
> **NOTE**
>
> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [exit<sup>9+</sup>](#exit9) instead.
W
wusongqing 已提交
198

W
wusongqing 已提交
199 200
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
201 202 203 204 205 206
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| code | number | Yes| Exit code of the process.|

W
wusongqing 已提交
207
**Example**
Z
zengyawen 已提交
208

209
```js
G
Gloria 已提交
210
process.exit(0);
W
wusongqing 已提交
211
```
Z
zengyawen 已提交
212 213


G
Gloria 已提交
214
## process.getUidForName<sup>(deprecated)</sup>
Z
zengyawen 已提交
215

G
Gloria 已提交
216
getUidForName(v: string): number
Z
zengyawen 已提交
217

G
Gloria 已提交
218
Obtains the process UID based on the process name.
219

G
Gloria 已提交
220 221 222
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getUidForName<sup>9+</sup>](#getuidforname9) instead.
W
wusongqing 已提交
223

W
wusongqing 已提交
224 225
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
226
**Parameters**
Z
zengyawen 已提交
227

W
wusongqing 已提交
228 229
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
230 231 232 233 234 235 236
| v | string | Yes| Name of a process.|

**Return value**

| Type| Description|
| -------- | -------- |
| number | Process UID.|
Z
zengyawen 已提交
237

W
wusongqing 已提交
238
**Example**
Z
zengyawen 已提交
239

240
```js
G
Gloria 已提交
241
let pres = process.getUidForName("tool")
W
wusongqing 已提交
242
```
Z
zengyawen 已提交
243

Z
zengyawen 已提交
244

G
Gloria 已提交
245
## process.getThreadPriority<sup>(deprecated)</sup>
Z
zengyawen 已提交
246

G
Gloria 已提交
247
getThreadPriority(v: number): number
Z
zengyawen 已提交
248

G
Gloria 已提交
249 250 251 252 253
Obtains the thread priority based on the specified TID.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getThreadPriority<sup>9+</sup>](#getthreadpriority9) instead.
Z
zengyawen 已提交
254

W
wusongqing 已提交
255 256
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
257 258 259 260 261 262
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|

W
wusongqing 已提交
263
**Return value**
Z
zengyawen 已提交
264

W
wusongqing 已提交
265 266
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
267
| number | Priority of the thread.|
Z
zengyawen 已提交
268

W
wusongqing 已提交
269
**Example**
Z
zengyawen 已提交
270

271
```js
G
Gloria 已提交
272 273
let tid = process.tid;
let pres = process.getThreadPriority(tid);
W
wusongqing 已提交
274
```
Z
zengyawen 已提交
275 276


G
Gloria 已提交
277
## process.isAppUid<sup>(deprecated)</sup>
Z
zengyawen 已提交
278

X
xdmal 已提交
279
isAppUid(v: number): boolean
Z
zengyawen 已提交
280

G
Gloria 已提交
281
Checks whether a UID belongs to this application.
Z
zengyawen 已提交
282

G
Gloria 已提交
283 284 285 286
> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [isAppUid<sup>9+</sup>](#isappuid9) instead.

W
wusongqing 已提交
287 288
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
289 290 291 292 293 294 295 296 297 298
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| UID.|

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
299
| boolean | Returns **true** if the UID belongs to the application; returns **false** otherwise.|
W
wusongqing 已提交
300 301 302

**Example**

303
```js
304
let result = process.isAppUid(688);
W
wusongqing 已提交
305 306 307
```


G
Gloria 已提交
308
## process.getSystemConfig<sup>(deprecated)</sup>
W
wusongqing 已提交
309

G
Gloria 已提交
310
getSystemConfig(name: number): number
W
wusongqing 已提交
311

G
Gloria 已提交
312 313 314 315 316
Obtains the system configuration.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getSystemConfig<sup>9+</sup>](#getsystemconfig9) instead.
W
wusongqing 已提交
317

W
wusongqing 已提交
318 319
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
320 321 322 323 324 325
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | number | Yes| System configuration parameter name.|

W
wusongqing 已提交
326 327 328 329
**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
330
| number | System configuration obtained.|
W
wusongqing 已提交
331 332 333

**Example**

334
```js
G
Gloria 已提交
335 336
let _SC_ARG_MAX = 0
let pres = process.getSystemConfig(_SC_ARG_MAX)
W
wusongqing 已提交
337 338 339
```


G
Gloria 已提交
340
## process.getEnvironmentVar<sup>(deprecated)</sup>
W
wusongqing 已提交
341

G
Gloria 已提交
342
getEnvironmentVar(name: string): string
Z
zengyawen 已提交
343

G
Gloria 已提交
344 345 346 347 348
Obtains the value of an environment variable.

> **NOTE**
>
> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [getEnvironmentVar<sup>9+</sup>](#getenvironmentvar9) instead.
Z
zengyawen 已提交
349

W
wusongqing 已提交
350 351
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
352 353 354 355
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
356
| name | string | Yes| Environment variable name.|
W
wusongqing 已提交
357 358 359 360 361

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
362
| string | Value of the environment variable.|
W
wusongqing 已提交
363 364 365

**Example**

366
```js
G
Gloria 已提交
367
let pres = process.getEnvironmentVar("PATH")
W
wusongqing 已提交
368 369 370
```


G
Gloria 已提交
371 372 373 374 375 376 377
## ProcessManager<sup>9+</sup>	

Provides APIs for throwing exceptions during the addition of a process.

A **ProcessManager** object is obtained through its own constructor.

### isAppUid<sup>9+</sup>
W
wusongqing 已提交
378

G
Gloria 已提交
379
isAppUid(v: number): boolean
Z
zengyawen 已提交
380

G
Gloria 已提交
381
Checks whether a UID belongs to this application.
Z
zengyawen 已提交
382

W
wusongqing 已提交
383 384
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
385
**Parameters**
Z
zengyawen 已提交
386

W
wusongqing 已提交
387 388
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
389
| v | number | Yes| UID.|
Z
zengyawen 已提交
390

W
wusongqing 已提交
391
**Return value**
Z
zengyawen 已提交
392

W
wusongqing 已提交
393 394
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
395
| boolean | Returns **true** if the UID belongs to the application; returns **false** otherwise.|
Z
zengyawen 已提交
396

W
wusongqing 已提交
397
**Example**
Z
zengyawen 已提交
398

399
```js
G
Gloria 已提交
400 401
let pro = new process.ProcessManager();
let result = pro.isAppUid(688);
W
wusongqing 已提交
402
```
Z
zengyawen 已提交
403 404


G
Gloria 已提交
405
### getUidForName<sup>9+</sup>
Z
zengyawen 已提交
406

G
Gloria 已提交
407
getUidForName(v: string): number
W
wusongqing 已提交
408

G
Gloria 已提交
409
Obtains the process UID based on the process name.
Z
zengyawen 已提交
410

W
wusongqing 已提交
411 412
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
413 414 415 416 417 418
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | string | Yes| Name of a process.|

W
wusongqing 已提交
419 420 421 422
**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
423
| number | Process UID.|
W
wusongqing 已提交
424 425 426

**Example**

427
```js
G
Gloria 已提交
428 429
let pro = new process.ProcessManager();
let pres = pro .getUidForName("tool");
W
wusongqing 已提交
430
```
Z
zengyawen 已提交
431 432


G
Gloria 已提交
433
### getThreadPriority<sup>9+</sup>
Z
zengyawen 已提交
434

G
Gloria 已提交
435 436 437
getThreadPriority(v: number): number

Obtains the thread priority based on the specified TID.
Z
zengyawen 已提交
438

W
wusongqing 已提交
439 440
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
441 442 443 444 445 446
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|

W
wusongqing 已提交
447
**Return value**
Z
zengyawen 已提交
448

W
wusongqing 已提交
449 450
| Type| Description|
| -------- | -------- |
G
Gloria 已提交
451
| number | Priority of the thread.|
Z
zengyawen 已提交
452

W
wusongqing 已提交
453
**Example**
Z
zengyawen 已提交
454

455
```js
G
Gloria 已提交
456 457 458
let pro = new process.ProcessManager();
let tid = process.tid;
let pres = pro.getThreadPriority(tid);
W
wusongqing 已提交
459
```
Z
zengyawen 已提交
460 461


G
Gloria 已提交
462
### getSystemConfig<sup>9+</sup>
Z
zengyawen 已提交
463

X
xdmal 已提交
464
getSystemConfig(name: number): number
Z
zengyawen 已提交
465 466 467

Obtains the system configuration.

W
wusongqing 已提交
468 469
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | number | Yes| System configuration parameter name.|

**Return value**

| Type| Description|
| -------- | -------- |
| number | System configuration obtained.|

**Example**

484
```js
G
Gloria 已提交
485 486 487
let pro = new process.ProcessManager();
let _SC_ARG_MAX = 0;
let pres = pro.getSystemConfig(_SC_ARG_MAX);
W
wusongqing 已提交
488 489 490
```


G
Gloria 已提交
491
### getEnvironmentVar<sup>9+</sup>
W
wusongqing 已提交
492

X
xdmal 已提交
493
getEnvironmentVar(name: string): string
Z
zengyawen 已提交
494 495 496

Obtains the value of an environment variable.

W
wusongqing 已提交
497 498
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | string | Yes| Environment variable name.|

**Return value**

| Type| Description|
| -------- | -------- |
| string | Value of the environment variable.|

**Example**

513
```js
G
Gloria 已提交
514 515
let pro = new process.ProcessManager();
let pres = pro.getEnvironmentVar("PATH");
W
wusongqing 已提交
516 517 518
```


G
Gloria 已提交
519
### exit<sup>9+</sup>
W
wusongqing 已提交
520

G
Gloria 已提交
521
exit(code: number): void
W
wusongqing 已提交
522

G
Gloria 已提交
523
Terminates this process.
524

G
Gloria 已提交
525
Exercise caution when using this API. After this API is called, the application exits. If the input parameter is not 0, data loss or exceptions may occur.
W
wusongqing 已提交
526

W
wusongqing 已提交
527 528
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
529 530 531 532
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
533
| code | number | Yes| Exit code of the process.|
W
wusongqing 已提交
534

G
Gloria 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
**Example**

```js
let pro = new process.ProcessManager();
pro.exit(0);
```


### kill<sup>9+</sup>

kill(signal: number, pid: number): boolean

Sends a signal to the specified process to terminate it.

**System capability**: SystemCapability.Utils.Lang

**Parameters**
W
wusongqing 已提交
552 553 554

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
555 556
| pid | number | Yes| PID of the process, to which the signal will be sent.|
| signal | number | Yes| Signal to send.|
W
wusongqing 已提交
557 558 559 560 561

**Return value**

| Type| Description|
| -------- | -------- |
G
Gloria 已提交
562
| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
W
wusongqing 已提交
563 564 565

**Example**

566
```js
G
Gloria 已提交
567 568 569
let pro = new process.ProcessManager();
let pres = process.pid;
let result = pro.kill(28, pres);
W
wusongqing 已提交
570
```