更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_process.c 文件参考

进程模块主文件 更多...

浏览源代码.

函数

STATIC INLINE VOID OsInsertPCBToFreeList (LosProcessCB *processCB)
 将进程插入到空闲链表中 更多...
 
VOID OsDeleteTaskFromProcess (LosTaskCB *taskCB)
 
UINT32 OsProcessAddNewTask (UINT32 pid, LosTaskCB *taskCB, SchedParam *param)
 
STATIC ProcessGroupOsCreateProcessGroup (UINT32 pid)
 创建进程组 更多...
 
STATIC VOID OsExitProcessGroup (LosProcessCB *processCB, ProcessGroup **group)
 
STATIC ProcessGroupOsFindProcessGroup (UINT32 gid)
 
STATIC INT32 OsSendSignalToSpecifyProcessGroup (ProcessGroup *group, siginfo_t *info, INT32 permission)
 
LITE_OS_SEC_TEXT INT32 OsSendSignalToAllProcess (siginfo_t *info, INT32 permission)
 
LITE_OS_SEC_TEXT INT32 OsSendSignalToProcessGroup (INT32 pid, siginfo_t *info, INT32 permission)
 
STATIC LosProcessCBOsFindGroupExitProcess (ProcessGroup *group, INT32 pid)
 
STATIC UINT32 OsFindChildProcess (const LosProcessCB *processCB, INT32 childPid)
 
STATIC LosProcessCBOsFindExitChildProcess (const LosProcessCB *processCB, INT32 childPid)
 
VOID OsWaitWakeTask (LosTaskCB *taskCB, UINT32 wakePID)
 
STATIC BOOL OsWaitWakeSpecifiedProcess (LOS_DL_LIST *head, const LosProcessCB *processCB, LOS_DL_LIST **anyList)
 
STATIC VOID OsWaitCheckAndWakeParentProcess (LosProcessCB *parentCB, const LosProcessCB *processCB)
 
LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree (LosProcessCB *processCB)
 
LITE_OS_SEC_TEXT STATIC VOID OsRecycleZombiesProcess (LosProcessCB *childCB, ProcessGroup **group)
 
STATIC VOID OsDealAliveChildProcess (LosProcessCB *processCB)
 
STATIC VOID OsChildProcessResourcesFree (const LosProcessCB *processCB)
 
VOID OsProcessNaturalExit (LosProcessCB *processCB, UINT32 status)
 
STATIC UINT32 OsProcessInit (VOID)
 
LITE_OS_SEC_TEXT VOID OsProcessCBRecycleToFree (VOID)
 
STATIC VOID OsDeInitPCB (LosProcessCB *processCB)
 
UINT32 OsSetProcessName (LosProcessCB *processCB, const CHAR *name)
 
STATIC UINT32 OsInitPCB (LosProcessCB *processCB, UINT32 mode, const CHAR *name)
 
STATIC UserOsCreateUser (UINT32 userID, UINT32 gid, UINT32 size)
 
LITE_OS_SEC_TEXT BOOL LOS_CheckInGroups (UINT32 gid)
 
LITE_OS_SEC_TEXT INT32 LOS_GetUserID (VOID)
 
LITE_OS_SEC_TEXT INT32 LOS_GetGroupID (VOID)
 
STATIC UINT32 OsProcessCreateInit (LosProcessCB *processCB, UINT32 flags, const CHAR *name)
 
LITE_OS_SEC_TEXT_INIT UINT32 OsSystemProcessCreate (VOID)
 
STATIC INLINE INT32 OsProcessSchedlerParamCheck (INT32 which, INT32 pid, UINT16 prio, UINT16 policy)
 进程调度参数检查 更多...
 
STATIC BOOL OsProcessCapPermitCheck (const LosProcessCB *processCB, const SchedParam *param, UINT16 prio)
 
LITE_OS_SEC_TEXT INT32 OsSetProcessScheduler (INT32 which, INT32 pid, UINT16 prio, UINT16 policy)
 设置进程调度计划 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_SetProcessScheduler (INT32 pid, UINT16 policy, UINT16 prio)
 设置指定进程的调度参数,包括优先级和调度策略 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_GetProcessScheduler (INT32 pid)
 获得指定进程的调度策略 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_SetProcessPriority (INT32 pid, UINT16 prio)
 接口封装 - 设置进程优先级 更多...
 
LITE_OS_SEC_TEXT INT32 OsGetProcessPriority (INT32 which, INT32 pid)
 接口封装 - 获取进程优先级 which:标识进程,进程组,用户 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_GetProcessPriority (INT32 pid)
 接口封装 - 获取指定进程优先级 更多...
 
STATIC VOID OsWaitInsertWaitListInOrder (LosTaskCB *runTask, LosProcessCB *processCB)
 
STATIC UINT32 OsWaitSetFlag (const LosProcessCB *processCB, INT32 pid, LosProcessCB **child)
 设置等待子进程退出方式方法 更多...
 
STATIC UINT32 OsWaitRecycleChildProcess (const LosProcessCB *childCB, UINT32 intSave, INT32 *status, siginfo_t *info)
 等待回收孩子进程 @note_thinking 这样写Porcess不太好吧 更多...
 
STATIC UINT32 OsWaitChildProcessCheck (LosProcessCB *processCB, INT32 pid, LosProcessCB **childCB)
 检查要等待的孩子进程 更多...
 
STATIC UINT32 OsWaitOptionsCheck (UINT32 options)
 
STATIC INT32 OsWait (INT32 pid, USER INT32 *status, USER siginfo_t *info, UINT32 options, VOID *rusage)
 等待子进程结束并回收子进程,返回已经终止的子进程的进程ID号,并清除僵死进程。 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_Wait (INT32 pid, USER INT32 *status, UINT32 options, VOID *rusage)
 
STATIC UINT32 OsWaitidOptionsCheck (UINT32 options)
 
LITE_OS_SEC_TEXT INT32 LOS_Waitid (INT32 pid, USER siginfo_t *info, UINT32 options, VOID *rusage)
 
STATIC UINT32 OsSetProcessGroupCheck (const LosProcessCB *processCB, UINT32 gid)
 设置进程组检查 更多...
 
STATIC UINT32 OsSetProcessGroupIDUnsafe (UINT32 pid, UINT32 gid, ProcessGroup **group)
 
LITE_OS_SEC_TEXT INT32 OsSetProcessGroupID (UINT32 pid, UINT32 gid)
 
LITE_OS_SEC_TEXT INT32 OsSetCurrProcessGroupID (UINT32 gid)
 
LITE_OS_SEC_TEXT INT32 LOS_GetProcessGroupID (UINT32 pid)
 
LITE_OS_SEC_TEXT INT32 LOS_GetCurrProcessGroupID (VOID)
 获取当前进程的组ID 更多...
 
STATIC LosProcessCBOsGetFreePCB (VOID)
 为用户态任务分配栈空间 更多...
 
STATIC VOID * OsUserInitStackAlloc (LosProcessCB *processCB, UINT32 *size)
 
LITE_OS_SEC_TEXT VOID OsExecProcessVmSpaceRestore (LosVmSpace *oldSpace)
 
LITE_OS_SEC_TEXT LosVmSpaceOsExecProcessVmSpaceReplace (LosVmSpace *newSpace, UINTPTR stackBase, INT32 randomDevFD)
 
LITE_OS_SEC_TEXT UINT32 OsExecRecycleAndInit (LosProcessCB *processCB, const CHAR *name, LosVmSpace *oldSpace, UINTPTR oldFiles)
 进程的回收再利用,被LOS_DoExecveFile调用 更多...
 
LITE_OS_SEC_TEXT UINT32 OsExecStart (const TSK_ENTRY_FUNC entry, UINTPTR sp, UINTPTR mapBase, UINT32 mapSize)
 执行用户态任务, entry为入口函数 ,其中 创建好task,task上下文 等待调度真正执行, sp:栈指针 mapBase:栈底 mapSize:栈大小 更多...
 
STATIC UINT32 OsUserInitProcessStart (LosProcessCB *processCB, TSK_INIT_PARAM_S *param)
 用户进程开始初始化 更多...
 
STATIC UINT32 OsLoadUserInit (LosProcessCB *processCB)
 
LITE_OS_SEC_TEXT_INIT UINT32 OsUserInitProcess (VOID)
 
STATIC UINT32 OsCopyUser (LosProcessCB *childCB, LosProcessCB *parentCB)
 拷贝用户信息 直接用memcpy_s 更多...
 
STATIC UINT32 OsCopyTask (UINT32 flags, LosProcessCB *childProcessCB, const CHAR *name, UINTPTR entry, UINT32 size)
 
