未验证 提交 36e8957a 编写于 作者: O openharmony_ci 提交者: Gitee

!15673 翻译完成:14822 process模块的tid的实际含义为:当前线程

Merge pull request !15673 from wusongqing/TR14822
...@@ -18,139 +18,117 @@ import process from '@ohos.process'; ...@@ -18,139 +18,117 @@ import process from '@ohos.process';
| Name| Type| Readable| Writable| Description| | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- | -------- |
| egid | number | Yes| No| Effective group identifier (EGID) of a process.<br>**System API**: This is a system API.<br>It is used only to test applications.| | egid | number | Yes| No| Effective group identifier (EGID) of the process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| euid | number | Yes| No| Effective user identifier (EUID) of a process.<br>**System API**: This is a system API.<br>It is used only to test applications.| | euid | number | Yes| No| Effective user identifier (EUID) of the process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| gid | number | Yes| No| Group identifier (GID) of a process.<br>**System API**: This is a system API.<br>It is used only to test applications.| | gid | number | Yes| No| Group identifier (GID) of the process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| uid | number | Yes| No| User identifier (UID) of a process.| | uid | number | Yes| No| User identifier (UID) of the process.|
| groups | number[] | Yes| No| Array with supplementary group IDs.<br>**System API**: This is a system API.<br>It is used only to test applications.| | groups | number[] | Yes| No| Array with supplementary group IDs.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| pid | number | Yes| No| Process ID (PID) of a process.| | pid | number | Yes| No| Process ID (PID) of the process.|
| ppid | number | Yes| No| Parent process ID (PPID) of a process.<br>**System API**: This is a system API.<br>It is used only to test applications.| | ppid | number | Yes| No| Parent process ID (PPID) of the process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| tid<sup>8+</sup> | number | Yes| No| Thread ID (TID) of a process.| | tid<sup>8+</sup> | number | Yes| No| Thread ID (TID) of the thread.|
## ProcessManager<sup>9+</sup> ## EventListener
Provides APIs for throwing exceptions during the addition of a process. **System capability**: SystemCapability.Utils.Lang
### isAppUid<sup>9+</sup> | Name| Description|
| -------- | -------- |
| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.|
isAppUid(v: number): boolean
Checks whether a UID belongs to this application. ## process.isIsolatedProcess<sup>8+</sup>
**System capability**: SystemCapability.Utils.Lang isIsolatedProcess(): boolean
**Parameters** Checks whether this process is isolated.
| Name| Type| Mandatory| Description| **System capability**: SystemCapability.Utils.Lang
| -------- | -------- | -------- | -------- |
| v | number | Yes| UID.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the UID is the application's UID; returns **false** otherwise.| | boolean | Returns **true** if the process is isolated; returns **false** otherwise.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); let result = process.isIsolatedProcess();
let result = pro.isAppUid(688);
``` ```
### getUidForName<sup>9+</sup> ## process.is64Bit<sup>8+</sup>
getUidForName(v: string): number is64Bit(): boolean
Obtains the process UID based on the process name. Checks whether this process is running in a 64-bit environment.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | string | Yes| Name of a process.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Process UID.| | boolean | Returns **true** if the process is running in a 64-bit environment; returns **false** otherwise.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); let result = process.is64Bit();
let pres = pro .getUidForName("tool");
``` ```
### getThreadPriority<sup>9+</sup> ## process.getStartRealtime<sup>8+</sup>
getThreadPriority(v: number): number getStartRealtime(): number
Obtains the thread priority based on the specified TID. Obtains the duration, in milliseconds, from the time the system starts to the time the process starts.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Priority of the thread.| | number | Duration obtained, in millisecond.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); let realtime = process.getStartRealtime();
let tid = process.tid;
let pres = pro.getThreadPriority(tid);
``` ```
## process.getPastCpuTime<sup>8+</sup>
### getSystemConfig<sup>9+</sup> getPastCpuTime(): number
getSystemConfig(name: number): number
Obtains the system configuration. Obtains the CPU time (in milliseconds) from the time the process starts to the current time.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | number | Yes| System configuration parameter name.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | System configuration obtained.| | number | CPU time obtained, in millisecond.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); let result = process.getPastCpuTime() ;
let _SC_ARG_MAX = 0;
let pres = pro.getSystemConfig(_SC_ARG_MAX);
``` ```
### getEnvironmentVar<sup>9+</sup> ## process.runCmd
getEnvironmentVar(name: string): string runCmd(command: string, options?: { timeout?: number, killSignal?: number | string, maxBuffer?: number }): ChildProcess
Obtains the value of an environment variable. Forks a new process to run a shell command and returns the **ChildProcess** object.
**System API**: This is a system API.
It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -158,29 +136,58 @@ Obtains the value of an environment variable. ...@@ -158,29 +136,58 @@ Obtains the value of an environment variable.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| name | string | Yes| Environment variable name.| | command | string | Yes| Shell command to run.|
| options | Object | No| Related parameters.|
**Table 1** options
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| timeout | number | No| Maximum running time (in milliseconds) of the child process. When the running time of the child process exceeds the value of this parameter, the parent process sends a **killSignal** to the child process to terminate it. The default value is **0**.|
| killSignal | number&nbsp;\|&nbsp;string | No| Signal sent to the child process when the running time of a child process exceeds the timeout period. The default value is **SIGTERM**.|
| maxBuffer | number | No| Maximum buffer size for the standard input and output of the child process. When the size is exceeded, the child process will be terminated. The default value is **1024 \* 1024**.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| string | Value of the environment variable.| | [ChildProcess](#childprocess) | **ChildProcess** object.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); let child = process.runCmd('ls', { maxBuffer : 2 });
let pres = pro.getEnvironmentVar("PATH"); let result = child.wait();
child.getOutput.then(val=>{
console.log("child.getOutput = " + val);
})
``` ```
### exit<sup>9+</sup> ## process.abort
exit(code: number): void abort(): void
Terminates this process. 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
Exercise caution when using this API. **Example**
```js
process.abort();
```
## process.on
on(type: string, listener: EventListener): void
Stores the events triggered by the user.
**System API**: This is a system API.
It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -188,21 +195,27 @@ Exercise caution when using this API. ...@@ -188,21 +195,27 @@ Exercise caution when using this API.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| code | number | Yes| Exit code of the process.| | type | string | Yes| Type of the events to store. |
| listener | [EventListener](#eventlistener) | Yes| Callback invoked to return the event.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); process.on("data", (e)=>{
pro.exit(0); console.log("data callback");
})
``` ```
### kill<sup>9+</sup> ## process.off
kill(signal: number, pid: number): boolean off(type: string): boolean
Sends a signal to the specified process to terminate it. Deletes the event stored by the user.
**System API**: This is a system API.
It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -210,45 +223,29 @@ Sends a signal to the specified process to terminate it. ...@@ -210,45 +223,29 @@ Sends a signal to the specified process to terminate it.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| pid | number | Yes| PID of the process, to which the signal will be sent.| | type | string | Yes| Type of the event to delete.|
| signal | number | Yes| Signal to send.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.| | boolean | Returns **true** if the event is deleted; returns **false** otherwise.|
**Example** **Example**
```js ```js
let pro = new process.ProcessManager(); process.on("data", (e)=>{
let pres = process.pid; console.log("data callback");
let result = pro.kill(28, pres); })
let result = process.off("data");
``` ```
## ChildProcess ## process.cwd
Allows a process to obtain the standard input and output of its child processes, send signals, and close its child processes.
### Attributes
**System capability**: SystemCapability.Utils.Lang
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| pid | number | Yes| No| PID of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| ppid | number | Yes| No| PPID of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| exitCode | number | Yes| No| Exit code of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| killed | boolean | Yes| No| Whether the parent process successfully sends a signal to the child process to terminate it.<br>**System API**: This is a system API.<br>It is used only to test applications.|
### wait
wait(): Promise&lt;number&gt; cwd(): string
Waits until the child process ends. This method uses a promise to return the exit code of the child process. Obtains the working directory of this process.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -260,24 +257,20 @@ It is used only to test applications. ...@@ -260,24 +257,20 @@ It is used only to test applications.
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;number&gt; | Promise used to return the exit code of the child process.| | string| Working directory obtained.|
**Example** **Example**
```js ```js
let child = process.runCmd('ls'); let path = process.cwd();
let result = child.wait();
result.then(val=>{
console.log("result = " + val);
})
``` ```
### getOutput ## process.chdir
getOutput(): Promise&lt;Uint8Array&gt; chdir(dir: string): void
Obtains the standard output of the child process. Changes the working directory of this process.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -285,81 +278,109 @@ It is used only to test applications. ...@@ -285,81 +278,109 @@ It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| dir | string | Yes| Path|
**Example**
```js
process.chdir('/system');
```
## process.uptime
uptime(): number
Obtains the running time of this process.
**System capability**: SystemCapability.Utils.Lang
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.| | number | Running time of the process, in seconds.|
**Example** **Example**
```js ```js
let child = process.runCmd('ls'); let time = process.uptime();
let result = child.wait();
child.getOutput().then(val=>{
console.log("child.getOutput = " + val);
})
``` ```
### getErrorOutput ## process.kill<sup>(deprecated)</sup>
getErrorOutput(): Promise&lt;Uint8Array&gt; kill(signal: number, pid: number): boolean
Obtains the standard error output of the child process.
**System API**: This is a system API. Sends a signal to the specified process to terminate it.
It is used only to test applications. > **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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**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.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.| | boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
**Example** **Example**
```js ```js
let child = process.runCmd('madir test.text'); let pres = process.pid
let result = child.wait(); let result = process.kill(28, pres)
child.getErrorOutput().then(val=>{
console.log("child.getErrorOutput= " + val);
})
``` ```
### close ## process.exit<sup>(deprecated)</sup>
close(): void exit(code: number): void
Closes the child process in running. Terminates this process.
**System API**: This is a system API. 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.
It is used only to test applications. > **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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| code | number | Yes| Exit code of the process.|
**Example** **Example**
```js ```js
let child = process.runCmd('sleep 5; ls'); process.exit(0);
child.close();
``` ```
### kill ## process.getUidForName<sup>(deprecated)</sup>
kill(signal: number | string): void
Sends a signal to the specified child process to terminate it. getUidForName(v: string): number
**System API**: This is a system API. Obtains the process UID based on the process name.
It is used only to test applications. > **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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -367,43 +388,63 @@ It is used only to test applications. ...@@ -367,43 +388,63 @@ It is used only to test applications.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.| | v | string | Yes| Name of a process.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Process UID.|
**Example** **Example**
```js ```js
let child = process.runCmd('sleep 5; ls'); let pres = process.getUidForName("tool")
child.kill(9);
``` ```
## process.isIsolatedProcess<sup>8+</sup> ## process.getThreadPriority<sup>(deprecated)</sup>
isIsolatedProcess(): boolean getThreadPriority(v: number): number
Checks whether this process is isolated. 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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the process is isolated; returns **false** otherwise.| | number | Priority of the thread.|
**Example** **Example**
```js ```js
let result = process.isIsolatedProcess(); let tid = process.tid;
let pres = process.getThreadPriority(tid);
``` ```
## process.isAppUid<sup>8+</sup> ## process.isAppUid<sup>(deprecated)</sup>
isAppUid(v: number): boolean isAppUid(v: number): boolean
Checks whether a UID belongs to this application. Checks whether a UID belongs to this application.
> **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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** **Parameters**
...@@ -416,7 +457,7 @@ Checks whether a UID belongs to this application. ...@@ -416,7 +457,7 @@ Checks whether a UID belongs to this application.
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the UID is the application's UID; returns **false** otherwise.| | boolean | Returns **true** if the UID belongs to the application; returns **false** otherwise.|
**Example** **Example**
...@@ -425,32 +466,47 @@ let result = process.isAppUid(688); ...@@ -425,32 +466,47 @@ let result = process.isAppUid(688);
``` ```
## process.is64Bit<sup>8+</sup> ## process.getSystemConfig<sup>(deprecated)</sup>
is64Bit(): boolean getSystemConfig(name: number): number
Checks whether this process is running in a 64-bit environment. 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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name | number | Yes| System configuration parameter name.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the process is running in a 64-bit environment; returns **false** otherwise.| | number | System configuration obtained.|
**Example** **Example**
```js ```js
let result = process.is64Bit(); let _SC_ARG_MAX = 0
let pres = process.getSystemConfig(_SC_ARG_MAX)
``` ```
## process.getUidForName<sup>8+</sup> ## process.getEnvironmentVar<sup>(deprecated)</sup>
getUidForName(v: string): number getEnvironmentVar(name: string): string
Obtains the process UID based on the process name. 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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -458,26 +514,32 @@ Obtains the process UID based on the process name. ...@@ -458,26 +514,32 @@ Obtains the process UID based on the process name.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| v | string | Yes| Name of a process.| | name | string | Yes| Environment variable name.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Process UID.| | string | Value of the environment variable.|
**Example** **Example**
```js ```js
let pres = process.getUidForName("tool") let pres = process.getEnvironmentVar("PATH")
``` ```
## process.getThreadPriority<sup>8+</sup> ## 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>
getThreadPriority(v: number): number isAppUid(v: number): boolean
Obtains the thread priority based on the specified TID. Checks whether a UID belongs to this application.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -485,64 +547,80 @@ Obtains the thread priority based on the specified TID. ...@@ -485,64 +547,80 @@ Obtains the thread priority based on the specified TID.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| v | number | Yes| TID.| | v | number | Yes| UID.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Priority of the thread.| | boolean | Returns **true** if the UID belongs to the application; returns **false** otherwise.|
**Example** **Example**
```js ```js
let tid = process.tid; let pro = new process.ProcessManager();
let pres = process.getThreadPriority(tid); let result = pro.isAppUid(688);
``` ```
## process.getStartRealtime<sup>8+</sup> ### getUidForName<sup>9+</sup>
getStartRealtime(): number getUidForName(v: string): number
Obtains the duration, in milliseconds, from the time the system starts to the time the process starts. Obtains the process UID based on the process name.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | string | Yes| Name of a process.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | Duration obtained.| | number | Process UID.|
**Example** **Example**
```js ```js
let realtime = process.getStartRealtime(); let pro = new process.ProcessManager();
let pres = pro .getUidForName("tool");
``` ```
## process.getPastCpuTime<sup>8+</sup>
getPastCpuTime(): number ### getThreadPriority<sup>9+</sup>
Obtains the CPU time (in milliseconds) from the time the process starts to the current time. getThreadPriority(v: number): number
Obtains the thread priority based on the specified TID.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| v | number | Yes| TID.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| number | CPU time obtained.| | number | Priority of the thread.|
**Example** **Example**
```js ```js
let result = process.getPastCpuTime() ; let pro = new process.ProcessManager();
let tid = process.tid;
let pres = pro.getThreadPriority(tid);
``` ```
## process.getSystemConfig<sup>8+</sup> ### getSystemConfig<sup>9+</sup>
getSystemConfig(name: number): number getSystemConfig(name: number): number
...@@ -565,12 +643,13 @@ Obtains the system configuration. ...@@ -565,12 +643,13 @@ Obtains the system configuration.
**Example** **Example**
```js ```js
let _SC_ARG_MAX = 0 let pro = new process.ProcessManager();
let pres = process.getSystemConfig(_SC_ARG_MAX) let _SC_ARG_MAX = 0;
let pres = pro.getSystemConfig(_SC_ARG_MAX);
``` ```
## process.getEnvironmentVar<sup>8+</sup> ### getEnvironmentVar<sup>9+</sup>
getEnvironmentVar(name: string): string getEnvironmentVar(name: string): string
...@@ -593,19 +672,18 @@ Obtains the value of an environment variable. ...@@ -593,19 +672,18 @@ Obtains the value of an environment variable.
**Example** **Example**
```js ```js
let pres = process.getEnvironmentVar("PATH") let pro = new process.ProcessManager();
let pres = pro.getEnvironmentVar("PATH");
``` ```
## process.runCmd ### exit<sup>9+</sup>
runCmd(command: string, options?: { timeout?: number, killSignal?: number | string, maxBuffer?: number }): ChildProcess
Forks a new process to run a shell command and returns the **ChildProcess** object. exit(code: number): void
**System API**: This is a system API. Terminates this process.
It is used only to test applications. 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.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
...@@ -613,54 +691,71 @@ It is used only to test applications. ...@@ -613,54 +691,71 @@ It is used only to test applications.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| command | string | Yes| Shell command to run.| | code | number | Yes| Exit code of the process.|
| options | Object | No| Related parameters.|
**Table 1** options **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**
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| timeout | number | No| Maximum running time (in ms) of the child process. When the running time of the child process exceeds the value of this parameter, the parent process sends a **killSignal** to the child process to terminate it. The default value is **0**.| | pid | number | Yes| PID of the process, to which the signal will be sent.|
| killSignal | number&nbsp;\|&nbsp;string | No| Signal sent to the child process when the running time of a child process exceeds the timeout period. The default value is **SIGTERM**.| | signal | number | Yes| Signal to send.|
| maxBuffer | number | No| Maximum buffer size for the standard input and output of the child process. When the size is exceeded, the child process will be terminated. The default value is **1024 \* 1024**.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| [ChildProcess](#childprocess) | **ChildProcess** object.| | boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
**Example** **Example**
```js ```js
let child = process.runCmd('ls', { maxBuffer : 2 }); let pro = new process.ProcessManager();
let result = child.wait(); let pres = process.pid;
child.getOutput.then(val=>{ let result = pro.kill(28, pres);
console.log("child.getOutput = " + val);
})
``` ```
## process.abort ## ChildProcess
abort(): void The **ChildProcess** object is a new child process and can be obtained by calling [process.runCmd](#processruncmd). The main process can obtain the standard input and output of the child process, send signals to the child process, and close the child process.
Aborts a process and generates a core file. This method will cause a process to exit immediately. Exercise caution when using this method. **System API**: This is a system API.
**System capability**: SystemCapability.Utils.Lang ### Attributes
**Example** **System API**: This is a system API.
```js **System capability**: SystemCapability.Utils.Lang
process.abort();
``` | Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| pid | number | Yes| No| PID of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| ppid | number | Yes| No| PPID of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| exitCode | number | Yes| No| Exit code of the child process.<br>**System API**: This is a system API.<br>It is used only to test applications.|
| killed | boolean | Yes| No| Whether the parent process successfully sends a signal to the child process to terminate it.<br>**System API**: This is a system API.<br>It is used only to test applications.|
## process.on ### wait
on(type: string, listener: EventListener): void wait(): Promise&lt;number&gt;
Stores the events triggered by the user. Waits until the child process ends. This method uses a promise to return the exit code of the child process.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -668,33 +763,28 @@ It is used only to test applications. ...@@ -668,33 +763,28 @@ It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** **Return value**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the events to store. |
| listener | EventListener | Yes| Callback invoked to return the event.|
**Table 2** EventListener
| Name| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| EventListener&nbsp;=&nbsp;(evt: &nbsp;Object)&nbsp;=&gt;&nbsp;void | Event to store.| | Promise&lt;number&gt; | Promise used to return the exit code of the child process.|
**Example** **Example**
```js ```js
process.on("data", (e)=>{ let child = process.runCmd('ls');
console.log("data callback"); let result = child.wait();
result.then(val=>{
console.log("result = " + val);
}) })
``` ```
## process.off ### getOutput
off(type: string): boolean getOutput(): Promise&lt;Uint8Array&gt;
Deletes the event stored by the user. Obtains the standard output of the child process.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -702,56 +792,57 @@ It is used only to test applications. ...@@ -702,56 +792,57 @@ It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| type | string | Yes| Type of the event to delete.|
**Return value** **Return value**
| Type| Description| | Type| Description|
| -------- | -------- | | -------- | -------- |
| boolean | Returns **true** if the event is deleted; returns **false** otherwise.| | Promise&lt;Uint8Array&gt; | Promise used to return the standard output in a Uint8Array.|
**Example** **Example**
```js ```js
process.on("data", (e)=>{ let child = process.runCmd('ls');
console.log("data callback"); let result = child.wait();
child.getOutput().then(val=>{
console.log("child.getOutput = " + val);
}) })
let result = process.off("data");
``` ```
## process.exit ### getErrorOutput
exit(code: number): void getErrorOutput(): Promise&lt;Uint8Array&gt;
Terminates this process. Obtains the standard error output of the child process.
**System API**: This is a system API.
Exercise caution when using this API. It is used only to test applications.
**System capability**: SystemCapability.Utils.Lang **System capability**: SystemCapability.Utils.Lang
**Parameters** **Return value**
| Name| Type| Mandatory| Description| | Type| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- |
| code | number | Yes| Exit code of the process.| | Promise&lt;Uint8Array&gt; | Promise used to return the standard error output in a Uint8Array.|
**Example** **Example**
```js ```js
process.exit(0); let child = process.runCmd('madir test.text');
let result = child.wait();
child.getErrorOutput().then(val=>{
console.log("child.getErrorOutput= " + val);
})
``` ```
## process.cwd ### close
cwd(): string close(): void
Obtains the working directory of this process. Closes the child process in running.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -762,15 +853,16 @@ It is used only to test applications. ...@@ -762,15 +853,16 @@ It is used only to test applications.
**Example** **Example**
```js ```js
let path = process.cwd(); let child = process.runCmd('sleep 5; ls');
child.close();
``` ```
## process.chdir ### kill
chdir(dir: string): void kill(signal: number | string): void
Changes the working directory of this process. Sends a signal to the specified child process to terminate it.
**System API**: This is a system API. **System API**: This is a system API.
...@@ -782,60 +874,11 @@ It is used only to test applications. ...@@ -782,60 +874,11 @@ It is used only to test applications.
| Name| Type| Mandatory| Description| | Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| dir | string | Yes| Path| | signal | number&nbsp;\|&nbsp;string | Yes| Number or string to send.|
**Example**
```js
process.chdir('/system');
```
## process.uptime
uptime(): number
Obtains the running time of this process.
**System capability**: SystemCapability.Utils.Lang
**Return value**
| Type| Description|
| -------- | -------- |
| number | Running time of the process, in seconds.|
**Example**
```js
let time = process.uptime();
```
## process.kill
kill(signal: number, pid: number): boolean
Sends a signal to the specified process to terminate it.
**System capability**: SystemCapability.Utils.Lang
**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.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the signal is sent successfully; returns **false** otherwise.|
**Example** **Example**
```js ```js
let pres = process.pid let child = process.runCmd('sleep 5; ls');
let result = process.kill(28, pres) child.kill(9);
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册