1# @ohos.process (Obtaining Process Information)
2
3The **process** module provides process management APIs, for example, APIs for obtaining process information.
4
5> **NOTE**
6>
7> 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.
8
9
10## Modules to Import
11
12```ts
13import { process } from '@kit.ArkTS';
14```
15
16
17## Attributes
18
19**System capability**: SystemCapability.Utils.Lang
20
21**Atomic service API**: This API can be used in atomic services since API version 11.
22
23| Name            | Type  | Readable | Writable | Description            |
24| ---------------- | ------ | ---- | ---- | ---------------- |
25| uid              | number | Yes  | No  | User identifier (UID) of the process. |
26| pid              | number | Yes  | No  | Process ID (PID) of the process. |
27| tid<sup>8+</sup> | number | Yes  | No  | Thread ID (TID) of the thread. |
28
29
30## EventListener
31
32type EventListener = (evt: Object) => void
33
34Describes the event to store.
35
36**Atomic service API**: This API can be used in atomic services since API version 11.
37
38**System capability**: SystemCapability.Utils.Lang
39
40**Parameters**
41
42| Name | Type  | Mandatory | Description           |
43| ------ | ------ | ---- | --------------- |
44| evt   | Object | Yes | Event.|
45
46## process.isIsolatedProcess<sup>8+</sup>
47
48isIsolatedProcess(): boolean
49
50Checks whether this process is isolated.
51
52**Atomic service API**: This API can be used in atomic services since API version 11.
53
54**System capability**: SystemCapability.Utils.Lang
55
56**Return value**
57
58| Type   | Description                                                   |
59| ------- | ------------------------------------------------------- |
60| boolean | **true**: The process is isolated.<br>**false**: The process is not isolated. |
61
62**Example**
63
64```js
65let result = process.isIsolatedProcess();
66```
67
68
69## process.is64Bit<sup>8+</sup>
70
71is64Bit(): boolean
72
73Checks whether this process is running in a 64-bit environment.
74
75**Atomic service API**: This API can be used in atomic services since API version 11.
76
77**System capability**: SystemCapability.Utils.Lang
78
79**Return value**
80
81| Type   | Description                                                       |
82| ------- | ----------------------------------------------------------- |
83| boolean | **true**: The process is running in a 64-bit environment.<br>**false**: The process is not running in a 64-bit environment. |
84
85**Example**
86
87```js
88let result = process.is64Bit();
89```
90
91
92## process.getStartRealtime<sup>8+</sup>
93
94getStartRealtime(): number
95
96Obtains the duration, in milliseconds, from the time the system starts to the time the process starts.
97
98**Atomic service API**: This API can be used in atomic services since API version 11.
99
100**System capability**: SystemCapability.Utils.Lang
101
102**Return value**
103
104| Type  | Description                          |
105| ------ | ------------------------------ |
106| number | Duration obtained, in millisecond. |
107
108**Example**
109
110```js
111let realtime = process.getStartRealtime();
112```
113
114## process.getPastCpuTime<sup>8+</sup>
115
116getPastCpuTime(): number
117
118Obtains the CPU time (in milliseconds) from the time the process starts to the current time.
119
120**Atomic service API**: This API can be used in atomic services since API version 11.
121
122**System capability**: SystemCapability.Utils.Lang
123
124**Return value**
125
126| Type  | Description                         |
127| ------ | ----------------------------- |
128| number | CPU time obtained, in millisecond. |
129
130**Example**
131
132```js
133let result = process.getPastCpuTime() ;
134```
135
136
137## process.abort
138
139abort(): void
140
141Aborts a process and generates a core file. This method will cause a process to exit immediately. Exercise caution when using this method.
142
143**Atomic service API**: This API can be used in atomic services since API version 11.
144
145**System capability**: SystemCapability.Utils.Lang
146
147**Example**
148
149```js
150process.abort();
151```
152
153
154## process.uptime
155
156uptime(): number
157
158Obtains the running time of this process.
159
160**Atomic service API**: This API can be used in atomic services since API version 11.
161
162**System capability**: SystemCapability.Utils.Lang
163
164**Return value**
165
166| Type  | Description                  |
167| ------ | ---------------------- |
168| number | Running time of the process, in seconds. |
169
170**Example**
171
172```js
173let time = process.uptime();
174```
175
176
177## process.kill<sup>(deprecated)</sup>
178
179kill(signal: number, pid: number): boolean
180
181Sends a signal to the specified process to terminate it.
182
183> **NOTE**
184>
185> 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.
186
187**System capability**: SystemCapability.Utils.Lang
188
189**Parameters**
190
191| Name | Type  | Mandatory | Description        |
192| ------ | ------ | ---- | ------------ |
193| signal | number | Yes  | Signal to send. |
194| pid    | number | Yes  | PID of the process, to which the signal will be sent.  |
195
196**Return value**
197
198| Type   | Description                                                        |
199| ------- | ------------------------------------------------------------ |
200| boolean |  **true**: The signal is sent successfully.<br>**false**: The signal fails to be sent. |
201
202**Example**
203
204```js
205let pres = process.pid
206let result = process.kill(28, pres)
207```
208
209
210## process.exit<sup>(deprecated)</sup>
211
212exit(code: number): void
213
214Terminates this process.
215
216Exercise 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.
217
218> **NOTE**
219>
220> 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.
221
222**System capability**: SystemCapability.Utils.Lang
223
224**Parameters**
225
226| Name | Type  | Mandatory | Description          |
227| ------ | ------ | ---- | -------------- |
228| code   | number | Yes  | Exit code of the process. |
229
230**Example**
231
232```js
233process.exit(0);
234```
235
236
237## process.getUidForName<sup>(deprecated)</sup>
238
239getUidForName(v: string): number
240
241Obtains the UID of a user from the user database of the system based on the specified user name.
242
243> **NOTE**
244>
245> 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.
246
247**System capability**: SystemCapability.Utils.Lang
248
249**Parameters**
250
251| Name | Type  | Mandatory | Description    |
252| ------ | ------ | ---- | -------- |
253| v      | string | Yes  | User name. |
254
255**Return value**
256
257| Type  | Description         |
258| ------ | ------------- |
259| number | UID of the user. |
260
261**Example**
262
263```js
264let pres = process.getUidForName("tool")
265```
266
267
268## process.getThreadPriority<sup>(deprecated)</sup>
269
270getThreadPriority(v: number): number
271
272Obtains the thread priority based on the specified TID.
273
274> **NOTE**
275>
276> 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.
277
278**System capability**: SystemCapability.Utils.Lang
279
280**Parameters**
281
282| Name | Type  | Mandatory | Description           |
283| ------ | ------ | ---- | --------------- |
284| v      | number | Yes  | TID. |
285
286**Return value**
287
288| Type  | Description                                            |
289| ------ | ------------------------------------------------ |
290| number | Priority of the thread. The priority depends on the operating system. |
291
292**Example**
293
294```js
295let tid = process.tid;
296let pres = process.getThreadPriority(tid);
297```
298
299
300## process.isAppUid<sup>(deprecated)</sup>
301
302isAppUid(v: number): boolean
303
304Checks whether a UID belongs to this application.
305
306> **NOTE**
307>
308> 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.
309
310**System capability**: SystemCapability.Utils.Lang
311
312**Parameters**
313
314| Name | Type  | Mandatory | Description           |
315| ------ | ------ | ---- | --------------- |
316| v      | number | Yes  | UID. |
317
318**Return value**
319
320| Type   | Description                                                        |
321| ------- | ------------------------------------------------------------ |
322| boolean | **true**: The UID belongs to the application.<br>**false**: The UID does not belong to the application. |
323
324**Example**
325
326```js
327let result = process.isAppUid(688);
328```
329
330
331## process.getSystemConfig<sup>(deprecated)</sup>
332
333getSystemConfig(name: number): number
334
335Obtains the system configuration.
336
337> **NOTE**
338>
339> 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.
340
341**System capability**: SystemCapability.Utils.Lang
342
343**Parameters**
344
345| Name | Type  | Mandatory | Description                |
346| ------ | ------ | ---- | -------------------- |
347| name   | number | Yes  | System configuration parameter name. |
348
349**Return value**
350
351| Type  | Description              |
352| ------ | ------------------ |
353| number | System configuration obtained. |
354
355**Example**
356
357```js
358let _SC_ARG_MAX = 0
359let pres = process.getSystemConfig(_SC_ARG_MAX)
360```
361
362
363## process.getEnvironmentVar<sup>(deprecated)</sup>
364
365getEnvironmentVar(name: string): string
366
367Obtains the value of an environment variable.
368
369> **NOTE**
370>
371> 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.
372
373**System capability**: SystemCapability.Utils.Lang
374
375**Parameters**
376
377| Name | Type  | Mandatory | Description        |
378| ------ | ------ | ---- | ------------ |
379| name   | string | Yes  | Environment variable name. |
380
381**Return value**
382
383| Type  | Description                       |
384| ------ | --------------------------- |
385| string | Value of the environment variable. |
386
387**Example**
388
389```js
390let pres = process.getEnvironmentVar("PATH")
391```
392
393
394## ProcessManager<sup>9+</sup>
395
396Provides APIs for throwing exceptions during the addition of a process.
397
398A **ProcessManager** object is obtained through its own constructor.
399
400### isAppUid<sup>9+</sup>
401
402isAppUid(v: number): boolean
403
404Checks whether a UID belongs to this application.
405
406**Atomic service API**: This API can be used in atomic services since API version 11.
407
408**System capability**: SystemCapability.Utils.Lang
409
410**Parameters**
411
412| Name | Type  | Mandatory | Description           |
413| ------ | ------ | ---- | --------------- |
414| v      | number | Yes  | UID, which can be obtained by running **process.uid**. |
415
416**Return value**
417
418| Type   | Description                                                        |
419| ------- | ------------------------------------------------------------ |
420| boolean | **true**: The UID belongs to the application.<br>**false**: The UID does not belong to the application. |
421
422**Error codes**
423
424For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
425
426| ID | Error Message |
427| -------- | -------- |
428| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
429
430**Example**
431
432```js
433let pro = new process.ProcessManager();
434// Use process.uid to obtain the UID.
435let pres = process.uid;
436let result = pro.isAppUid(pres);
437console.log("result: " + result); // result: true
438```
439
440
441### getUidForName<sup>9+</sup>
442
443getUidForName(v: string): number
444
445Obtains the UID of a user from the user database of the system based on the specified user name.
446
447**Atomic service API**: This API can be used in atomic services since API version 11.
448
449**System capability**: SystemCapability.Utils.Lang
450
451**Parameters**
452
453| Name | Type  | Mandatory | Description    |
454| ------ | ------ | ---- | -------- |
455| v      | string | Yes  | User name. |
456
457**Return value**
458
459| Type  | Description         |
460| ------ | ------------- |
461| number | UID of the user. If the user does not exist, **-1** is returned.|
462
463**Error codes**
464
465For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
466
467| ID | Error Message |
468| -------- | -------- |
469| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
470
471**Example**
472
473```js
474let pro = new process.ProcessManager();
475let pres = pro .getUidForName("tool");
476```
477
478
479### getThreadPriority<sup>9+</sup>
480
481getThreadPriority(v: number): number
482
483Obtains the thread priority based on the specified TID.
484
485**Atomic service API**: This API can be used in atomic services since API version 11.
486
487**System capability**: SystemCapability.Utils.Lang
488
489**Parameters**
490
491| Name | Type  | Mandatory | Description           |
492| ------ | ------ | ---- | --------------- |
493| v      | number | Yes  | TID. |
494
495**Return value**
496
497| Type  | Description                                            |
498| ------ | ------------------------------------------------ |
499| number | Priority of the thread. The priority depends on the operating system. |
500
501**Error codes**
502
503For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
504
505| ID | Error Message |
506| -------- | -------- |
507| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
508
509**Example**
510
511```js
512let pro = new process.ProcessManager();
513let tid = process.tid;
514let pres = pro.getThreadPriority(tid);
515```
516
517
518### getSystemConfig<sup>9+</sup>
519
520getSystemConfig(name: number): number
521
522Obtains the system configuration.
523
524**Atomic service API**: This API can be used in atomic services since API version 11.
525
526**System capability**: SystemCapability.Utils.Lang
527
528**Parameters**
529
530| Name | Type  | Mandatory | Description                |
531| ------ | ------ | ---- | -------------------- |
532| name   | number | Yes  | System configuration parameter name. |
533
534**Return value**
535
536| Type  | Description              |
537| ------ | ------------------ |
538| number | System configuration obtained. If the system configuration does not exist, **-1** is returned. |
539
540**Error codes**
541
542For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
543
544| ID | Error Message |
545| -------- | -------- |
546| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
547
548**Example**
549
550```js
551let pro = new process.ProcessManager();
552let _SC_ARG_MAX = 0;
553let pres = pro.getSystemConfig(_SC_ARG_MAX);
554```
555
556
557### getEnvironmentVar<sup>9+</sup>
558
559getEnvironmentVar(name: string): string
560
561Obtains the value of an environment variable.
562
563> **NOTE**
564>
565>  If the environment variable does not exist, **undefined** is returned.
566
567**Atomic service API**: This API can be used in atomic services since API version 11.
568
569**System capability**: SystemCapability.Utils.Lang
570
571**Parameters**
572
573| Name | Type  | Mandatory | Description        |
574| ------ | ------ | ---- | ------------ |
575| name   | string | Yes  | Environment variable name. |
576
577**Return value**
578
579| Type  | Description                    |
580| ------ | ------------------------ |
581| string | Value of the environment variable. |
582
583**Error codes**
584
585For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
586
587| ID | Error Message |
588| -------- | -------- |
589| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
590
591**Example**
592
593```js
594let pro = new process.ProcessManager();
595let pres = pro.getEnvironmentVar("PATH");
596```
597
598
599### exit<sup>9+</sup>
600
601exit(code: number): void
602
603Terminates this process.
604
605Exercise 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.
606
607**Atomic service API**: This API can be used in atomic services since API version 11.
608
609**System capability**: SystemCapability.Utils.Lang
610
611**Parameters**
612
613| Name | Type  | Mandatory | Description          |
614| ------ | ------ | ---- | -------------- |
615| code   | number | Yes  | Exit code of the process. |
616
617**Error codes**
618
619For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
620
621| ID | Error Message |
622| -------- | -------- |
623| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
624
625**Example**
626
627```js
628let pro = new process.ProcessManager();
629pro.exit(0);
630```
631
632
633### kill<sup>9+</sup>
634
635kill(signal: number, pid: number): boolean
636
637Sends a signal to the specified process to terminate it.
638
639**Atomic service API**: This API can be used in atomic services since API version 11.
640
641**System capability**: SystemCapability.Utils.Lang
642
643**Parameters**
644
645| Name | Type  | Mandatory | Description        |
646| ------ | ------ | ---- | ------------ |
647| signal | number | Yes  | Signal to send. |
648| pid    | number | Yes  | PID of the process, to which the signal will be sent.  |
649
650**Return value**
651
652| Type   | Description                                                        |
653| ------- | ------------------------------------------------------------ |
654| boolean |  **true**: The signal is sent successfully.<br>**false**: The signal fails to be sent. |
655
656**Error codes**
657
658For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
659
660| ID | Error Message |
661| -------- | -------- |
662| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
663
664**Example**
665
666```js
667let pro = new process.ProcessManager();
668let pres = process.pid;
669let result = pro.kill(28, pres);
670```
671