STATIC UINT32 OsCopyParent (UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
 
STATIC UINT32 OsCopyMM (UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
 
STATIC UINT32 OsCopyFile (UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
 拷贝进程文件描述符(proc_fd)信息 更多...
 
STATIC UINT32 OsForkInitPCB (UINT32 flags, LosProcessCB *child, const CHAR *name, UINTPTR sp, UINT32 size)
 
STATIC UINT32 OsChildSetProcessGroupAndSched (LosProcessCB *child, LosProcessCB *run)
 
STATIC UINT32 OsCopyProcessResources (UINT32 flags, LosProcessCB *child, LosProcessCB *run)
 拷贝进程资源 更多...
 
STATIC INT32 OsCopyProcess (UINT32 flags, const CHAR *name, UINTPTR sp, UINT32 size)
 拷贝进程 更多...
 
LITE_OS_SEC_TEXT INT32 OsClone (UINT32 flags, UINTPTR sp, UINT32 size)
 OsClone 进程克隆 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_Fork (UINT32 flags, const CHAR *name, const TSK_ENTRY_FUNC entry, UINT32 stackSize)
 
LITE_OS_SEC_TEXT VOID LOS_Exit (INT32 status)
 LOS_Exit 进程退出 更多...
 
LITE_OS_SEC_TEXT INT32 LOS_GetUsedPIDList (UINT32 *pidList, INT32 pidMaxNum)
 LOS_GetUsedPIDList
获取使用中的进程列表 更多...
 
LITE_OS_SEC_TEXT struct fd_table_sLOS_GetFdTable (UINT32 pid)
 
LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID (VOID)
 获取当前进程的进程ID 更多...
 
STATIC VOID ThreadGroupActiveTaskKilled (LosTaskCB *taskCB)
 按指定状态退出指定进程 更多...
 
LITE_OS_SEC_TEXT VOID OsProcessThreadGroupDestroy (VOID)
 
LITE_OS_SEC_TEXT UINT32 LOS_GetSystemProcessMaximum (VOID)
 获取系统支持的最大进程数目 更多...
 
LITE_OS_SEC_TEXT UINT32 OsGetUserInitProcessID (VOID)
 获取用户态进程的根进程,所有用户进程都是g_processCBArray[g_userInitProcess] fork来的 更多...
 
LITE_OS_SEC_TEXT UINT32 OsGetKernelInitProcessID (VOID)
 获取内核态根进程 更多...
 
LITE_OS_SEC_TEXT UINT32 OsGetIdleProcessID (VOID)
 获取内核态空闲进程 更多...
 
LITE_OS_SEC_TEXT VOID OsSetSigHandler (UINTPTR addr)
 设置进程的信号处理函数 更多...
 
LITE_OS_SEC_TEXT UINTPTR OsGetSigHandler (VOID)
 获取进程的信号处理函数 更多...
 

变量

LITE_OS_SEC_BSS LosProcessCBg_processCBArray = NULL
 进程池数组 更多...
 
LITE_OS_SEC_DATA_INIT STATIC LOS_DL_LIST g_freeProcess
 空闲状态下的进程链表, .个人觉得应该取名为 g_freeProcessList @note_thinking 更多...
 
LITE_OS_SEC_DATA_INIT STATIC LOS_DL_LIST g_processRecycleList
 需要回收的进程列表 更多...
 
LITE_OS_SEC_BSS UINT32 g_userInitProcess = OS_INVALID_VALUE
 1号进程 用户态的初始init进程,用户态下其他进程由它 fork 更多...
 
LITE_OS_SEC_BSS UINT32 g_kernelInitProcess = OS_INVALID_VALUE
 2号进程 内核态初始Kprocess进程,内核态下其他进程由它 fork 更多...
 
LITE_OS_SEC_BSS UINT32 g_kernelIdleProcess = OS_INVALID_VALUE
 0号进程 内核态idle进程,由Kprocess fork 更多...
 
LITE_OS_SEC_BSS UINT32 g_processMaxNum
 进程最大数量,默认64个 更多...
 
LITE_OS_SEC_BSS ProcessGroupg_processGroup = NULL
 全局进程组,负责管理所有进程组 更多...
 

详细描述

进程模块主文件

并发(Concurrent):多个线程在单个核心运行,同一时间只能一个线程运行,内核不停切换线程,
      看起来像同时运行,实际上是线程不停切换
并行(Parallel)每个线程分配给独立的CPU核心,线程同时运行
单核CPU多个进程或多个线程内能实现并发(微观上的串行,宏观上的并行)
多核CPU线程间可以实现宏观和微观上的并行
LITE_OS_SEC_BSS 和 LITE_OS_SEC_DATA_INIT 是告诉编译器这些全局变量放在哪个数据段
版本
作者
weharmonyos.com | 鸿蒙研究站 | 每天死磕一点点
日期
2021-12-15

在文件 los_process.c 中定义.

函数说明

◆ LOS_CheckInGroups()

LITE_OS_SEC_TEXT BOOL LOS_CheckInGroups ( UINT32  gid)

检查参数群组ID是否在当前用户所属群组中

在文件 los_process.c805 行定义.

806{
807 UINT32 intSave;
808 UINT32 count;
809 User *user = NULL;
810
811 SCHEDULER_LOCK(intSave);
812 user = OsCurrUserGet();//当前进程所属用户
813 for (count = 0; count < user->groupNumber; count++) {//循环对比
814 if (user->groups[count] == gid) {
815 SCHEDULER_UNLOCK(intSave);
816 return TRUE;
817 }
818 }
819
820 SCHEDULER_UNLOCK(intSave);
821 return FALSE;
822}
STATIC INLINE User * OsCurrUserGet(VOID)
unsigned int UINT32
Definition: los_typedef.h:57
UINT32 groupNumber
用户组数量
UINT32 groups[1]
函数调用图:
这是这个函数的调用关系图:

◆ LOS_Exit()

LITE_OS_SEC_TEXT VOID LOS_Exit ( INT32  status)

LOS_Exit 进程退出

参数
status
返回
参见

在文件 los_process.c2086 行定义.

2087{
2088 UINT32 intSave;
2089
2090 (void)status;
2091 /* The exit of a kernel - state process must be kernel - state and all threads must actively exit */
2092 LosProcessCB *processCB = OsCurrProcessGet();
2093 SCHEDULER_LOCK(intSave);
2094 if (!OsProcessIsUserMode(processCB) && (processCB->threadNumber != 1)) {//内核态下进程的退出方式,必须是所有的任务都退出了
2095 SCHEDULER_UNLOCK(intSave);
2096 PRINT_ERR("Kernel-state processes with multiple threads are not allowed to exit directly\n");
2097 return;
2098 }
2099 SCHEDULER_UNLOCK(intSave);
2100
2102 OsRunningTaskToExit(OsCurrTaskGet(), OS_PRO_EXIT_OK);
2103}
LITE_OS_SEC_TEXT VOID OsProcessThreadGroupDestroy(VOID)
Definition: los_process.c:2200
STATIC INLINE BOOL OsProcessIsUserMode(const LosProcessCB *processCB)
STATIC INLINE LosProcessCB * OsCurrProcessGet(VOID)
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
LITE_OS_SEC_TEXT VOID OsRunningTaskToExit(LosTaskCB *runTask, UINT32 status)
Definition: los_task.c:908
volatile UINT32 threadNumber
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void
函数调用图:
这是这个函数的调用关系图:

◆ LOS_Fork()

LITE_OS_SEC_TEXT INT32 LOS_Fork ( UINT32  flags,
const CHAR name,
const TSK_ENTRY_FUNC  entry,
UINT32  stackSize 
)

在文件 los_process.c2060 行定义.

2061{
2062 UINT32 cloneFlag = CLONE_PARENT | CLONE_THREAD | CLONE_VFORK | CLONE_FILES;
2063
2064 if (flags & (~cloneFlag)) {
2065 PRINT_WARN("Clone dont support some flags!\n");
2066 }
2067
2068 flags |= CLONE_FILES;
2069 return OsCopyProcess(cloneFlag & flags, name, (UINTPTR)entry, stackSize);//拷贝一个进程
2070}
macro EXC_SP_SET stackSize
Definition: asm.h:50
STATIC INT32 OsCopyProcess(UINT32 flags, const CHAR *name, UINTPTR sp, UINT32 size)
拷贝进程
Definition: los_process.c:1999
unsigned long UINTPTR
Definition: los_typedef.h:68
函数调用图:

◆ LOS_GetCurrProcessGroupID()

LITE_OS_SEC_TEXT INT32 LOS_GetCurrProcessGroupID ( VOID  )

获取当前进程的组ID

在文件 los_process.c1485 行定义.

1486{
1487 return LOS_GetProcessGroupID(OsCurrProcessGet()->processID);
1488}
LITE_OS_SEC_TEXT INT32 LOS_GetProcessGroupID(UINT32 pid)
Definition: los_process.c:1461
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetCurrProcessID()

LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID ( VOID  )

获取当前进程的进程ID

在文件 los_process.c2161 行定义.

2162{
2163 return OsCurrProcessGet()->processID;
2164}
UINT32 processID
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetFdTable()

LITE_OS_SEC_TEXT struct fd_table_s * LOS_GetFdTable ( UINT32  pid)

在文件 los_process.c2143 行定义.

2144{
2145 LosProcessCB *pcb = NULL;
2146 struct files_struct *files = NULL;
2147
2148 if (OS_PID_CHECK_INVALID(pid)) {
2149 return NULL;
2150 }
2151 pcb = OS_PCB_FROM_PID(pid);
2152 files = pcb->files;
2153 if (files == NULL) {
2154 return NULL;
2155 }
2156
2157 return files->fdt;
2158}
struct files_struct * files
struct fd_table_s * fdt
持有的文件表
Definition: fd_table.h:95
这是这个函数的调用关系图:

◆ LOS_GetGroupID()

LITE_OS_SEC_TEXT INT32 LOS_GetGroupID ( VOID  )

获取当前进程的用户组ID

在文件 los_process.c842 行定义.

843{
844#ifdef LOSCFG_SECURITY_CAPABILITY
845 UINT32 intSave;
846 INT32 gid;
847
848 SCHEDULER_LOCK(intSave);
849 gid = (INT32)OsCurrUserGet()->gid;
850 SCHEDULER_UNLOCK(intSave);
851
852 return gid;
853#else
854 return 0;
855#endif
856}
signed int INT32
Definition: los_typedef.h:60
UINT32 gid
用户组ID [0,60000],0为root用户组
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetProcessGroupID()

LITE_OS_SEC_TEXT INT32 LOS_GetProcessGroupID ( UINT32  pid)

在文件 los_process.c1461 行定义.

1462{
1463 INT32 gid;
1464 UINT32 intSave;
1465 LosProcessCB *processCB = NULL;
1466
1467 if (OS_PID_CHECK_INVALID(pid)) {
1468 return -LOS_EINVAL;
1469 }
1470
1471 SCHEDULER_LOCK(intSave);
1472 processCB = OS_PCB_FROM_PID(pid);
1473 if (OsProcessIsUnused(processCB)) {
1474 gid = -LOS_ESRCH;
1475 goto EXIT;
1476 }
1477
1478 gid = (INT32)processCB->group->groupID;
1479
1480EXIT:
1481 SCHEDULER_UNLOCK(intSave);
1482 return gid;
1483}
STATIC INLINE BOOL OsProcessIsUnused(const LosProcessCB *processCB)
ProcessGroup * group
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetProcessPriority()

LITE_OS_SEC_TEXT INT32 LOS_GetProcessPriority ( INT32  pid)

接口封装 - 获取指定进程优先级

在文件 los_process.c1083 行定义.

1084{
1085 return OsGetProcessPriority(LOS_PRIO_PROCESS, pid);
1086}
LITE_OS_SEC_TEXT INT32 OsGetProcessPriority(INT32 which, INT32 pid)
接口封装 - 获取进程优先级 which:标识进程,进程组,用户
Definition: los_process.c:1055
函数调用图:

◆ LOS_GetProcessScheduler()

LITE_OS_SEC_TEXT INT32 LOS_GetProcessScheduler ( INT32  pid)

获得指定进程的调度策略

在文件 los_process.c1030 行定义.

1031{
1032 UINT32 intSave;
1033
1034 if (OS_PID_CHECK_INVALID(pid)) {
1035 return -LOS_EINVAL;
1036 }
1037
1038 SCHEDULER_LOCK(intSave);
1039 LosProcessCB *processCB = OS_PCB_FROM_PID(pid);
1040 if (OsProcessIsUnused(processCB)) {
1041 SCHEDULER_UNLOCK(intSave);
1042 return -LOS_ESRCH;
1043 }
1044
1045 SCHEDULER_UNLOCK(intSave);
1046
1047 return LOS_SCHED_RR;
1048}
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetSystemProcessMaximum()

LITE_OS_SEC_TEXT UINT32 LOS_GetSystemProcessMaximum ( VOID  )

获取系统支持的最大进程数目

在文件 los_process.c2239 行定义.

2240{
2241 return g_processMaxNum;
2242}
LITE_OS_SEC_BSS UINT32 g_processMaxNum
进程最大数量,默认64个
Definition: los_process.c:86
这是这个函数的调用关系图:

◆ LOS_GetUsedPIDList()

LITE_OS_SEC_TEXT INT32 LOS_GetUsedPIDList ( UINT32 pidList,
INT32  pidMaxNum 
)

LOS_GetUsedPIDList
获取使用中的进程列表

参数
pidList
pidMaxNum
返回
参见

在文件 los_process.c2115 行定义.

2116{
2117 LosProcessCB *pcb = NULL;
2118 INT32 num = 0;
2119 UINT32 intSave;
2120 UINT32 pid = 1;
2121
2122 if (pidList == NULL) {
2123 return 0;
2124 }
2125 SCHEDULER_LOCK(intSave);
2126 while (OsProcessIDUserCheckInvalid(pid) == false) {//遍历进程池
2127 pcb = OS_PCB_FROM_PID(pid);
2128 pid++;
2129 if (OsProcessIsUnused(pcb)) {//未使用的不算
2130 continue;
2131 }
2132 pidList[num] = pcb->processID;//由参数带走
2133 num++;
2134 if (num >= pidMaxNum) {
2135 break;
2136 }
2137 }
2138 SCHEDULER_UNLOCK(intSave);
2139 return num;
2140}
STATIC INLINE BOOL OsProcessIDUserCheckInvalid(UINT32 pid)
函数调用图:
这是这个函数的调用关系图:

◆ LOS_GetUserID()

LITE_OS_SEC_TEXT INT32 LOS_GetUserID ( VOID  )

获取当前进程的用户ID

在文件 los_process.c826 行定义.

827{
828#ifdef LOSCFG_SECURITY_CAPABILITY
829 UINT32 intSave;
830 INT32 uid;
831
832 SCHEDULER_LOCK(intSave);
833 uid = (INT32)OsCurrUserGet()->userID;
834 SCHEDULER_UNLOCK(intSave);
835 return uid;
836#else
837 return 0;
838#endif
839}
UINT32 userID
用户ID [0,60000],0为root用户
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SetProcessPriority()

LITE_OS_SEC_TEXT INT32 LOS_SetProcessPriority ( INT32  pid,
UINT16  prio 
)

接口封装 - 设置进程优先级

在文件 los_process.c1050 行定义.

1051{
1052 return OsSetProcessScheduler(LOS_PRIO_PROCESS, pid, prio, LOS_GetProcessScheduler(pid));
1053}
LITE_OS_SEC_TEXT INT32 OsSetProcessScheduler(INT32 which, INT32 pid, UINT16 prio, UINT16 policy)
设置进程调度计划
Definition: los_process.c:983
LITE_OS_SEC_TEXT INT32 LOS_GetProcessScheduler(INT32 pid)
获得指定进程的调度策略
Definition: los_process.c:1030
函数调用图:
这是这个函数的调用关系图:

◆ LOS_SetProcessScheduler()

LITE_OS_SEC_TEXT INT32 LOS_SetProcessScheduler ( INT32  pid,
UINT16  policy,
UINT16  prio 
)

设置指定进程的调度参数,包括优先级和调度策略

在文件 los_process.c1025 行定义.

1026{
1027 return OsSetProcessScheduler(LOS_PRIO_PROCESS, pid, prio, policy);
1028}
函数调用图:

◆ LOS_Wait()

LITE_OS_SEC_TEXT INT32 LOS_Wait ( INT32  pid,
USER INT32 status,
UINT32  options,
VOID *  rusage 
)

在文件 los_process.c1313 行定义.

1314{
1315 (VOID)rusage;
1316 UINT32 ret;
1317
1318 ret = OsWaitOptionsCheck(options);
1319 if (ret != LOS_OK) {
1320 return -ret;
1321 }
1322
1323 return OsWait(pid, status, NULL, options, NULL);
1324}
STATIC UINT32 OsWaitOptionsCheck(UINT32 options)
Definition: los_process.c:1245
STATIC INT32 OsWait(INT32 pid, USER INT32 *status, USER siginfo_t *info, UINT32 options, VOID *rusage)
等待子进程结束并回收子进程,返回已经终止的子进程的进程ID号,并清除僵死进程。
Definition: los_process.c:1265
函数调用图:
这是这个函数的调用关系图:

◆ LOS_Waitid()

LITE_OS_SEC_TEXT INT32 LOS_Waitid ( INT32  pid,
USER siginfo_t *  info,
UINT32  options,
VOID *  rusage 
)

在文件 los_process.c1350 行定义.

1351{
1352 (VOID)rusage;
1353 UINT32 ret;
1354
1355 /* check options value */
1356 ret = OsWaitidOptionsCheck(options);
1357 if (ret != LOS_OK) {
1358 return -ret;
1359 }
1360
1361 return OsWait(pid, NULL, info, options, NULL);
1362}
STATIC UINT32 OsWaitidOptionsCheck(UINT32 options)
Definition: los_process.c:1326
函数调用图:
这是这个函数的调用关系图:

◆ OsChildProcessResourcesFree()

STATIC VOID OsChildProcessResourcesFree ( const LosProcessCB processCB)

回收指定进程的已经退出(死亡)的孩子进程所占资源

在文件 los_process.c537 行定义.

538{
539 LosProcessCB *childCB = NULL;
540 ProcessGroup *group = NULL;
541
542 while (!LOS_ListEmpty(&((LosProcessCB *)processCB)->exitChildList)) {//遍历直到没有了退出(死亡)的孩子进程
543 childCB = LOS_DL_LIST_ENTRY(processCB->exitChildList.pstNext, LosProcessCB, siblingList);//获取孩子进程,
544 OsRecycleZombiesProcess(childCB, &group);//其中会将childCB从exitChildList链表上摘出去
545 (VOID)LOS_MemFree(m_aucSysMem1, group);//
546 }
547}
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
UINT8 * m_aucSysMem1
系统动态内存池地址的起始地址 @note_thinking 能否不要用 0,1来命名核心变量 ???
Definition: los_memory.c:108
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
释放从指定动态内存中申请的内存
Definition: los_memory.c:1369
LITE_OS_SEC_TEXT STATIC VOID OsRecycleZombiesProcess(LosProcessCB *childCB, ProcessGroup **group)
Definition: los_process.c:482
struct LOS_DL_LIST * pstNext
Definition: los_list.h:84
LOS_DL_LIST exitChildList
函数调用图:
这是这个函数的调用关系图:

◆ OsChildSetProcessGroupAndSched()

STATIC UINT32 OsChildSetProcessGroupAndSched ( LosProcessCB child,
LosProcessCB run 
)

在文件 los_process.c1945 行定义.

1946{
1947 UINT32 intSave;
1948 UINT32 ret;
1949 ProcessGroup *group = NULL;
1950
1951 LosTaskCB *taskCB = OS_TCB_FROM_TID(child->threadGroupID);
1952 SCHEDULER_LOCK(intSave);
1953 if (run->group->groupID == OS_USER_PRIVILEGE_PROCESS_GROUP) {//如果是有用户特权进程组
1954 ret = OsSetProcessGroupIDUnsafe(child->processID, child->processID, &group);//设置组ID,存在不安全的风险
1955 if (ret != LOS_OK) {
1956 SCHEDULER_UNLOCK(intSave);
1957 return LOS_ENOMEM;
1958 }
1959 }
1960
1961 child->processStatus &= ~OS_PROCESS_STATUS_INIT;
1962 taskCB->ops->enqueue(OsSchedRunqueue(), taskCB);
1963 SCHEDULER_UNLOCK(intSave);
1964
1965 (VOID)LOS_MemFree(m_aucSysMem1, group);
1966 return LOS_OK;
1967}
STATIC UINT32 OsSetProcessGroupIDUnsafe(UINT32 pid, UINT32 gid, ProcessGroup **group)
Definition: los_process.c:1400
STATIC INLINE SchedRunqueue * OsSchedRunqueue(VOID)
UINT32 threadGroupID
UINT16 processStatus
VOID(* enqueue)(SchedRunqueue *rq, LosTaskCB *taskCB)
入队列
const SchedOps * ops
函数调用图:
这是这个函数的调用关系图:

◆ OsClone()

LITE_OS_SEC_TEXT INT32 OsClone ( UINT32  flags,
UINTPTR  sp,
UINT32  size 
)

OsClone 进程克隆

参数
flags
size进程主任务内核栈大小
sp进程主任务的入口函数
返回
参见

在文件 los_process.c2049 行定义.

2050{
2051 UINT32 cloneFlag = CLONE_PARENT | CLONE_THREAD | CLONE_VFORK | CLONE_VM;
2052
2053 if (flags & (~cloneFlag)) {
2054 PRINT_WARN("Clone dont support some flags!\n");
2055 }
2056
2057 return OsCopyProcess(cloneFlag & flags, NULL, sp, size);
2058}
macro EXC_SP_SET reg1 mrc 获取CPU信息 and mov mul reg0 计算当前CPU栈的偏移位置 ldr reg1 相减得到栈顶 mov sp
Definition: asm.h:57
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyFile()

STATIC UINT32 OsCopyFile ( UINT32  flags,
LosProcessCB childProcessCB,
LosProcessCB runProcessCB 
)

拷贝进程文件描述符(proc_fd)信息

在文件 los_process.c1909 行定义.

1910{
1911#ifdef LOSCFG_FS_VFS
1912 if (flags & CLONE_FILES) {
1913 childProcessCB->files = runProcessCB->files;
1914 } else {
1915 childProcessCB->files = dup_fd(runProcessCB->files);
1916 }
1917 if (childProcessCB->files == NULL) {
1918 return LOS_ENOMEM;
1919 }
1920#endif
1921
1922 childProcessCB->consoleID = runProcessCB->consoleID;//控制台也是文件
1923 childProcessCB->umask = runProcessCB->umask;
1924 return LOS_OK;
1925}
struct files_struct * dup_fd(struct files_struct *oldf)
复制FD
UINT16 consoleID
mode_t umask
umask(user file-creatiopn mode mask)为用户文件创建掩码,是创建文件或文件夹时默认权限的基础。
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyMM()

STATIC UINT32 OsCopyMM ( UINT32  flags,
LosProcessCB childProcessCB,
LosProcessCB runProcessCB 
)

在文件 los_process.c1885 行定义.

1886{
1887 status_t status;
1888 UINT32 intSave;
1889
1890 if (!OsProcessIsUserMode(childProcessCB)) {//不是用户模式,直接返回,什么意思?内核虚拟空间只有一个,无需COPY !!!
1891 return LOS_OK;
1892 }
1893
1894 if (flags & CLONE_VM) {//贴有虚拟内存的标签
1895 SCHEDULER_LOCK(intSave);
1896 childProcessCB->vmSpace->archMmu.virtTtb = runProcessCB->vmSpace->archMmu.virtTtb;//TTB虚拟地址基地址,即L1表存放位置,virtTtb是个指针,进程的虚拟空间是指定的范围的
1897 childProcessCB->vmSpace->archMmu.physTtb = runProcessCB->vmSpace->archMmu.physTtb;//TTB物理地址基地址,physTtb是个值,取决于运行时映射到物理内存的具体哪个位置.
1898 SCHEDULER_UNLOCK(intSave);
1899 return LOS_OK;
1900 }
1901
1902 status = LOS_VmSpaceClone(runProcessCB->vmSpace, childProcessCB->vmSpace);//虚拟空间clone
1903 if (status != LOS_OK) {
1904 return LOS_ENOMEM;
1905 }
1906 return LOS_OK;
1907}
int status_t
Definition: los_typedef.h:205
STATUS_T LOS_VmSpaceClone(LosVmSpace *oldVmSpace, LosVmSpace *newVmSpace)
Definition: los_vm_map.c:318
VADDR_T * virtTtb
Definition: los_arch_mmu.h:90
PADDR_T physTtb
Definition: los_arch_mmu.h:91
LosVmSpace * vmSpace
LosArchMmu archMmu
Definition: los_vm_map.h:157
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyParent()

STATIC UINT32 OsCopyParent ( UINT32  flags,
LosProcessCB childProcessCB,
LosProcessCB runProcessCB 
)

在文件 los_process.c1861 行定义.

1862{
1863 UINT32 ret;
1864 UINT32 intSave;
1865 LosProcessCB *parentProcessCB = NULL;
1866
1867 SCHEDULER_LOCK(intSave);
1868
1869 if (flags & CLONE_PARENT) { //这里指明 childProcessCB 和 runProcessCB 有同一个父亲,是兄弟关系
1870 parentProcessCB = OS_PCB_FROM_PID(runProcessCB->parentProcessID);//找出当前进程的父亲大人
1871 } else {
1872 parentProcessCB = runProcessCB;
1873 }
1874 childProcessCB->parentProcessID = parentProcessCB->processID;//指认父亲,这个赋值代表从此是你儿了
1875 LOS_ListTailInsert(&parentProcessCB->childrenList, &childProcessCB->siblingList);//通过我的兄弟姐妹节点,挂到父亲的孩子链表上,于我而言,父亲的这个链表上挂的都是我的兄弟姐妹
1876 //不会被排序,老大,老二,老三 老天爷指定了。
1877 childProcessCB->group = parentProcessCB->group;//跟父亲大人在同一个进程组,注意父亲可能是组长,但更可能不是组长,
1878 LOS_ListTailInsert(&parentProcessCB->group->processList, &childProcessCB->subordinateGroupList);//自己去组里登记下,这个要自己登记,跟父亲没啥关系。
1879 ret = OsCopyUser(childProcessCB, parentProcessCB);
1880
1881 SCHEDULER_UNLOCK(intSave);
1882 return ret;
1883}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
Definition: los_list.h:244
STATIC UINT32 OsCopyUser(LosProcessCB *childCB, LosProcessCB *parentCB)
拷贝用户信息 直接用memcpy_s
Definition: los_process.c:1788
LOS_DL_LIST siblingList
LOS_DL_LIST childrenList
LOS_DL_LIST subordinateGroupList
UINT32 parentProcessID
LOS_DL_LIST processList
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyProcess()

STATIC INT32 OsCopyProcess ( UINT32  flags,
const CHAR name,
UINTPTR  sp,
UINT32  size 
)

拷贝进程

在文件 los_process.c1999 行定义.

2000{
2001 UINT32 ret, processID;
2002 LosProcessCB *run = OsCurrProcessGet();//获取当前进程
2003
2004 LosProcessCB *child = OsGetFreePCB();//从进程池中申请一个进程控制块,鸿蒙进程池默认64
2005 if (child == NULL) {
2006 return -LOS_EAGAIN;
2007 }
2008 processID = child->processID;
2009
2010 ret = OsForkInitPCB(flags, child, name, sp, size);//初始化进程控制块
2011 if (ret != LOS_OK) {
2012 goto ERROR_INIT;
2013 }
2014
2015 ret = OsCopyProcessResources(flags, child, run);//拷贝进程的资源,包括虚拟空间,文件,安全,IPC ==
2016 if (ret != LOS_OK) {
2017 goto ERROR_TASK;
2018 }
2019
2020 ret = OsChildSetProcessGroupAndSched(child, run);//设置进程组和加入进程调度就绪队列
2021 if (ret != LOS_OK) {
2022 goto ERROR_TASK;
2023 }
2024
2025 LOS_MpSchedule(OS_MP_CPU_ALL);//给各CPU发送准备接受调度信号
2026 if (OS_SCHEDULER_ACTIVE) {//当前CPU core处于活动状态
2027 LOS_Schedule();// 申请调度
2028 }
2029
2030 return processID;
2031
2032ERROR_TASK:
2033 (VOID)LOS_TaskDelete(child->threadGroupID);
2034ERROR_INIT:
2035 OsDeInitPCB(child);
2036 return -ret;
2037}
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskDelete(UINT32 taskID)
Delete a task.
Definition: los_task.c:968
VOID LOS_Schedule(VOID)
Trigger active task scheduling.
Definition: los_sched.c:469
VOID LOS_MpSchedule(UINT32 target)
Definition: los_mp.c:76
STATIC UINT32 OsCopyProcessResources(UINT32 flags, LosProcessCB *child, LosProcessCB *run)
拷贝进程资源
Definition: los_process.c:1969
STATIC UINT32 OsChildSetProcessGroupAndSched(LosProcessCB *child, LosProcessCB *run)
Definition: los_process.c:1945
STATIC UINT32 OsForkInitPCB(UINT32 flags, LosProcessCB *child, const CHAR *name, UINTPTR sp, UINT32 size)
Definition: los_process.c:1927
STATIC LosProcessCB * OsGetFreePCB(VOID)
为用户态任务分配栈空间
Definition: los_process.c:1491
STATIC VOID OsDeInitPCB(LosProcessCB *processCB)
Definition: los_process.c:672
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyProcessResources()

STATIC UINT32 OsCopyProcessResources ( UINT32  flags,
LosProcessCB child,
LosProcessCB run 
)

拷贝进程资源

在文件 los_process.c1969 行定义.

1970{
1971 UINT32 ret;
1972
1973 ret = OsCopyMM(flags, child, run);//拷贝虚拟空间
1974 if (ret != LOS_OK) {
1975 return ret;
1976 }
1977
1978 ret = OsCopyFile(flags, child, run);//拷贝文件信息
1979 if (ret != LOS_OK) {
1980 return ret;
1981 }
1982
1983#ifdef LOSCFG_KERNEL_LITEIPC
1984 if (run->ipcInfo != NULL) { //重新初始化IPC池
1985 child->ipcInfo = LiteIpcPoolReInit((const ProcIpcInfo *)(run->ipcInfo));//@note_good 将沿用用户态空间地址(即线性区地址)
1986 if (child->ipcInfo == NULL) {//因为整个进程虚拟空间都是拷贝的,ipc的用户态虚拟地址当然可以拷贝,但因进程不同了,所以需要重新申请ipc池和重新
1987 return LOS_ENOMEM;//映射池中两个地址.
1988 }
1989 }
1990#endif
1991
1992#ifdef LOSCFG_SECURITY_CAPABILITY
1993 OsCopyCapability(run, child);//拷贝安全能力
1994#endif
1995
1996 return LOS_OK;
1997}
VOID OsCopyCapability(LosProcessCB *from, LosProcessCB *to)
进程间安全能力的拷贝
Definition: capability.c:59
LITE_OS_SEC_TEXT ProcIpcInfo * LiteIpcPoolReInit(const ProcIpcInfo *parent)
LiteIpcPoolReInit 重新初始化进程的IPC消息内存池
Definition: hm_liteipc.c:346
STATIC UINT32 OsCopyFile(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
拷贝进程文件描述符(proc_fd)信息
Definition: los_process.c:1909
STATIC UINT32 OsCopyMM(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
Definition: los_process.c:1885
进程IPC信息,见于进程结构体: LosProcessCB.ipcInfo
Definition: hm_liteipc.h:92
ProcIpcInfo * ipcInfo
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyTask()

STATIC UINT32 OsCopyTask ( UINT32  flags,
LosProcessCB childProcessCB,
const CHAR name,
UINTPTR  entry,
UINT32  size 
)

在文件 los_process.c1803 行定义.

1804{
1805 LosTaskCB *runTask = OsCurrTaskGet();
1806 TSK_INIT_PARAM_S taskParam = { 0 };
1807 UINT32 ret, taskID, intSave;
1808 SchedParam param = { 0 };
1809
1810 SCHEDULER_LOCK(intSave);
1811 if (OsProcessIsUserMode(childProcessCB)) {//用户态进程
1812 taskParam.pfnTaskEntry = runTask->taskEntry;//拷贝当前任务入口地址
1813 taskParam.uwStackSize = runTask->stackSize; //栈空间大小
1814 taskParam.userParam.userArea = runTask->userArea; //用户态栈区栈顶位置
1815 taskParam.userParam.userMapBase = runTask->userMapBase; //用户态栈底
1816 taskParam.userParam.userMapSize = runTask->userMapSize; //用户态栈大小
1817 } else {//注意内核态进程创建任务的入口由外界指定,例如 OsCreateIdleProcess 指定了OsIdleTask
1818 taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)entry;//参数(sp)为内核态入口地址
1819 taskParam.uwStackSize = size;//参数(size)为内核态栈大小
1820 }
1821 if (runTask->taskStatus & OS_TASK_FLAG_PTHREAD_JOIN) {
1822 taskParam.uwResved = LOS_TASK_ATTR_JOINABLE;
1823 }
1824
1825 runTask->ops->schedParamGet(runTask, &param);
1826 SCHEDULER_UNLOCK(intSave);
1827
1828 taskParam.pcName = (CHAR *)name;
1829 taskParam.policy = param.policy;
1830 taskParam.usTaskPrio = param.priority;
1831 taskParam.processID = childProcessCB->processID;
1832
1833 ret = LOS_TaskCreateOnly(&taskID, &taskParam);
1834 if (ret != LOS_OK) {
1835 if (ret == LOS_ERRNO_TSK_TCB_UNAVAILABLE) {
1836 return LOS_EAGAIN;
1837 }
1838 return LOS_ENOMEM;
1839 }
1840
1841 LosTaskCB *childTaskCB = OS_TCB_FROM_TID(taskID);
1842 childTaskCB->taskStatus = runTask->taskStatus;//任务状态先同步,注意这里是赋值操作. ...01101001
1843 childTaskCB->ops->schedParamModify(childTaskCB, &param);
1844 if (childTaskCB->taskStatus & OS_TASK_STATUS_RUNNING) {//因只能有一个运行的task,所以如果一样要改4号位
1845 childTaskCB->taskStatus &= ~OS_TASK_STATUS_RUNNING;//将四号位清0 ,变成 ...01100001
1846 } else {//非运行状态下会发生什么?
1847 if (OS_SCHEDULER_ACTIVE) {//克隆线程发生错误未运行
1848 LOS_Panic("Clone thread status not running error status: 0x%x\n", childTaskCB->taskStatus);
1849 }
1850 childTaskCB->taskStatus &= ~OS_TASK_STATUS_UNUSED;//干净的Task
1851 }
1852
1853 if (OsProcessIsUserMode(childProcessCB)) {//是否是用户进程
1854 SCHEDULER_LOCK(intSave);
1855 OsUserCloneParentStack(childTaskCB->stackPointer, runTask->topOfStack, runTask->stackSize);//拷贝当前任务上下文给新的任务
1856 SCHEDULER_UNLOCK(intSave);
1857 }
1858 return LOS_OK;
1859}
NORETURN VOID LOS_Panic(const CHAR *fmt,...)
Kernel panic function.
LITE_OS_SEC_TEXT_INIT UINT32 LOS_TaskCreateOnly(UINT32 *taskID, TSK_INIT_PARAM_S *initParam)
LOS_TaskCreateOnly 创建任务,并使该任务进入suspend状态,不对该任务进行调度。如果需要调度,可以调用LOS_TaskResume使该任务进入ready状态
Definition: los_task.c:663
VOID *(* TSK_ENTRY_FUNC)(UINTPTR param1, UINTPTR param2, UINTPTR param3, UINTPTR param4)
Define the type of a task entrance function.
Definition: los_task.h:480
VOID OsUserCloneParentStack(VOID *childStack, UINTPTR parentTopOfStask, UINT32 parentStackSize)
把父任务上下文克隆给子任务
Definition: los_hw.c:107
char CHAR
Definition: los_typedef.h:63
BOOL(* schedParamModify)(LosTaskCB *taskCB, const SchedParam *param)
修改调度参数
UINT32(* schedParamGet)(const LosTaskCB *taskCB, SchedParam *param)
获取调度参数
UINT16 policy
UINT16 priority
TSK_ENTRY_FUNC taskEntry
UINT32 stackSize
UINTPTR userMapBase
用户空间的栈顶位置,内存来自用户空间,和topOfStack有本质的区别.
UINTPTR topOfStack
VOID * stackPointer
UINT32 userMapSize
UINTPTR userArea
用户空间的堆区开始位置
UINT16 taskStatus
UINT32 userMapSize
用户空间的栈大小,栈底 = userMapBase + userMapSize
Definition: los_task.h:494
UINTPTR userArea
用户空间的堆区开始位置
Definition: los_task.h:491
UINTPTR userMapBase
用户空间的栈顶位置.
Definition: los_task.h:493
UINT16 policy
Definition: los_task.h:506
UINT16 usTaskPrio
Definition: los_task.h:505
UINT32 uwStackSize
Definition: los_task.h:508
CHAR * pcName
Definition: los_task.h:509
UINT32 processID
进程ID
Definition: los_task.h:516
TSK_ENTRY_FUNC pfnTaskEntry
Definition: los_task.h:504
UserTaskParam userParam
任务用户态运行时任何参数
Definition: los_task.h:517
UINT32 uwResved
Definition: los_task.h:513
函数调用图:
这是这个函数的调用关系图:

◆ OsCopyUser()

STATIC UINT32 OsCopyUser ( LosProcessCB childCB,
LosProcessCB parentCB 
)

拷贝用户信息 直接用memcpy_s

在文件 los_process.c1788 行定义.

1789{
1790#ifdef LOSCFG_SECURITY_CAPABILITY
1791 UINT32 size = sizeof(User) + sizeof(UINT32) * (parentCB->user->groupNumber - 1);
1792 childCB->user = LOS_MemAlloc(m_aucSysMem1, size);
1793 if (childCB->user == NULL) {
1794 return LOS_ENOMEM;
1795 }
1796
1797 (VOID)memcpy_s(childCB->user, size, parentCB->user, size);
1798#endif
1799 return LOS_OK;
1800}
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
User * user
进程的拥有者
函数调用图:
这是这个函数的调用关系图:

◆ OsCreateProcessGroup()

STATIC ProcessGroup * OsCreateProcessGroup ( UINT32  pid)

创建进程组

参数
argc1
[UINT32]pid 进程ID
返回
函数执行结果

在文件 los_process.c160 行定义.

161{
162 LosProcessCB *processCB = NULL;
163 ProcessGroup *group = LOS_MemAlloc(m_aucSysMem1, sizeof(ProcessGroup));//从内存池中分配进程组结构体
164 if (group == NULL) {
165 return NULL;
166 }
167
168 group->groupID = pid;//参数当进程组ID
169 LOS_ListInit(&group->processList);//初始化进程链表,这里把组内的进程都挂上去
170 LOS_ListInit(&group->exitProcessList);//初始化退出进程链表,这里挂退出的进程
171
172 processCB = OS_PCB_FROM_PID(pid);//通过pid获得进程实体
173 LOS_ListTailInsert(&group->processList, &processCB->subordinateGroupList);//通过subordinateGroupList挂在进程组上,自然后续要通过它来找到进程实体
174 processCB->group = group;//设置进程所属进程组
175 processCB->processStatus |= OS_PROCESS_FLAG_GROUP_LEADER;//进程状态贴上当老大的标签
176 if (g_processGroup != NULL) {//全局进程组链表判空,g_processGroup指向"Kernel"进程所在组,详见: OsKernelInitProcess
177 LOS_ListTailInsert(&g_processGroup->groupList, &group->groupList);//把进程组挂到全局进程组链表上
178 }
179
180 return group;
181}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_BSS ProcessGroup * g_processGroup
全局进程组,负责管理所有进程组
Definition: los_process.c:87
LOS_DL_LIST exitProcessList
LOS_DL_LIST groupList
函数调用图:
这是这个函数的调用关系图:

◆ OsCreateUser()

STATIC User * OsCreateUser ( UINT32  userID,
UINT32  gid,
UINT32  size 
)

在文件 los_process.c788 行定义.

789{ //(size - 1) * sizeof(UINT32) 用于 user->groups[..],这种设计节约了内存,不造成不需要的浪费
790 User *user = LOS_MemAlloc(m_aucSysMem1, sizeof(User) + (size - 1) * sizeof(UINT32));
791 if (user == NULL) {
792 return NULL;
793 }
794
795 user->userID = userID;
796 user->effUserID = userID;
797 user->gid = gid;
798 user->effGid = gid;
799 user->groupNumber = size;//用户组数量
800 user->groups[0] = gid; //用户组列表,一个用户可以属于多个用户组
801 return user;
802}
UINT32 effUserID
UINT32 effGid
函数调用图:
这是这个函数的调用关系图:

◆ OsDealAliveChildProcess()

STATIC VOID OsDealAliveChildProcess ( LosProcessCB processCB)

当一个进程自然退出的时候,它的孩子进程由两位老祖宗收养

在文件 los_process.c503 行定义.

504{
505 UINT32 parentID;
506 LosProcessCB *childCB = NULL;
507 LosProcessCB *parentCB = NULL;
508 LOS_DL_LIST *nextList = NULL;
509 LOS_DL_LIST *childHead = NULL;
510
511 if (!LOS_ListEmpty(&processCB->childrenList)) {//如果存在孩子进程
512 childHead = processCB->childrenList.pstNext;//获取孩子链表
513 LOS_ListDelete(&(processCB->childrenList));//清空自己的孩子链表
514 if (OsProcessIsUserMode(processCB)) {//是用户态进程
515 parentID = g_userInitProcess;//用户态进程老祖宗
516 } else {
517 parentID = g_kernelInitProcess;//内核态进程老祖宗
518 }
519
520 for (nextList = childHead; ;) {//遍历孩子链表
521 childCB = OS_PCB_FROM_SIBLIST(nextList);//找到孩子的真身
522 childCB->parentProcessID = parentID;//孩子磕头认老祖宗为爸爸
523 nextList = nextList->pstNext;//找下一个孩子进程
524 if (nextList == childHead) {//一圈下来,孩子们都磕完头了
525 break;
526 }
527 }
528
529 parentCB = OS_PCB_FROM_PID(parentID);//找个老祖宗的真身
530 LOS_ListTailInsertList(&parentCB->childrenList, childHead);//挂到老祖宗的孩子链表上
531 }
532
533 return;
534}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsertList(LOS_DL_LIST *oldList, LOS_DL_LIST *newList)
Insert a doubly list to the tail of a doubly linked list.
Definition: los_list.h:377
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListDelete(LOS_DL_LIST *node)
Definition: los_list.h:292
LITE_OS_SEC_BSS UINT32 g_kernelInitProcess
2号进程 内核态初始Kprocess进程,内核态下其他进程由它 fork
Definition: los_process.c:84
LITE_OS_SEC_BSS UINT32 g_userInitProcess
1号进程 用户态的初始init进程,用户态下其他进程由它 fork
Definition: los_process.c:83
函数调用图:
这是这个函数的调用关系图:

◆ OsDeInitPCB()

STATIC VOID OsDeInitPCB ( LosProcessCB processCB)

删除PCB块 其实是 PCB块回归进程池,先进入回收链表

在文件 los_process.c672 行定义.

673{
674 UINT32 intSave;
675 ProcessGroup *group = NULL;
676
677 if (processCB == NULL) {
678 return;
679 }
680
681 OsProcessResourcesToFree(processCB);//释放进程所占用的资源
682
683 SCHEDULER_LOCK(intSave);
684 if (processCB->parentProcessID != OS_INVALID_VALUE) {
685 LOS_ListDelete(&processCB->siblingList);//将进程从兄弟链表中摘除
686 processCB->parentProcessID = OS_INVALID_VALUE;
687 }
688
689 if (processCB->group != NULL) {
690 OsExitProcessGroup(processCB, &group);//退出进程组
691 }
692
693 processCB->processStatus &= ~OS_PROCESS_STATUS_INIT;//设置进程状态为非初始化
694 processCB->processStatus |= OS_PROCESS_FLAG_EXIT; //设置进程状态为退出
696 SCHEDULER_UNLOCK(intSave);
697
698 (VOID)LOS_MemFree(m_aucSysMem1, group);//释放内存
699 OsWriteResourceEvent(OS_RESOURCE_EVENT_FREE);
700 return;
701}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListHeadInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the head of a doubly linked list.
Definition: los_list.h:268
STATIC VOID OsExitProcessGroup(LosProcessCB *processCB, ProcessGroup **group)
Definition: los_process.c:184
LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
Definition: los_process.c:431
LITE_OS_SEC_DATA_INIT STATIC LOS_DL_LIST g_processRecycleList
需要回收的进程列表
Definition: los_process.c:82
LITE_OS_SEC_TEXT VOID OsWriteResourceEvent(UINT32 events)
Definition: los_task.c:1642
LOS_DL_LIST pendList
函数调用图:
这是这个函数的调用关系图:

◆ OsDeleteTaskFromProcess()

VOID OsDeleteTaskFromProcess ( LosTaskCB taskCB)

在文件 los_process.c107 行定义.

108{
109 LosProcessCB *processCB = OS_PCB_FROM_PID(taskCB->processID);
110
111 LOS_ListDelete(&taskCB->threadList);
112 processCB->threadNumber--;
114}
VOID OsTaskInsertToRecycleList(LosTaskCB *taskCB)
Definition: los_task.c:198
LOS_DL_LIST threadList
UINT32 processID
函数调用图:
这是这个函数的调用关系图:

◆ OsExecProcessVmSpaceReplace()

LITE_OS_SEC_TEXT LosVmSpace * OsExecProcessVmSpaceReplace ( LosVmSpace newSpace,
UINTPTR  stackBase,
INT32  randomDevFD 
)

在文件 los_process.c1541 行定义.

1542{
1543 LosProcessCB *processCB = OsCurrProcessGet();
1544 LosTaskCB *runTask = OsCurrTaskGet();
1545
1548
1549 LosVmSpace *oldSpace = processCB->vmSpace;
1550 processCB->vmSpace = newSpace;
1551 processCB->vmSpace->heapBase += OsGetRndOffset(randomDevFD);
1552 processCB->vmSpace->heapNow = processCB->vmSpace->heapBase;
1553 processCB->vmSpace->mapBase += OsGetRndOffset(randomDevFD);
1554 processCB->vmSpace->mapSize = stackBase - processCB->vmSpace->mapBase;
1555 runTask->archMmu = (UINTPTR)&processCB->vmSpace->archMmu;
1557 return oldSpace;
1558}
VOID LOS_ArchMmuContextSwitch(LosArchMmu *archMmu)
LOS_ArchMmuContextSwitch 切换MMU上下文
UINT32 OsGetRndOffset(INT32 randomDevFD)
Definition: los_load_elf.c:719
LITE_OS_SEC_TEXT VOID OsTaskCBRecycleToFree()
Definition: los_task.c:472
内存管理单元(英语:memory management unit,缩写为MMU),有时称作分页内存管理单元(英语:paged memory management unit,缩写为PMMU)。
Definition: los_arch_mmu.h:86
UINTPTR archMmu
虚拟空间,每个进程都有一个属于自己的虚拟内存地址空间
Definition: los_vm_map.h:146
VADDR_T mapBase
Definition: los_vm_map.h:155
VADDR_T heapBase
Definition: los_vm_map.h:152
VADDR_T heapNow
Definition: los_vm_map.h:153
UINT32 mapSize
Definition: los_vm_map.h:156
函数调用图:
这是这个函数的调用关系图:

◆ OsExecProcessVmSpaceRestore()

LITE_OS_SEC_TEXT VOID OsExecProcessVmSpaceRestore ( LosVmSpace oldSpace)

在文件 los_process.c1531 行定义.

1532{
1533 LosProcessCB *processCB = OsCurrProcessGet();
1534 LosTaskCB *runTask = OsCurrTaskGet();
1535
1536 processCB->vmSpace = oldSpace;
1537 runTask->archMmu = (UINTPTR)&processCB->vmSpace->archMmu;
1539}
函数调用图:
这是这个函数的调用关系图:

◆ OsExecRecycleAndInit()

LITE_OS_SEC_TEXT UINT32 OsExecRecycleAndInit ( LosProcessCB processCB,
const CHAR name,
LosVmSpace oldSpace,
UINTPTR  oldFiles 
)

进程的回收再利用,被LOS_DoExecveFile调用

参数
processCB
name
oldSpace
oldFiles
返回
LITE_OS_SEC_TEXT

在文件 los_process.c1568 行定义.

1570{
1571 UINT32 ret;
1572 const CHAR *processName = NULL;
1573
1574 if ((processCB == NULL) || (name == NULL)) {
1575 return LOS_NOK;
1576 }
1577
1578 processName = strrchr(name, '/');
1579 processName = (processName == NULL) ? name : (processName + 1); /* 1: Do not include '/' */
1580
1581 ret = (UINT32)OsSetTaskName(OsCurrTaskGet(), processName, TRUE);
1582 if (ret != LOS_OK) {
1583 return ret;
1584 }
1585
1586#ifdef LOSCFG_KERNEL_LITEIPC
1587 (VOID)LiteIpcPoolDestroy(processCB->processID);
1588#endif
1589
1590 processCB->sigHandler = 0;
1592
1593 LOS_VmSpaceFree(oldSpace);
1594#ifdef LOSCFG_FS_VFS
1595 CloseOnExec((struct files_struct *)oldFiles);
1596 delete_files_snapshot((struct files_struct *)oldFiles);
1597#endif
1598
1599 OsSwtmrRecycle(processCB->processID);//回收定时器
1600 processCB->timerID = (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID;
1601
1602#ifdef LOSCFG_SECURITY_VID
1603 VidMapDestroy(processCB);
1604 ret = VidMapListInit(processCB);
1605 if (ret != LOS_OK) {
1606 return LOS_NOK;
1607 }
1608#endif
1609
1610 processCB->processStatus &= ~OS_PROCESS_FLAG_EXIT; //去掉进程退出标签
1611 processCB->processStatus |= OS_PROCESS_FLAG_ALREADY_EXEC;//加上进程运行elf标签
1612
1613 return LOS_OK;
1614}
void delete_files_snapshot(struct files_struct *files)
删除文件管理器快照
void CloseOnExec(struct files_struct *files)
Definition: vfs_cloexec.c:42
LITE_OS_SEC_TEXT UINT32 LiteIpcPoolDestroy(UINT32 processID)
销毁指定进程的IPC池
Definition: hm_liteipc.c:386
LITE_OS_SEC_TEXT_INIT VOID OsSwtmrRecycle(UINT32 processID)
回收指定进程的软时钟
Definition: los_swtmr.c:391
LITE_OS_SEC_TEXT INT32 OsSetTaskName(LosTaskCB *taskCB, const CHAR *name, BOOL setPName)
Definition: los_task.c:1337
STATUS_T LOS_VmSpaceFree(LosVmSpace *space)
timer_t timerID
UINTPTR sigHandler
sig_cb sig
信号控制块,用于异步通信,类似于 linux singal模块
sigset_t sigprocmask
Signals that are blocked | 任务屏蔽了哪些信号
Definition: los_signal.h:223
void VidMapDestroy(LosProcessCB *processCB)
销毁虚拟ID映射
Definition: vid.c:57
UINT32 VidMapListInit(LosProcessCB *processCB)
Definition: vid.c:36
函数调用图:
这是这个函数的调用关系图:

◆ OsExecStart()

LITE_OS_SEC_TEXT UINT32 OsExecStart ( const TSK_ENTRY_FUNC  entry,
UINTPTR  sp,
UINTPTR  mapBase,
UINT32  mapSize 
)

执行用户态任务, entry为入口函数 ,其中 创建好task,task上下文 等待调度真正执行, sp:栈指针 mapBase:栈底 mapSize:栈大小

在文件 los_process.c1616 行定义.

1617{
1618 UINT32 intSave;
1619
1620 if (entry == NULL) {
1621 return LOS_NOK;
1622 }
1623
1624 if ((sp == 0) || (LOS_Align(sp, LOSCFG_STACK_POINT_ALIGN_SIZE) != sp)) {//对齐
1625 return LOS_NOK;
1626 }
1627 //注意 sp此时指向栈底,栈底地址要大于栈顶
1628 if ((mapBase == 0) || (mapSize == 0) || (sp <= mapBase) || (sp > (mapBase + mapSize))) {//参数检查
1629 return LOS_NOK;
1630 }
1631
1632 LosTaskCB *taskCB = OsCurrTaskGet();//获取当前任务
1633 SCHEDULER_LOCK(intSave);//拿自旋锁
1634
1635 taskCB->userMapBase = mapBase;//用户态栈顶位置
1636 taskCB->userMapSize = mapSize;//用户态栈
1637 taskCB->taskEntry = (TSK_ENTRY_FUNC)entry;//任务的入口函数
1638 //初始化内核态栈
1639 TaskContext *taskContext = (TaskContext *)OsTaskStackInit(taskCB->taskID, taskCB->stackSize,
1640 (VOID *)taskCB->topOfStack, FALSE);
1641 OsUserTaskStackInit(taskContext, (UINTPTR)taskCB->taskEntry, sp);//初始化用户栈,将内核栈中上下文的 context->R[0] = sp ,context->sp = sp
1642 //这样做的目的是将用户栈SP保存到内核栈中,
1643 SCHEDULER_UNLOCK(intSave);//解锁
1644 return LOS_OK;
1645}
LITE_OS_SEC_TEXT UINTPTR LOS_Align(UINTPTR addr, UINT32 boundary)
Align the value (addr) by some bytes (boundary) you specify.
Definition: los_misc.c:35
VOID * OsTaskStackInit(UINT32 taskID, UINT32 stackSize, VOID *topStack, BOOL initFlag)
内核态任务运行栈初始化
Definition: los_hw.c:73
VOID OsUserTaskStackInit(TaskContext *context, UINTPTR taskEntry, UINTPTR stack)
用户态运行栈初始化,此时上下文还在内核区
Definition: los_hw.c:123
UINT32 taskID
函数调用图:
这是这个函数的调用关系图:

◆ OsExitProcessGroup()

STATIC VOID OsExitProcessGroup ( LosProcessCB processCB,
ProcessGroup **  group 
)

退出进程组,参数是进程地址和进程组地址的地址

在文件 los_process.c184 行定义.

185{
186 LosProcessCB *groupProcessCB = OS_PCB_FROM_PID(processCB->group->groupID);//找到进程组老大进程的实体
187
188 LOS_ListDelete(&processCB->subordinateGroupList);//从进程组进程链表上摘出去
189 if (LOS_ListEmpty(&processCB->group->processList) && LOS_ListEmpty(&processCB->group->exitProcessList)) {//进程组进程链表和退出进程链表都为空时
190 LOS_ListDelete(&processCB->group->groupList);//从全局进程组链表上把自己摘出去 记住它是 LOS_ListTailInsert(&g_processGroup->groupList, &group->groupList) 挂上去的
191 groupProcessCB->processStatus &= ~OS_PROCESS_FLAG_GROUP_LEADER;//贴上不是组长的标签
192 *group = processCB->group;//????? 这步操作没看明白,谁能告诉我为何要这么做?
193 if (OsProcessIsUnused(groupProcessCB) && !(groupProcessCB->processStatus & OS_PROCESS_FLAG_EXIT)) {//组长进程时退出的标签
194 LOS_ListDelete(&groupProcessCB->pendList);//进程从全局进程链表上摘除
195 OsInsertPCBToFreeList(groupProcessCB);//释放进程的资源,回到freelist再利用
196 }
197 }
198
199 processCB->group = NULL;
200}
STATIC INLINE VOID OsInsertPCBToFreeList(LosProcessCB *processCB)
将进程插入到空闲链表中
Definition: los_process.c:97
函数调用图:
这是这个函数的调用关系图:

◆ OsFindChildProcess()

STATIC UINT32 OsFindChildProcess ( const LosProcessCB processCB,
INT32  childPid 
)

查找进程是否有指定孩子进程

在文件 los_process.c291 行定义.

292{
293 LosProcessCB *childCB = NULL;
294
295 if (childPid < 0) {
296 goto ERR;
297 }
298
299 LOS_DL_LIST_FOR_EACH_ENTRY(childCB, &(processCB->childrenList), LosProcessCB, siblingList) {//
300 if (childCB->processID == childPid) {
301 return LOS_OK;
302 }
303 }
304
305ERR:
306 PRINT_INFO("%s is find the child : %d failed in parent : %u\n", __FUNCTION__, childPid, processCB->processID);
307 return LOS_NOK;
308}
这是这个函数的调用关系图:

◆ OsFindExitChildProcess()

STATIC LosProcessCB * OsFindExitChildProcess ( const LosProcessCB processCB,
INT32  childPid 
)

找出指定进程的指定孩子进程

在文件 los_process.c311 行定义.

312{
313 LosProcessCB *exitChild = NULL;
314
315 LOS_DL_LIST_FOR_EACH_ENTRY(exitChild, &(processCB->exitChildList), LosProcessCB, siblingList) {
316 if ((childPid == OS_INVALID_VALUE) || (exitChild->processID == childPid)) {
317 return exitChild;
318 }
319 }
320
321 PRINT_INFO("%s is find the exit child : %d failed in parent : %u\n", __FUNCTION__, childPid, processCB->processID);
322 return NULL;
323}
这是这个函数的调用关系图:

◆ OsFindGroupExitProcess()

STATIC LosProcessCB * OsFindGroupExitProcess ( ProcessGroup group,
INT32  pid 
)

在文件 los_process.c276 行定义.

277{
278 LosProcessCB *childCB = NULL;
279
280 LOS_DL_LIST_FOR_EACH_ENTRY(childCB, &(group->exitProcessList), LosProcessCB, subordinateGroupList) {
281 if ((childCB->processID == pid) || (pid == OS_INVALID_VALUE)) {
282 return childCB;
283 }
284 }
285
286 PRINT_INFO("%s find exit process : %d failed in group : %u\n", __FUNCTION__, pid, group->groupID);
287 return NULL;
288}
这是这个函数的调用关系图:

◆ OsFindProcessGroup()

STATIC ProcessGroup * OsFindProcessGroup ( UINT32  gid)

通过指定组ID找到进程组

在文件 los_process.c203 行定义.

204{
205 ProcessGroup *group = NULL;
206 if (g_processGroup->groupID == gid) {
207 return g_processGroup;
208 }
209 //变量进程组
210 LOS_DL_LIST_FOR_EACH_ENTRY(group, &g_processGroup->groupList, ProcessGroup, groupList) {
211 if (group->groupID == gid) {
212 return group;
213 }
214 }
215
216 PRINT_INFO("%s failed! group id = %u\n", __FUNCTION__, gid);
217 return NULL;
218}
这是这个函数的调用关系图:

◆ OsForkInitPCB()

STATIC UINT32 OsForkInitPCB ( UINT32  flags,
LosProcessCB child,
const CHAR name,
UINTPTR  sp,
UINT32  size 
)

在文件 los_process.c1927 行定义.

1928{
1929 UINT32 ret;
1930 LosProcessCB *run = OsCurrProcessGet();//获取当前进程
1931
1932 ret = OsInitPCB(child, run->processMode, name);
1933 if (ret != LOS_OK) {
1934 return ret;
1935 }
1936
1937 ret = OsCopyParent(flags, child, run);//拷贝父亲大人的基因信息
1938 if (ret != LOS_OK) {
1939 return ret;
1940 }
1941
1942 return OsCopyTask(flags, child, name, sp, size);//拷贝任务,设置任务入口函数,栈大小
1943}
STATIC UINT32 OsInitPCB(LosProcessCB *processCB, UINT32 mode, const CHAR *name)
Definition: los_process.c:737
STATIC UINT32 OsCopyParent(UINT32 flags, LosProcessCB *childProcessCB, LosProcessCB *runProcessCB)
Definition: los_process.c:1861
STATIC UINT32 OsCopyTask(UINT32 flags, LosProcessCB *childProcessCB, const CHAR *name, UINTPTR entry, UINT32 size)
Definition: los_process.c:1803
UINT16 processMode
函数调用图:
这是这个函数的调用关系图:

◆ OsGetFreePCB()

STATIC LosProcessCB * OsGetFreePCB ( VOID  )

为用户态任务分配栈空间

在文件 los_process.c1491 行定义.

1492{
1493 LosProcessCB *processCB = NULL;
1494 UINT32 intSave;
1495
1496 SCHEDULER_LOCK(intSave);
1498 SCHEDULER_UNLOCK(intSave);
1499 PRINT_ERR("No idle PCB in the system!\n");
1500 return NULL;
1501 }
1502
1503 processCB = OS_PCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&g_freeProcess));
1504 LOS_ListDelete(&processCB->pendList);
1505 SCHEDULER_UNLOCK(intSave);
1506
1507 return processCB;
1508}
LITE_OS_SEC_DATA_INIT STATIC LOS_DL_LIST g_freeProcess
空闲状态下的进程链表, .个人觉得应该取名为 g_freeProcessList @note_thinking
Definition: los_process.c:81
函数调用图:
这是这个函数的调用关系图:

◆ OsGetIdleProcessID()

LITE_OS_SEC_TEXT UINT32 OsGetIdleProcessID ( VOID  )

获取内核态空闲进程

在文件 los_process.c2254 行定义.

2255{
2256 return g_kernelIdleProcess;
2257}
LITE_OS_SEC_BSS UINT32 g_kernelIdleProcess
0号进程 内核态idle进程,由Kprocess fork
Definition: los_process.c:85
这是这个函数的调用关系图:

◆ OsGetKernelInitProcessID()

LITE_OS_SEC_TEXT UINT32 OsGetKernelInitProcessID ( VOID  )

获取内核态根进程

在文件 los_process.c2249 行定义.

2250{
2251 return g_kernelInitProcess;
2252}
这是这个函数的调用关系图:

◆ OsGetProcessPriority()

LITE_OS_SEC_TEXT INT32 OsGetProcessPriority ( INT32  which,
INT32  pid 
)

接口封装 - 获取进程优先级 which:标识进程,进程组,用户

在文件 los_process.c1055 行定义.

1056{
1057 UINT32 intSave;
1058 SchedParam param = { 0 };
1059 (VOID)which;
1060
1061 if (OS_PID_CHECK_INVALID(pid)) {
1062 return -LOS_EINVAL;
1063 }
1064
1065 if (which != LOS_PRIO_PROCESS) {
1066 return -LOS_EINVAL;
1067 }
1068
1069 LosProcessCB *processCB = OS_PCB_FROM_PID(pid);
1070 SCHEDULER_LOCK(intSave);
1071 if (OsProcessIsUnused(processCB)) {
1072 SCHEDULER_UNLOCK(intSave);
1073 return -LOS_ESRCH;
1074 }
1075
1076 LosTaskCB *taskCB = OS_TCB_FROM_TID(processCB->threadGroupID);
1077 taskCB->ops->schedParamGet(taskCB, &param);
1078
1079 SCHEDULER_UNLOCK(intSave);
1080 return param.basePrio;
1081}
UINT16 basePrio
函数调用图:
这是这个函数的调用关系图:

◆ OsGetSigHandler()

LITE_OS_SEC_TEXT UINTPTR OsGetSigHandler ( VOID  )

获取进程的信号处理函数

在文件 los_process.c2264 行定义.

2265{
2266 return OsCurrProcessGet()->sigHandler;
2267}
函数调用图:
这是这个函数的调用关系图:

◆ OsGetUserInitProcessID()

LITE_OS_SEC_TEXT UINT32 OsGetUserInitProcessID ( VOID  )

获取用户态进程的根进程,所有用户进程都是g_processCBArray[g_userInitProcess] fork来的

在文件 los_process.c2244 行定义.

2245{
2246 return g_userInitProcess;//用户态根进程 序号为 1
2247}
这是这个函数的调用关系图:

◆ OsInitPCB()

STATIC UINT32 OsInitPCB ( LosProcessCB processCB,
UINT32  mode,
const CHAR name 
)

初始化PCB(进程控制块)

在文件 los_process.c737 行定义.

738{
739 processCB->processMode = mode; //用户态进程还是内核态进程
740 processCB->processStatus = OS_PROCESS_STATUS_INIT; //进程初始状态
741 processCB->parentProcessID = OS_INVALID_VALUE; //爸爸进程,外面指定
742 processCB->threadGroupID = OS_INVALID_VALUE; //所属线程组
743 processCB->umask = OS_PROCESS_DEFAULT_UMASK; //掩码
744 processCB->timerID = (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID;
745
746 LOS_ListInit(&processCB->threadSiblingList);//初始化孩子任务/线程链表,上面挂的都是由此fork的孩子线程 见于 OsTaskCBInit LOS_ListTailInsert(&(processCB->threadSiblingList), &(taskCB->threadList));
747 LOS_ListInit(&processCB->childrenList); //初始化孩子进程链表,上面挂的都是由此fork的孩子进程 见于 OsCopyParent LOS_ListTailInsert(&parentProcessCB->childrenList, &childProcessCB->siblingList);
748 LOS_ListInit(&processCB->exitChildList); //初始化记录退出孩子进程链表,上面挂的是哪些exit 见于 OsProcessNaturalExit LOS_ListTailInsert(&parentCB->exitChildList, &processCB->siblingList);
749 LOS_ListInit(&(processCB->waitList)); //初始化等待任务链表 上面挂的是处于等待的 见于 OsWaitInsertWaitLIstInOrder LOS_ListHeadInsert(&processCB->waitList, &runTask->pendList);
750
751#ifdef LOSCFG_KERNEL_VM
752 if (OsProcessIsUserMode(processCB)) {//如果是用户态进程
753 processCB->vmSpace = OsCreateUserVmSpace();//创建用户空间
754 if (processCB->vmSpace == NULL) {
755 processCB->processStatus = OS_PROCESS_FLAG_UNUSED;
756 return LOS_ENOMEM;
757 }
758 } else {
759 processCB->vmSpace = LOS_GetKVmSpace();//从这里也可以看出,所有内核态进程是共享一个进程空间的
760 }//在鸿蒙内核态进程只有kprocess 和 kidle 两个
761#endif
762
763#ifdef LOSCFG_KERNEL_CPUP
765 if (processCB->processCpup == NULL) {
766 return LOS_ENOMEM;
767 }
768 (VOID)memset_s(processCB->processCpup, sizeof(OsCpupBase), 0, sizeof(OsCpupBase));
769#endif
770#ifdef LOSCFG_SECURITY_VID
771 status_t status = VidMapListInit(processCB);
772 if (status != LOS_OK) {
773 return LOS_ENOMEM;
774 }
775#endif
776#ifdef LOSCFG_SECURITY_CAPABILITY
777 OsInitCapability(processCB);//初始化进程安全相关功能
778#endif
779
780 if (OsSetProcessName(processCB, name) != LOS_OK) {
781 return LOS_ENOMEM;
782 }
783
784 return LOS_OK;
785}
VOID OsInitCapability(LosProcessCB *processCB)
初始化进程安全能力
Definition: capability.c:54
UINT32 OsSetProcessName(LosProcessCB *processCB, const CHAR *name)
Definition: los_process.c:704
LosVmSpace * OsCreateUserVmSpace(VOID)
创建用户进程空间
Definition: los_vm_map.c:281
LosVmSpace * LOS_GetKVmSpace(VOID)
内核空间只有g_kVmSpace一个,所有的内核进程都共用一个内核空间
Definition: los_vm_map.c:130
LOS_DL_LIST waitList
OsCpupBase * processCpup
LOS_DL_LIST threadSiblingList
函数调用图:
这是这个函数的调用关系图:

◆ OsInsertPCBToFreeList()

STATIC INLINE VOID OsInsertPCBToFreeList ( LosProcessCB processCB)

将进程插入到空闲链表中

参数
argc1
[LosProcessCB]processCB 指定进程
返回
函数执行结果
  • VOID 无

在文件 los_process.c97 行定义.

98{
99 UINT32 pid = processCB->processID;//获取进程ID
100 (VOID)memset_s(processCB, sizeof(LosProcessCB), 0, sizeof(LosProcessCB));//进程描述符数据清0
101 processCB->processID = pid;//进程ID
102 processCB->processStatus = OS_PROCESS_FLAG_UNUSED;//设置为进程未使用
103 processCB->timerID = (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID;//timeID初始化值
104 LOS_ListTailInsert(&g_freeProcess, &processCB->pendList);//进程节点挂入g_freeProcess以分配给后续进程使用
105}
struct ProcessCB LosProcessCB
Definition: fd_table.h:105
函数调用图:
这是这个函数的调用关系图:

◆ OsLoadUserInit()

STATIC UINT32 OsLoadUserInit ( LosProcessCB processCB)

在文件 los_process.c1681 行定义.

1682{
1683 /* userInitTextStart -----
1684 * | user text |
1685 *
1686 * | user data | initSize
1687 * userInitBssStart ---
1688 * | user bss | initBssSize
1689 * userInitEnd --- -----
1690 */
1691 errno_t errRet;
1692 INT32 ret;
1693 CHAR *userInitTextStart = (CHAR *)&__user_init_entry;
1694 CHAR *userInitBssStart = (CHAR *)&__user_init_bss;
1695 CHAR *userInitEnd = (CHAR *)&__user_init_end;
1696 UINT32 initBssSize = userInitEnd - userInitBssStart;
1697 UINT32 initSize = userInitEnd - userInitTextStart;
1698 VOID *userBss = NULL;
1699 VOID *userText = NULL;
1700
1701 if ((LOS_Align((UINTPTR)userInitTextStart, PAGE_SIZE) != (UINTPTR)userInitTextStart) ||
1702 (LOS_Align((UINTPTR)userInitEnd, PAGE_SIZE) != (UINTPTR)userInitEnd)) {
1703 return LOS_EINVAL;
1704 }
1705
1706 if ((initSize == 0) || (initSize <= initBssSize)) {
1707 return LOS_EINVAL;
1708 }
1709
1710 userText = LOS_PhysPagesAllocContiguous(initSize >> PAGE_SHIFT);
1711 if (userText == NULL) {
1712 return LOS_NOK;
1713 }
1714
1715 errRet = memcpy_s(userText, initSize, (VOID *)&__user_init_load_addr, initSize - initBssSize);
1716 if (errRet != EOK) {
1717 PRINT_ERR("Load user init text, data and bss failed! err : %d\n", errRet);
1718 goto ERROR;
1719 }
1720 ret = LOS_VaddrToPaddrMmap(processCB->vmSpace, (VADDR_T)(UINTPTR)userInitTextStart, LOS_PaddrQuery(userText),
1721 initSize, VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE |
1722 VM_MAP_REGION_FLAG_FIXED | VM_MAP_REGION_FLAG_PERM_EXECUTE |
1723 VM_MAP_REGION_FLAG_PERM_USER);
1724 if (ret < 0) {
1725 PRINT_ERR("Mmap user init text, data and bss failed! err : %d\n", ret);
1726 goto ERROR;
1727 }
1728
1729 /* The User init boot segment may not actually exist */
1730 if (initBssSize != 0) {
1731 userBss = (VOID *)((UINTPTR)userText + userInitBssStart - userInitTextStart);
1732 errRet = memset_s(userBss, initBssSize, 0, initBssSize);
1733 if (errRet != EOK) {
1734 PRINT_ERR("memset user init bss failed! err : %d\n", errRet);
1735 goto ERROR;
1736 }
1737 }
1738
1739 return LOS_OK;
1740
1741ERROR:
1742 (VOID)LOS_PhysPagesFreeContiguous(userText, initSize >> PAGE_SHIFT);
1743 return LOS_NOK;
1744}
UINTPTR __user_init_load_addr
init 进程的加载地址 ,由链接器赋值
UINTPTR __user_init_bss
查看 LITE_USER_SEC_BSS ,赋值由liteos.ld完成
UINTPTR __user_init_end
init 进程的用户空间初始化结束地址
UINTPTR __user_init_entry
第一个用户态进程(init)的入口地址 查看 LITE_USER_SEC_ENTRY
unsigned long VADDR_T
Definition: los_typedef.h:208
PADDR_T LOS_PaddrQuery(VOID *vaddr)
通过虚拟地址查询映射的物理地址
Definition: los_vm_map.c:550
STATUS_T LOS_VaddrToPaddrMmap(LosVmSpace *space, VADDR_T vaddr, PADDR_T paddr, size_t len, UINT32 flags)
VOID * LOS_PhysPagesAllocContiguous(size_t nPages)
分配连续的物理页
Definition: los_vm_phys.c:478
VOID LOS_PhysPagesFreeContiguous(VOID *ptr, size_t nPages)
释放指定页数地址连续的物理内存
Definition: los_vm_phys.c:494
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessAddNewTask()

UINT32 OsProcessAddNewTask ( UINT32  pid,
LosTaskCB taskCB,
SchedParam param 
)

在文件 los_process.c116 行定义.

117{
118 UINT32 intSave;
119 UINT16 numCount;
120 LosProcessCB *processCB = OS_PCB_FROM_PID(pid);
121
122 SCHEDULER_LOCK(intSave);
123 taskCB->processID = pid;
124 LOS_ListTailInsert(&(processCB->threadSiblingList), &(taskCB->threadList));
125
126 if (OsProcessIsUserMode(processCB)) {
127 taskCB->taskStatus |= OS_TASK_FLAG_USER_MODE;
128 if (processCB->threadNumber > 0) {
129 LosTaskCB *task = OS_TCB_FROM_TID(processCB->threadGroupID);
130 task->ops->schedParamGet(task, param);
131 } else {
133 }
134 } else {
135 LosTaskCB *runTask = OsCurrTaskGet();
136 runTask->ops->schedParamGet(runTask, param);
137 }
138
139#ifdef LOSCFG_KERNEL_VM
140 taskCB->archMmu = (UINTPTR)&processCB->vmSpace->archMmu;
141#endif
142 if (!processCB->threadNumber) {
143 processCB->threadGroupID = taskCB->taskID;
144 }
145 processCB->threadNumber++;
146
147 numCount = processCB->threadCount;
148 processCB->threadCount++;
149 SCHEDULER_UNLOCK(intSave);
150 return numCount;
151}
VOID OsSchedProcessDefaultSchedParamGet(UINT16 policy, SchedParam *param)
Definition: los_sched.c:267
unsigned short UINT16
Definition: los_typedef.h:56
UINT32 threadCount
if(tv==NULL)
Definition: time.c:430
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessCapPermitCheck()

STATIC BOOL OsProcessCapPermitCheck ( const LosProcessCB processCB,
const SchedParam param,
UINT16  prio 
)

在文件 los_process.c961 行定义.

962{
963 LosProcessCB *runProcess = OsCurrProcessGet();//获得当前进程
964
965 /* always trust kernel process */
966 if (!OsProcessIsUserMode(runProcess)) {//进程必须在内核模式下,也就是说在内核模式下是安全的.
967 return TRUE;
968 }
969
970 /* user mode process can reduce the priority of itself */
971 if ((runProcess->processID == processCB->processID) && (prio > param->basePrio)) {
972 return TRUE;
973 }
974
975 /* user mode process with privilege of CAP_SCHED_SETPRIORITY can change the priority */
976 if (IsCapPermit(CAP_SCHED_SETPRIORITY)) {
977 return TRUE;
978 }
979 return FALSE;
980}
BOOL IsCapPermit(UINT32 capIndex)
Definition: capability.c:43
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessCBRecycleToFree()

LITE_OS_SEC_TEXT VOID OsProcessCBRecycleToFree ( VOID  )

进程回收再利用过程

在文件 los_process.c625 行定义.

626{
627 UINT32 intSave;
628 LosProcessCB *processCB = NULL;
629
630 SCHEDULER_LOCK(intSave);
631 while (!LOS_ListEmpty(&g_processRecycleList)) {//循环任务回收链表,直到为空
632 processCB = OS_PCB_FROM_PENDLIST(LOS_DL_LIST_FIRST(&g_processRecycleList));//找到回收链表中第一个进程实体
633 //OS_PCB_FROM_PENDLIST 代表的是通过pendlist节点找到 PCB实体,因为g_processRecyleList上面挂的是pendlist节点位置
634 if (!(processCB->processStatus & OS_PROCESS_FLAG_EXIT)) {//进程没有退出标签
635 break;
636 }
637 SCHEDULER_UNLOCK(intSave);
638
640
641 SCHEDULER_LOCK(intSave);
642 processCB->processStatus &= ~OS_PROCESS_FLAG_EXIT;//给进程撕掉退出标签,(可能进程并没有这个标签)
643#ifdef LOSCFG_KERNEL_VM
644 LosVmSpace *space = NULL;
645 if (OsProcessIsUserMode(processCB)) {//进程是否是用户态进程
646 space = processCB->vmSpace;//只有用户态的进程才需要释放虚拟内存空间
647 }
648 processCB->vmSpace = NULL;
649#endif
650 /* OS_PROCESS_FLAG_GROUP_LEADER: The lead process group cannot be recycled without destroying the PCB.
651 * !OS_PROCESS_FLAG_UNUSED: Parent process does not reclaim child process resources.
652 */
653 LOS_ListDelete(&processCB->pendList);//将进程从进程链表上摘除
654 if ((processCB->processStatus & OS_PROCESS_FLAG_GROUP_LEADER) ||//如果进程是进程组组长或者处于僵死状态
655 (processCB->processStatus & OS_PROCESS_STATUS_ZOMBIES)) {
656 LOS_ListTailInsert(&g_processRecycleList, &processCB->pendList);//将进程挂到进程回收链表上,因为组长不能走啊
657 } else {
658 /* Clear the bottom 4 bits of process status */
659 OsInsertPCBToFreeList(processCB);//进程回到可分配池中,再分配利用
660 }
661#ifdef LOSCFG_KERNEL_VM
662 SCHEDULER_UNLOCK(intSave);
663 (VOID)LOS_VmSpaceFree(space);//释放用户态进程的虚拟内存空间,因为内核只有一个虚拟空间,因此不需要释放虚拟空间.
664 SCHEDULER_LOCK(intSave);
665#endif
666 }
667
668 SCHEDULER_UNLOCK(intSave);
669}
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessCreateInit()

STATIC UINT32 OsProcessCreateInit ( LosProcessCB processCB,
UINT32  flags,
const CHAR name 
)

进程创建初始化

在文件 los_process.c859 行定义.

860{
861 ProcessGroup *group = NULL;
862 UINT32 ret = OsInitPCB(processCB, flags, name);
863 if (ret != LOS_OK) {
864 goto EXIT;
865 }
866
867#ifdef LOSCFG_FS_VFS
868 processCB->files = alloc_files();//分配进程的文件的管理器
869 if (processCB->files == NULL) {
870 ret = LOS_ENOMEM;
871 goto EXIT;
872 }
873#endif
874
875 group = OsCreateProcessGroup(processCB->processID);//创建进程组
876 if (group == NULL) {
877 ret = LOS_ENOMEM;
878 goto EXIT;
879 }
880
881#ifdef LOSCFG_SECURITY_CAPABILITY //用户安全宏
882 processCB->user = OsCreateUser(0, 0, 1);//创建用户
883 if (processCB->user == NULL) {
884 ret = LOS_ENOMEM;
885 goto EXIT;
886 }
887#endif
888
889 return LOS_OK;
890
891EXIT:
892 OsDeInitPCB(processCB);//删除进程控制块,归还内存
893 return ret;
894}
struct files_struct * alloc_files(void)
为进程分配文件管理器,其中包含fd总数,(0,1,2)默认给了stdin,stdout,stderr
STATIC User * OsCreateUser(UINT32 userID, UINT32 gid, UINT32 size)
Definition: los_process.c:788
STATIC ProcessGroup * OsCreateProcessGroup(UINT32 pid)
创建进程组
Definition: los_process.c:160
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessInit()

STATIC UINT32 OsProcessInit ( VOID  )

进程模块初始化,被编译放在代码段 .init 中

在文件 los_process.c586 行定义.

587{
588 UINT32 index;
589 UINT32 size;
590
591 g_processMaxNum = LOSCFG_BASE_CORE_PROCESS_LIMIT;//默认支持64个进程
592 size = g_processMaxNum * sizeof(LosProcessCB);//算出总大小
593
594 g_processCBArray = (LosProcessCB *)LOS_MemAlloc(m_aucSysMem1, size);// 进程池,占用内核堆,内存池分配
595 if (g_processCBArray == NULL) {
596 return LOS_NOK;
597 }
598 (VOID)memset_s(g_processCBArray, size, 0, size);//安全方式重置清0
599
600 LOS_ListInit(&g_freeProcess);//进程空闲链表初始化,创建一个进程时从g_freeProcess中申请一个进程描述符使用
601 LOS_ListInit(&g_processRecycleList);//进程回收链表初始化,回收完成后进入g_freeProcess等待再次被申请使用
602
603 for (index = 0; index < g_processMaxNum; index++) {//进程池循环创建
604 g_processCBArray[index].processID = index;//进程ID[0-g_processMaxNum-1]赋值
605 g_processCBArray[index].processStatus = OS_PROCESS_FLAG_UNUSED;// 默认都是白纸一张,贴上未使用标签
606 LOS_ListTailInsert(&g_freeProcess, &g_processCBArray[index].pendList);//注意g_freeProcess挂的是pendList节点,所以使用要通过OS_PCB_FROM_PENDLIST找到进程实体.
607 }
608
609 g_kernelIdleProcess = 0; /* 0: The idle process ID of the kernel-mode process is fixed at 0 *///内核态init进程,从名字可以看出来这是让cpu休息的进程.
610 LOS_ListDelete(&OS_PCB_FROM_PID(g_kernelIdleProcess)->pendList);//从空闲链表中摘掉
611
612 g_userInitProcess = 1; /* 1: The root process ID of the user-mode process is fixed at 1 *///用户态的根进程
613 LOS_ListDelete(&OS_PCB_FROM_PID(g_userInitProcess)->pendList);//从空闲链表中摘掉
614
615 g_kernelInitProcess = 2; /* 2: The root process ID of the kernel-mode process is fixed at 2 *///内核态的根进程
616 LOS_ListDelete(&OS_PCB_FROM_PID(g_kernelInitProcess)->pendList);//从空闲链表中摘掉
617
618 //注意:这波骚操作之后,g_freeProcess链表上还有[3,g_processMaxNum-1]号进程.创建进程是从g_freeProcess上申请
619 //即下次申请到的将是0号进程,而 OsCreateIdleProcess 将占有0号进程.
620
621 return LOS_OK;
622}
LITE_OS_SEC_BSS LosProcessCB * g_processCBArray
进程池数组
Definition: los_process.c:80
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessNaturalExit()

VOID OsProcessNaturalExit ( LosProcessCB processCB,
UINT32  status 
)

一个进程的自然消亡过程,参数是当前运行的任务

在文件 los_process.c550 行定义.

551{
552 LosProcessCB *parentCB = NULL;
553
554 OsChildProcessResourcesFree(processCB);//释放孩子进程的资源
555
556
557 /* is a child process */
558 if (processCB->parentProcessID != OS_INVALID_VALUE) {//判断是否有父进程
559 parentCB = OS_PCB_FROM_PID(processCB->parentProcessID);//获取父进程实体
560 LOS_ListDelete(&processCB->siblingList);//将自己从兄弟链表中摘除,家人们,永别了!
561 if (!OsProcessExitCodeSignalIsSet(processCB)) {//是否设置了退出码?
562 OsProcessExitCodeSet(processCB, status);//将进程状态设为退出码
563 }
564 LOS_ListTailInsert(&parentCB->exitChildList, &processCB->siblingList);//挂到父进程的孩子消亡链表,家人中,永别的可不止我一个.
565 LOS_ListDelete(&processCB->subordinateGroupList);//和志同道合的朋友们永别了,注意家里可不一定是朋友的,所有各有链表.
566 LOS_ListTailInsert(&processCB->group->exitProcessList, &processCB->subordinateGroupList);//挂到进程组消亡链表,朋友中,永别的可不止我一个.
567
568 OsWaitCheckAndWakeParentProcess(parentCB, processCB);//检查父进程的等待任务链表并唤醒对应的任务,此处将会频繁的切到其他任务运行.
569
570 OsDealAliveChildProcess(processCB);//孩子们要怎么处理,移交给(用户态和内核态)根进程
571
572 processCB->processStatus |= OS_PROCESS_STATUS_ZOMBIES;//贴上僵死进程的标签
573
574#ifdef LOSCFG_KERNEL_VM
575 (VOID)OsKill(processCB->parentProcessID, SIGCHLD, OS_KERNEL_KILL_PERMISSION);//以内核权限发送SIGCHLD(子进程退出)信号.
576#endif
577 LOS_ListHeadInsert(&g_processRecycleList, &processCB->pendList);//将进程通过其阻塞节点挂入全局进程回收链表
578 return;
579 }
580
581 LOS_Panic("pid : %u is the root process exit!\n", processCB->processID);
582 return;
583}
STATIC VOID OsDealAliveChildProcess(LosProcessCB *processCB)
Definition: los_process.c:503
STATIC VOID OsChildProcessResourcesFree(const LosProcessCB *processCB)
Definition: los_process.c:537
STATIC VOID OsWaitCheckAndWakeParentProcess(LosProcessCB *parentCB, const LosProcessCB *processCB)
Definition: los_process.c:368
STATIC INLINE BOOL OsProcessExitCodeSignalIsSet(LosProcessCB *processCB)
进程退出码是否被设置过,默认是 0 ,如果 & 0x7FU 还是 0 ,说明没有被设置过.
STATIC INLINE VOID OsProcessExitCodeSet(LosProcessCB *processCB, UINT32 code)
设置进程退出号(8 ~ 15)
int OsKill(pid_t pid, int sig, int permission)
Definition: los_signal.c:450
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessResourcesToFree()

LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree ( LosProcessCB processCB)

回收指定进程的资源

在文件 los_process.c431 行定义.

432{
433#ifdef LOSCFG_KERNEL_VM
434 if (OsProcessIsUserMode(processCB)) {
435 (VOID)OsVmSpaceRegionFree(processCB->vmSpace);
436 }
437#endif
438#ifdef LOSCFG_FS_VFS
439 if (OsProcessIsUserMode(processCB)) {//用户进程
440 delete_files(processCB->files);//归还进程占用的进程描述符`profd`,如果是最后一个占用的系统描述符的进程,则同时归还系统文件描述符`sysfd`
441 }
442 processCB->files = NULL; //重置指针为空
443#endif
444
445#ifdef LOSCFG_SECURITY_CAPABILITY //安全开关
446 if (processCB->user != NULL) {
447 (VOID)LOS_MemFree(m_aucSysMem1, processCB->user);//删除用户
448 processCB->user = NULL; //重置指针为空
449 }
450#endif
451
452 OsSwtmrRecycle(processCB->processID);//回收进程使用的定时器
453 processCB->timerID = (timer_t)(UINTPTR)MAX_INVALID_TIMER_VID;
454
455#ifdef LOSCFG_SECURITY_VID
456 if (processCB->timerIdMap.bitMap != NULL) {
457 VidMapDestroy(processCB);
458 processCB->timerIdMap.bitMap = NULL;
459 }
460#endif
461
462#ifdef LOSCFG_KERNEL_LITEIPC
463 (VOID)LiteIpcPoolDestroy(processCB->processID);
464#endif
465
466#ifdef LOSCFG_KERNEL_CPUP
467 UINT32 intSave;
468 OsCpupBase *processCpup = processCB->processCpup;
469 SCHEDULER_LOCK(intSave);
470 processCB->processCpup = NULL;
471 SCHEDULER_UNLOCK(intSave);
472 (VOID)LOS_MemFree(m_aucSysMem1, processCpup);
473#endif
474
475 if (processCB->resourceLimit != NULL) {
476 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, processCB->resourceLimit);
477 processCB->resourceLimit = NULL;
478 }
479}
void delete_files(struct files_struct *files)
删除参数进程的文件管理器
UINT8 * m_aucSysMem0
异常交互动态内存池地址的起始地址,当不支持异常交互特性时,m_aucSysMem0等于m_aucSysMem1。
Definition: los_memory.c:107
STATUS_T OsVmSpaceRegionFree(LosVmSpace *space)
struct rlimit * resourceLimit
每个进程在运行时系统不会无限制的允许单个进程不断的消耗资源,因此都会设置资源限制。
TimerIdMap timerIdMap
UINT32 * bitMap
Definition: vid_type.h:40
函数调用图:
这是这个函数的调用关系图:

◆ OsProcessSchedlerParamCheck()

STATIC INLINE INT32 OsProcessSchedlerParamCheck ( INT32  which,
INT32  pid,
UINT16  prio,
UINT16  policy 
)

进程调度参数检查

在文件 los_process.c939 行定义.

940{
941 if (OS_PID_CHECK_INVALID(pid)) {//进程ID是否有效,默认 g_processMaxNum = 64
942 return LOS_EINVAL;
943 }
944
945 if (which != LOS_PRIO_PROCESS) {//进程标识
946 return LOS_EINVAL;
947 }
948
949 if (prio > OS_PROCESS_PRIORITY_LOWEST) {//鸿蒙优先级是 0 -31级,0级最大
950 return LOS_EINVAL;//返回无效参数
951 }
952
953 if (policy != LOS_SCHED_RR) {
954 return LOS_EINVAL;
955 }
956
957 return LOS_OK;
958}
这是这个函数的调用关系图:

◆ OsProcessThreadGroupDestroy()

LITE_OS_SEC_TEXT VOID OsProcessThreadGroupDestroy ( VOID  )

在文件 los_process.c2200 行定义.

2201{
2202#ifdef LOSCFG_KERNEL_VM
2203 UINT32 intSave;
2204
2205 LosProcessCB *processCB = OsCurrProcessGet();
2206 LosTaskCB *currTask = OsCurrTaskGet();
2207 SCHEDULER_LOCK(intSave);
2208 if ((processCB->processStatus & OS_PROCESS_FLAG_EXIT) || !OsProcessIsUserMode(processCB)) {
2209 SCHEDULER_UNLOCK(intSave);
2210 return;
2211 }
2212
2213 processCB->processStatus |= OS_PROCESS_FLAG_EXIT;
2214 processCB->threadGroupID = currTask->taskID;
2215
2216 LOS_DL_LIST *list = &processCB->threadSiblingList;
2217 LOS_DL_LIST *head = list;
2218 do {
2219 LosTaskCB *taskCB = LOS_DL_LIST_ENTRY(list->pstNext, LosTaskCB, threadList);
2220 if ((OsTaskIsInactive(taskCB) ||
2221 ((taskCB->taskStatus & OS_TASK_STATUS_READY) && !taskCB->sig.sigIntLock)) &&
2222 !(taskCB->taskStatus & OS_TASK_STATUS_RUNNING)) {
2223 OsInactiveTaskDelete(taskCB);
2224 } else if (taskCB != currTask) {
2226 } else {
2227 /* Skip the current task */
2228 list = list->pstNext;
2229 }
2230 } while (head != list->pstNext);
2231
2232 SCHEDULER_UNLOCK(intSave);
2233
2234 LOS_ASSERT(processCB->threadNumber == 1);
2235#endif
2236 return;
2237}
STATIC VOID ThreadGroupActiveTaskKilled(LosTaskCB *taskCB)
按指定状态退出指定进程
Definition: los_process.c:2167
STATIC INLINE BOOL OsTaskIsInactive(const LosTaskCB *taskCB)
LITE_OS_SEC_TEXT VOID OsInactiveTaskDelete(LosTaskCB *taskCB)
Definition: los_task.c:947
unsigned int sigIntLock
信号中断锁
Definition: los_signal.h:229
函数调用图:
这是这个函数的调用关系图:

◆ OsRecycleZombiesProcess()

LITE_OS_SEC_TEXT STATIC VOID OsRecycleZombiesProcess ( LosProcessCB childCB,
ProcessGroup **  group 
)

回收僵死状态进程的资源

在文件 los_process.c482 行定义.

483{
484 OsExitProcessGroup(childCB, group);//退出进程组
485 LOS_ListDelete(&childCB->siblingList);//从父亲大人的子孙链表上摘除
486 if (childCB->processStatus & OS_PROCESS_STATUS_ZOMBIES) {//如果身上僵死状态的标签
487 OsDeleteTaskFromProcess(OS_TCB_FROM_TID(childCB->threadGroupID));
488 childCB->processStatus &= ~OS_PROCESS_STATUS_ZOMBIES;//去掉僵死标签
489 childCB->processStatus |= OS_PROCESS_FLAG_UNUSED;//贴上没使用标签,进程由进程池分配,进程退出后重新回到空闲进程池
490 }
491
492 LOS_ListDelete(&childCB->pendList);//将自己从阻塞链表上摘除,注意有很多原因引起阻塞,pendList挂在哪里就以为这属于哪类阻塞
493 if (childCB->processStatus & OS_PROCESS_FLAG_EXIT) {//如果有退出标签
494 LOS_ListHeadInsert(&g_processRecycleList, &childCB->pendList);//从头部插入,注意g_processRecyleList挂的是pendList节点,所以要通过OS_PCB_FROM_PENDLIST找.
495 } else if (childCB->processStatus & OS_PROCESS_FLAG_GROUP_LEADER) {//如果是进程组的组长
496 LOS_ListTailInsert(&g_processRecycleList, &childCB->pendList);//从尾部插入,意思就是组长尽量最后一个处理
497 } else {
498 OsInsertPCBToFreeList(childCB);//直接插到freeList中去,可用于重新分配了。
499 }
500}
VOID OsDeleteTaskFromProcess(LosTaskCB *taskCB)
Definition: los_process.c:107
函数调用图:
这是这个函数的调用关系图:

◆ OsSendSignalToAllProcess()

LITE_OS_SEC_TEXT INT32 OsSendSignalToAllProcess ( siginfo_t *  info,
INT32  permission 
)

给所有进程发送指定信号

在文件 los_process.c242 行定义.

243{
244 INT32 ret, success, err;
245 ProcessGroup *group = NULL;
246
247 success = 0;
248 err = OsSendSignalToSpecifyProcessGroup(g_processGroup, info, permission);//给g_processGroup进程组发送信号
249 success |= !err;
250 ret = err;
251 /* all processes group */
252 LOS_DL_LIST_FOR_EACH_ENTRY(group, &g_processGroup->groupList, ProcessGroup, groupList) {//遍历进程组
253 /* all processes in the process group. */
254 err = OsSendSignalToSpecifyProcessGroup(group, info, permission);
255 success |= !err;
256 ret = err;
257 }
258 return success ? LOS_OK : ret;
259}
STATIC INT32 OsSendSignalToSpecifyProcessGroup(ProcessGroup *group, siginfo_t *info, INT32 permission)
Definition: los_process.c:221
函数调用图:
这是这个函数的调用关系图:

◆ OsSendSignalToProcessGroup()

LITE_OS_SEC_TEXT INT32 OsSendSignalToProcessGroup ( INT32  pid,
siginfo_t *  info,
INT32  permission 
)

发送指定信号给给进程组

在文件 los_process.c262 行定义.

263{
264 ProcessGroup *group = NULL;
265 /* Send SIG to all processes in process group PGRP.
266 If PGRP is zero, send SIG to all processes in
267 the current process's process group. */
268 group = OsFindProcessGroup(pid ? -pid : LOS_GetCurrProcessGroupID());
269 if (group == NULL) {
270 return -LOS_ESRCH;
271 }
272 /* all processes in the process group. */
273 return OsSendSignalToSpecifyProcessGroup(group, info, permission);
274}
STATIC ProcessGroup * OsFindProcessGroup(UINT32 gid)
Definition: los_process.c:203
LITE_OS_SEC_TEXT INT32 LOS_GetCurrProcessGroupID(VOID)
获取当前进程的组ID
Definition: los_process.c:1485
函数调用图:
这是这个函数的调用关系图:

◆ OsSendSignalToSpecifyProcessGroup()

STATIC INT32 OsSendSignalToSpecifyProcessGroup ( ProcessGroup group,
siginfo_t *  info,
INT32  permission 
)

给指定进程组发送信号

在文件 los_process.c221 行定义.

222{
223 INT32 ret, success, err;
224 LosProcessCB *childCB = NULL;
225
226 success = 0;
227 ret = -LOS_ESRCH;
228 LOS_DL_LIST_FOR_EACH_ENTRY(childCB, &(group->processList), LosProcessCB, subordinateGroupList) {//遍历进程组内的进程
229 if (childCB->processID == 0) {//0号进程为KIdle进程,是让CPU休息的进程,不处理信号
230 continue;
231 }
232
233 err = OsDispatch(childCB->processID, info, permission);//给进程发送信号
234 success |= !err;
235 ret = err;
236 }
237 /* At least one success. */
238 return success ? LOS_OK : ret;
239}
int OsDispatch(pid_t pid, siginfo_t *info, int permission)
信号分发,发送信号权限/进程组过滤.
Definition: los_signal.c:412
函数调用图:
这是这个函数的调用关系图:

◆ OsSetCurrProcessGroupID()

LITE_OS_SEC_TEXT INT32 OsSetCurrProcessGroupID ( UINT32  gid)

在文件 los_process.c1456 行定义.

1457{
1458 return OsSetProcessGroupID(OsCurrProcessGet()->processID, gid);
1459}
LITE_OS_SEC_TEXT INT32 OsSetProcessGroupID(UINT32 pid, UINT32 gid)
Definition: los_process.c:1439
函数调用图:
这是这个函数的调用关系图:

◆ OsSetProcessGroupCheck()

STATIC UINT32 OsSetProcessGroupCheck ( const LosProcessCB processCB,
UINT32  gid 
)

设置进程组检查

在文件 los_process.c1365 行定义.

1366{
1367 LosProcessCB *runProcessCB = OsCurrProcessGet();//拿到当前运行进程
1368 LosProcessCB *groupProcessCB = OS_PCB_FROM_PID(gid);//通过组ID拿到组长PCB实体
1369
1370 if (OsProcessIsInactive(processCB)) {//进程是否活动
1371 return LOS_ESRCH;
1372 }
1373 //参数进程不在用户态或者组长不在用户态
1374 if (!OsProcessIsUserMode(processCB) || !OsProcessIsUserMode(groupProcessCB)) {
1375 return LOS_EPERM;
1376 }
1377
1378 if (runProcessCB->processID == processCB->parentProcessID) {
1379 if (processCB->processStatus & OS_PROCESS_FLAG_ALREADY_EXEC) {
1380 return LOS_EACCES;
1381 }
1382 } else if (processCB->processID != runProcessCB->processID) {
1383 return LOS_ESRCH;
1384 }
1385
1386 /* Add the process to another existing process group */
1387 if (processCB->processID != gid) {
1388 if (!(groupProcessCB->processStatus & OS_PROCESS_FLAG_GROUP_LEADER)) {
1389 return LOS_EPERM;
1390 }
1391
1392 if ((groupProcessCB->parentProcessID != processCB->parentProcessID) && (gid != processCB->parentProcessID)) {
1393 return LOS_EPERM;
1394 }
1395 }
1396
1397 return LOS_OK;
1398}
STATIC INLINE BOOL OsProcessIsInactive(const LosProcessCB *processCB)
进程不活跃函数定义:身上贴有不使用且不活跃标签的进程
函数调用图:
这是这个函数的调用关系图:

◆ OsSetProcessGroupID()

LITE_OS_SEC_TEXT INT32 OsSetProcessGroupID ( UINT32  pid,
UINT32  gid 
)

在文件 los_process.c1439 行定义.

1440{
1441 ProcessGroup *group = NULL;
1442 UINT32 ret;
1443 UINT32 intSave;
1444
1445 if ((OS_PID_CHECK_INVALID(pid)) || (OS_PID_CHECK_INVALID(gid))) {
1446 return -LOS_EINVAL;
1447 }
1448
1449 SCHEDULER_LOCK(intSave);
1450 ret = OsSetProcessGroupIDUnsafe(pid, gid, &group);
1451 SCHEDULER_UNLOCK(intSave);
1452 (VOID)LOS_MemFree(m_aucSysMem1, group);
1453 return -ret;
1454}
函数调用图:
这是这个函数的调用关系图:

◆ OsSetProcessGroupIDUnsafe()

STATIC UINT32 OsSetProcessGroupIDUnsafe ( UINT32  pid,
UINT32  gid,
ProcessGroup **  group 
)

在文件 los_process.c1400 行定义.

1401{
1402 ProcessGroup *oldGroup = NULL;
1403 ProcessGroup *newGroup = NULL;
1404 LosProcessCB *processCB = OS_PCB_FROM_PID(pid);
1405 UINT32 ret = OsSetProcessGroupCheck(processCB, gid);
1406 if (ret != LOS_OK) {
1407 return ret;
1408 }
1409
1410 if (processCB->group->groupID == gid) {
1411 return LOS_OK;
1412 }
1413
1414 oldGroup = processCB->group;
1415 OsExitProcessGroup(processCB, group);
1416
1417 newGroup = OsFindProcessGroup(gid);
1418 if (newGroup != NULL) {
1419 LOS_ListTailInsert(&newGroup->processList, &processCB->subordinateGroupList);
1420 processCB->group = newGroup;
1421 return LOS_OK;
1422 }
1423
1424 newGroup = OsCreateProcessGroup(gid);
1425 if (newGroup == NULL) {
1426 LOS_ListTailInsert(&oldGroup->processList, &processCB->subordinateGroupList);
1427 processCB->group = oldGroup;
1428 if (*group != NULL) {
1430 processCB = OS_PCB_FROM_PID(oldGroup->groupID);
1431 processCB->processStatus |= OS_PROCESS_FLAG_GROUP_LEADER;
1432 *group = NULL;
1433 }
1434 return LOS_EPERM;
1435 }
1436 return LOS_OK;
1437}
STATIC UINT32 OsSetProcessGroupCheck(const LosProcessCB *processCB, UINT32 gid)
设置进程组检查
Definition: los_process.c:1365
函数调用图:
这是这个函数的调用关系图:

◆ OsSetProcessName()

UINT32 OsSetProcessName ( LosProcessCB processCB,
const CHAR name 
)

设置进程的名字

在文件 los_process.c704 行定义.

705{
706 errno_t errRet;
707
708 if (processCB == NULL) {
709 return LOS_EINVAL;
710 }
711
712 if (name != NULL) {
713 errRet = strncpy_s(processCB->processName, OS_PCB_NAME_LEN, name, OS_PCB_NAME_LEN - 1);
714 if (errRet == EOK) {
715 return LOS_OK;
716 }
717 }
718
719 switch (processCB->processMode) {
720 case OS_KERNEL_MODE:
721 errRet = snprintf_s(processCB->processName, OS_PCB_NAME_LEN, OS_PCB_NAME_LEN - 1,
722 "KerProcess%u", processCB->processID);
723 break;
724 default:
725 errRet = snprintf_s(processCB->processName, OS_PCB_NAME_LEN, OS_PCB_NAME_LEN - 1,
726 "UserProcess%u", processCB->processID);
727 break;
728 }
729
730 if (errRet < 0) {
731 return LOS_NOK;
732 }
733 return LOS_OK;
734}