kernel-small-start-user.md 3.0 KB
Newer Older
D
duangavin123 已提交
1
# Startup in User Mode<a name="EN-US_TOPIC_0000001123640059"></a>
D
duangavin123 已提交
2

D
duangavin123 已提交
3 4 5
-   [Startup of the Root Process in User Mode](#section79911135647)
    -   [Startup Process of the Root Process](#section1184317581349)
    -   [Responsibilities of the Root Process](#section1590220321759)
D
duangavin123 已提交
6

D
duangavin123 已提交
7 8 9 10 11
-   [Running Programs in User Mode](#section194576310611)

## Startup of the Root Process in User Mode<a name="section79911135647"></a>

The root process is the first user-mode process in the system. The process ID is 1. The root process is the ancestor of all user-mode processes.
D
duangavin123 已提交
12 13

**Figure  1**  Process tree<a name="fig427516409375"></a>  
D
duangavin123 已提交
14
![](figures/process-tree.png "process-tree")
D
duangavin123 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

### Startup Process of the Root Process<a name="section1184317581349"></a>

Use the link script to place the following init startup code to the specified location in the system image.

```
#define LITE_USER_SEC_ENTRY   __attribute__((section(".user.entry")))
LITE_USER_SEC_ENTRY VOID OsUserInit(VOID *args)
{
#ifdef LOSCFG_KERNEL_DYNLOAD
    sys_call3(__NR_execve, (UINTPTR)g_initPath, 0, 0);
#endif
    while (true) {
    }
}
```

D
duangavin123 已提交
32
During system startup,  **OsUserInitProcess**  is called to start the  **init**  process. The procedure is as follows:
D
duangavin123 已提交
33 34 35 36 37 38 39 40 41

1.  The kernel calls  **OsLoadUserInit**  to load the code.
2.  A process space is created to start the  **/bin/init**  process.

### Responsibilities of the Root Process<a name="section1590220321759"></a>

-   Starts key system programs or services, such as shell.

    >![](../public_sys-resources/icon-note.gif) **NOTE:** 
42
    >In OpenHarmony, the  **init**  process reads the  **/etc/init.cfg**  file and runs specified commands or starts specified processes based on configurations. For details, see  [init Module](../subsystems/subsys-boot-init.md).
D
duangavin123 已提交
43 44 45 46


-   Monitors the process for reclaiming the orphan process and clears the zombie processes in child processes.

D
duangavin123 已提交
47
## Running Programs in User Mode<a name="section194576310611"></a>
D
duangavin123 已提交
48

D
duangavin123 已提交
49
Common compilation modes of user-mode programs include:
D
duangavin123 已提交
50

D
duangavin123 已提交
51 52
1.  [Compilation based on the framework](../quick-start/quickstart-lite-steps-hi3516-running.md)
2.  Manual compilation
D
duangavin123 已提交
53 54 55 56 57 58 59

    Example:

    ```
    clang --target=arm-liteos --sysroot=prebuilts/lite/sysroot -o helloworld helloworld.c
    ```

60
    Before running the  **clang**  command, install the LLVM compiler. For details, see  [Installing LLVM](../quick-start/quickstart-lite-steps-hi3861-setting.md).
D
duangavin123 已提交
61

D
duangavin123 已提交
62
    **--target=arm-liteos**: specifies the compilation platform, which is arm-liteos.
D
duangavin123 已提交
63 64 65 66

    **--sysroot=$\{YOUR\_ROOT\_PATH\}/prebuilts/lite/sysroot**: specifies the directory in which you can search for the header file and the dependent standard libraries.


D
duangavin123 已提交
67
A user-mode program can be started in either of the following ways:
D
duangavin123 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

-   Run the shell command to start the process.

    ```
    OHOS $ exec helloworld
    OHOS $ ./helloworld
    OHOS $ /bin/helloworld
    ```


-   Start a new process by calling the POSIX API.

    Use the  **Fork\(\)**  method to create a process, and call the  **exec\(\)**  method to execute a new process.