Startup in User Space

Startup of the Root Process in User Space

The root process is the first user-space process in the system. The process ID is 1. The root process is the ancestor of all user-space processes.

Figure 1 Process tree

Startup Process of the Root Process

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) {
    }
}

During system startup, OsUserInitProcess is called to start the init process. The process is as follows:

  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

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

    NOTE: 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.

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

Running Programs in User Space

Common compilation modes of user-space programs include:

  1. Compilation using the framework

  2. Manual compilation

    Example:

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

    Before running the clang command, install the LLVM compiler. For details, see Installing LLVM.

    --target=arm-liteos: specifies that the compilation platform is arm-liteos.

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

A user-space program can be started in either of the following ways:

  • 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.