Process

Basic Concepts

A process is the minimum unit for system resource management. The process module provided by the OpenHarmony LiteOS-A kernel is used to isolate user-space processes. The kernel space is considered as a process space and does not have other processes except KIdle, which is an idle process provided by the system and shares the same process space with KProcess.

  • The OpenHarmony process module allows multiple processes to run simultaneously, switch, and communicate, facilitating your management over service programs.
  • The OpenHarmony processes use the preemption scheduling mechanism. The process with a higher priority is scheduled over the process with a lower priority. Time slice round-robin is used to schedule processes with the same priority.
  • The OpenHarmony processes are assigned 32 priorities (0 to 31). Among them, user processes can be configured with 22 priorities from 10 (highest) to 31 (lowest).
  • A higher-priority process can preempt the resources of a lower-priority process. The lower-priority process can be scheduled only after the higher-priority process is blocked or terminated.
  • Each user-space process has its own memory space, which is invisible to other processes. In this way, processes are isolated from each other.
  • The user-space root process init is created by the kernel. Other user-space processes are created by the init process via the fork call.

Process States:

  • Init: The process is being created.

  • Ready: The process is in the Ready queue and waits for being scheduled by the CPU.

  • Running: The process is running.

  • Pending: The process is blocked and suspended. When all threads in a process are blocked, the process is blocked and suspended.

  • Zombies: The process stops running and waits for the parent process to reclaim its control block resources.

Figure 1 Process state transition

Process State Transition:

  • Init→Ready:

    When a process is created, the process enters the Init state to start initialization after obtaining the process control block. After the process is initialized, the process is inserted into the scheduling queue and therefore enters the Ready state.

  • Ready→Running:

    When a process switchover is triggered, the process with the highest priority in the Ready queue is executed and enters the Running state. If this process has no thread in the Ready state, the process is deleted from the Ready queue and resides only in the Running state. However, if it has threads in the Ready state, the process still stays in the Ready queue. In this case, the process is in both the Ready and Running states, but presented as the Running state.

  • Running→Pending:

    When the last thread of a process enters the Pending state, all threads in the process are in the Pending state. Then, the process enters the Pending state, and process switching occurs.

  • Pending→Ready:

    When any thread in a Pending process restores to the Ready state, the process is added to the Ready queue and changes to the Ready state.

  • Ready→Pending:

    When the last ready thread in a process enters the Pending state, the process is deleted from the Ready queue, and the process changes from the Ready state to the Pending state.

  • Running→Ready:

    A process may change from the Running state to the Ready state in either of the following scenarios:

    1. After a process with a higher priority is created or restored, processes will be scheduled. The process with the highest priority in the Ready queue will change to the Running state, and the originally running process will change from the Running state to the Ready state.
    2. If a process has the LOS_SCHED_RR scheduling policy and shares the same priority with another process in the Ready state, this process will change from the Running state to the Ready state after its time slices are used up, and the other process with the same priority will change from the Ready state to the Running state.
  • Running→Zombies:

    After the main thread or all threads of a process are stopped, the process changes from the Running state to the Zombies state and waits for the parent process to reclaim resources.

Working Principles

The OpenHarmony process module is used to isolate user-space processes and supports the following functions: creating and exiting user-space processes, reclaiming process resources, setting and obtaining scheduling parameters and process group IDs, and obtaining process IDs.

A user-space process is created by forking a parent process. During forking, the virtual memory space of the parent process is cloned to the child process. When the child process is running, the content of the parent process is copied to the virtual memory space of the child process as required through the copy-on-write mechanism.

A process is only a resource management unit, and the actual running is executed by threads in the process. When threads in different processes switch with each other, the process space is switched.

Figure 2 Process management

Development Guidelines

Available APIs

Table 1 Process management module APIs

Category

API

Description

Process scheduling parameter control

LOS_GetProcessScheduler

Obtains the scheduling policy of the specified process.

LOS_SetProcessScheduler

Sets the scheduling parameters, including the priority and scheduling policy, for the specified process.

LOS_GetProcessPriority

Obtains the priority of the specified process.

LOS_SetProcessPriority

Sets the priority of the specified process.

Waiting for reclaiming child processes

LOS_Wait

Waits for the specified child process to terminate and reclaims its resources.

Process group

LOS_GetProcessGroupID

Obtains the process group ID of the specified process.

LOS_GetCurrProcessGroupID

Obtains the process group ID of the current process.

Obtaining the process ID.

LOS_GetCurrProcessID

Obtains the ID of the current process.

User and user group

LOS_GetUserID

Obtains the user ID of the current process.

LOS_GetGroupID

Obtains the user group ID of the current process.

LOS_CheckInGroups

Checks whether the specified user group ID is in the user group of the current process.

Maximum number of processes supported

LOS_GetSystemProcessMaximum

Obtains the maximum number of processes supported by the system.

How to Develop

Kernel-space processes cannot be created. Therefore, kernel-space process development is not involved.

NOTE:

  • The number of idle threads depends on the number of CPU cores. Each CPU has a corresponding idle thread.
  • Except KProcess and KIdle, other kernel-space processes cannot be created.
  • The thread created by calling a user-space process in the kernel space is a KProcess, not a user-space process.