1# Task 2 3## Basic Concepts<a name="section138411646175417"></a> 4 5Tasks are the minimum running units that compete for system resources. They can use or wait to use CPUs and use system resources such as memory. They run independently from one another. 6 7In the OpenHarmony kernel, a task represents a thread. 8 9Tasks in the processes of the same priority in the OpenHarmony kernel are scheduled and run in a unified manner. 10 11The tasks in the kernel use the preemptive scheduling mechanism, either round-robin \(RR\) scheduling or First In First Out \(FIFO\) scheduling. 12 13Tasks are assigned 32 priorities, ranging from **0** \(highest\) to **31** \(lowest\). 14 15In the same process, a higher-priority task can preempt resources of a lower-priority task. The lower-priority task can be scheduled only after the higher-priority task is blocked or terminated. 16 17**Task Status Description** 18 19- Init: The task is being created. 20- Ready: The task is in the Ready queue and waits for scheduling by the CPU. 21- Running: The task is running. 22- Blocked: The task is blocked and suspended. The Blocked states include pending \(blocked due to lock, event, or semaphore issues\), suspended \(active pending\), delay \(blocked due to delays\), and pendtime \(blocked by waiting timeout of locks, events, or semaphores\). 23- Exit: The task is complete and waits for the parent task to reclaim its control block resources. 24 25**Figure 1** Task state transition<a name="fig5251243193113"></a> 26 27 28**Task State Transition** 29 30- Init→Ready: 31 32 When a task is created, the task obtains the control block and enters the Init state \(initialization\). After the initialization is complete, the task is inserted into the scheduling queue and enters the Ready state. 33 34- Ready→Running: 35 36 When a task switching is triggered, the task with the highest priority in the Ready queue is executed and enters the Running state. Then, this task is deleted from the Ready queue. 37 38- Running→Blocked: 39 40 When a running task is blocked \(for example, is pended, delayed, or reading semaphores\), its state changes from Running to Blocked. Then, a task switching is triggered to run the task with the highest priority in the Ready queue. 41 42- Blocked→Ready: 43 44 After the blocked task is restored \(the task is restored, the delay times out, the semaphore reading times out, or the semaphore is read\), the task is added to the Ready queue and will change from the Blocked state to the Ready state. 45 46- Ready→Blocked: 47 48 When a task in the Ready state is blocked \(suspended\), the task changes to the Blocked state and is deleted from the Ready queue. The blocked task will not be scheduled until it is recovered. 49 50- Running→Ready: 51 52 When a task with a higher priority is created or recovered, tasks will be scheduled. The task with the highest priority in the Ready queue changes to the Running state. The originally running task changes to the Ready state and is added to the Ready queue. 53 54- Running→Exit: 55 56 When a running task is complete, it changes to the Exit state. If the task is set with a detach attribute \(**LOS\_TASK\_STATUS\_DETACHED**\), it will be directly destroyed after being terminated. 57 58 59## Working Principles<a name="section1381918945512"></a> 60 61The OpenHarmony task management module provides the following functions: creating, delaying, suspending, and restoring tasks, locking and unlocking task scheduling, and querying task control block information by ID. 62 63When a task is created, the system initializes the task stack and presets the context. The system also places the task entry function in the corresponding position so that the function can be executed when the task enters the running state for the first time. 64 65## Development Guidelines<a name="section10649727135519"></a> 66 67### Available APIs<a name="section78333315555"></a> 68 69<a name="table687929113814"></a> 70<table><thead align="left"><tr id="row513082983812"><th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.1.4.1.1"><p id="p121309298384"><a name="p121309298384"></a><a name="p121309298384"></a><strong id="b1288394945813"><a name="b1288394945813"></a><a name="b1288394945813"></a>Function</strong></p> 71</th> 72<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.1.4.1.2"><p id="p713082933817"><a name="p713082933817"></a><a name="p713082933817"></a><strong id="b184372716456"><a name="b184372716456"></a><a name="b184372716456"></a>API</strong></p> 73</th> 74<th class="cellrowborder" valign="top" width="33.33333333333333%" id="mcps1.1.4.1.3"><p id="p20130829123810"><a name="p20130829123810"></a><a name="p20130829123810"></a><strong id="b27211529174516"><a name="b27211529174516"></a><a name="b27211529174516"></a>Description</strong></p> 75</th> 76</tr> 77</thead> 78<tbody><tr id="row431611256303"><td class="cellrowborder" rowspan="3" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p0297202573015"><a name="p0297202573015"></a><a name="p0297202573015"></a>Task creation and deletion</p> 79</td> 80<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p1229720252303"><a name="p1229720252303"></a><a name="p1229720252303"></a>LOS_TaskCreateOnly</p> 81</td> 82<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p12297825123016"><a name="p12297825123016"></a><a name="p12297825123016"></a>Creates a task and places the task in the Init state without scheduling.</p> 83</td> 84</tr> 85<tr id="row19316182517304"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p1829732511308"><a name="p1829732511308"></a><a name="p1829732511308"></a>LOS_TaskCreate</p> 86</td> 87<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p19297122518303"><a name="p19297122518303"></a><a name="p19297122518303"></a>Creates a task and places it in the Init state for scheduling.</p> 88</td> 89</tr> 90<tr id="row7316192593013"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p3297162583020"><a name="p3297162583020"></a><a name="p3297162583020"></a>LOS_TaskDelete</p> 91</td> 92<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p3297162517308"><a name="p3297162517308"></a><a name="p3297162517308"></a>Deletes the specified task.</p> 93</td> 94</tr> 95<tr id="row13316112533020"><td class="cellrowborder" rowspan="6" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p172979255306"><a name="p172979255306"></a><a name="p172979255306"></a>Task status control</p> 96</td> 97<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p14297425103016"><a name="p14297425103016"></a><a name="p14297425103016"></a>LOS_TaskResume</p> 98</td> 99<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p1929732593018"><a name="p1929732593018"></a><a name="p1929732593018"></a>Resumes a suspended task.</p> 100</td> 101</tr> 102<tr id="row1231632513012"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p5297132553014"><a name="p5297132553014"></a><a name="p5297132553014"></a>LOS_TaskSuspend</p> 103</td> 104<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p8297182563015"><a name="p8297182563015"></a><a name="p8297182563015"></a>Suspends the specified task.</p> 105</td> 106</tr> 107<tr id="row1547201019421"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p5885913184219"><a name="p5885913184219"></a><a name="p5885913184219"></a>LOS_TaskJoin</p> 108</td> 109<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p28851313104213"><a name="p28851313104213"></a><a name="p28851313104213"></a>Suspends this task till the specified task is complete and the task control block resources are reclaimed.</p> 110</td> 111</tr> 112<tr id="row69281012114217"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p68857132427"><a name="p68857132427"></a><a name="p68857132427"></a>LOS_TaskDetach</p> 113</td> 114<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p188851813184219"><a name="p188851813184219"></a><a name="p188851813184219"></a>Changes the task attribute from <strong id="b1986175411125"><a name="b1986175411125"></a><a name="b1986175411125"></a>joinable</strong> to <strong id="b18996959101215"><a name="b18996959101215"></a><a name="b18996959101215"></a>detach</strong>. After the task of the <strong id="b15851429171317"><a name="b15851429171317"></a><a name="b15851429171317"></a>detach</strong> attribute is complete, the task control block resources will be automatically reclaimed.</p> 115</td> 116</tr> 117<tr id="row1231672512301"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p92979251305"><a name="p92979251305"></a><a name="p92979251305"></a>LOS_TaskDelay</p> 118</td> 119<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p1297182517306"><a name="p1297182517306"></a><a name="p1297182517306"></a>Delays a task.</p> 120</td> 121</tr> 122<tr id="row73161725163018"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p10297152573014"><a name="p10297152573014"></a><a name="p10297152573014"></a>LOS_TaskYield</p> 123</td> 124<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p2297325163019"><a name="p2297325163019"></a><a name="p2297325163019"></a>Adjusts the scheduling sequence of tasks that call the task priority.</p> 125</td> 126</tr> 127<tr id="row1231520259306"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p9297142511307"><a name="p9297142511307"></a><a name="p9297142511307"></a>Task scheduling control</p> 128</td> 129<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p429719257307"><a name="p429719257307"></a><a name="p429719257307"></a>LOS_TaskLock</p> 130</td> 131<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p11297142510306"><a name="p11297142510306"></a><a name="p11297142510306"></a>Locks task scheduling.</p> 132</td> 133</tr> 134<tr id="row431442573010"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p17297625193015"><a name="p17297625193015"></a><a name="p17297625193015"></a>LOS_TaskUnlock</p> 135</td> 136<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p829715252308"><a name="p829715252308"></a><a name="p829715252308"></a>Unlocks task scheduling.</p> 137</td> 138</tr> 139<tr id="row53141525123010"><td class="cellrowborder" rowspan="3" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p1729732593014"><a name="p1729732593014"></a><a name="p1729732593014"></a>Task priority control</p> 140</td> 141<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p629715254308"><a name="p629715254308"></a><a name="p629715254308"></a>LOS_CurTaskPriSet</p> 142</td> 143<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p142981925183020"><a name="p142981925183020"></a><a name="p142981925183020"></a>Sets the priority for the current task.</p> 144</td> 145</tr> 146<tr id="row18314102563015"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p11298202513303"><a name="p11298202513303"></a><a name="p11298202513303"></a>LOS_TaskPriSet</p> 147</td> 148<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p629822563014"><a name="p629822563014"></a><a name="p629822563014"></a>Sets the priority for a specified task.</p> 149</td> 150</tr> 151<tr id="row731442543011"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p1029892517302"><a name="p1029892517302"></a><a name="p1029892517302"></a>LOS_TaskPriGet</p> 152</td> 153<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p19298182563018"><a name="p19298182563018"></a><a name="p19298182563018"></a>Obtains the priority of a specified task.</p> 154</td> 155</tr> 156<tr id="row18314172523011"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p17298225203011"><a name="p17298225203011"></a><a name="p17298225203011"></a>Obtaining task information</p> 157</td> 158<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p229811251307"><a name="p229811251307"></a><a name="p229811251307"></a>LOS_CurTaskIDGet</p> 159</td> 160<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p1029842519303"><a name="p1029842519303"></a><a name="p1029842519303"></a>Obtains the ID of the current task.</p> 161</td> 162</tr> 163<tr id="row1331452516307"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p172984256301"><a name="p172984256301"></a><a name="p172984256301"></a>LOS_TaskInfoGet</p> 164</td> 165<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p122988259303"><a name="p122988259303"></a><a name="p122988259303"></a>Obtains information about the specified task.</p> 166</td> 167</tr> 168<tr id="row93146250305"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p82986256300"><a name="p82986256300"></a><a name="p82986256300"></a>Binding tasks to CPU cores</p> 169</td> 170<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p1929862583018"><a name="p1929862583018"></a><a name="p1929862583018"></a>LOS_TaskCpuAffiSet</p> 171</td> 172<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p82981925163013"><a name="p82981925163013"></a><a name="p82981925163013"></a>Binds a specified task to the specified CPU. It is used only in multi-core scenarios.</p> 173</td> 174</tr> 175<tr id="row5314192543014"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p02984251305"><a name="p02984251305"></a><a name="p02984251305"></a>LOS_TaskCpuAffiGet</p> 176</td> 177<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p1729822513017"><a name="p1729822513017"></a><a name="p1729822513017"></a>Obtains the core binding information of the specified task. It is used only in multi-core scenarios.</p> 178</td> 179</tr> 180<tr id="row43141525193013"><td class="cellrowborder" rowspan="2" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p16298152514308"><a name="p16298152514308"></a><a name="p16298152514308"></a>Task scheduling parameter control</p> 181</td> 182<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p1529872563011"><a name="p1529872563011"></a><a name="p1529872563011"></a>LOS_GetTaskScheduler</p> 183</td> 184<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p1298182513010"><a name="p1298182513010"></a><a name="p1298182513010"></a>Obtains the scheduling policy of the specified task.</p> 185</td> 186</tr> 187<tr id="row1331472519305"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p15298152514301"><a name="p15298152514301"></a><a name="p15298152514301"></a>LOS_SetTaskScheduler</p> 188</td> 189<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p11298172553015"><a name="p11298172553015"></a><a name="p11298172553015"></a>Sets the scheduling parameters, including the priority and scheduling policy, for the specified task.</p> 190</td> 191</tr> 192<tr id="row831492514307"><td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.1 "><p id="p12981250307"><a name="p12981250307"></a><a name="p12981250307"></a>Maximum number of tasks supported</p> 193</td> 194<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.2 "><p id="p112981425183019"><a name="p112981425183019"></a><a name="p112981425183019"></a>LOS_GetSystemTaskMaximum</p> 195</td> 196<td class="cellrowborder" valign="top" width="33.33333333333333%" headers="mcps1.1.4.1.3 "><p id="p6298525123017"><a name="p6298525123017"></a><a name="p6298525123017"></a>Obtains the maximum number of tasks supported by the system.</p> 197</td> 198</tr> 199</tbody> 200</table> 201 202### How to Develop<a name="section16229657115514"></a> 203 204The typical task development process is as follows: 205 2061. Call **LOS\_TaskCreate** to create a task. 207 - Specify the execution entry function for the task. 208 - Specifies the task name. 209 - Specify the task stack size. 210 - Specify the priority of the task. 211 - Specify the task attribute, which can be **LOS\_TASK\_ATTR\_JOINABLE** or **LOS\_TASK\_STATUS\_DETACHED**. 212 - Specify the task-core binding attribute for multi-core environment. 213 2142. Run the service code to implement task scheduling. 2153. Reclaim resources when the task is complete. If the task attribute is **LOS\_TASK\_STATUS\_DETACHED**, the task resources are automatically reclaimed. If the task attribute is **LOS\_TASK\_ATTR\_JOINABLE**, call **LOS\_TaskJoin** to reclaim task resources. The default task attribute is **LOS\_TASK\_STATUS\_DETACHED**. 216 217> **NOTE:** 218>- The kernel mode has the highest permission and can operate tasks in any process. 219>- If a task is created after a user-mode process enters the kernel mode by a system call, the task belongs to a KProcess not a user-mode process. 220 221### Development Example<a name="section2809723165612"></a> 222 223The sample code is as follows: 224 225``` 226UINT32 g_taskLoID; 227UINT32 g_taskHiID; 228#define TSK_PRIOR_HI 4 229#define TSK_PRIOR_LO 5 230UINT32 ExampleTaskHi(VOID) 231{ 232 UINT32 ret; 233 PRINTK("Enter TaskHi Handler.\n"); 234 /* Delay the task for 2 ticks. The task is then suspended, and the remaining task with the highest priority (g_taskLoID) will be executed.*/ 235 ret = LOS_TaskDelay(2); 236 if (ret != LOS_OK) { 237 PRINTK("Delay Task Failed.\n"); 238 return LOS_NOK; 239 } 240 /* After 2 ticks elapse, the task is resumed and executed.*/ 241 PRINTK("TaskHi LOS_TaskDelay Done.\n"); 242 /* Suspend the task.*/ 243 ret = LOS_TaskSuspend(g_taskHiID); 244 if (ret != LOS_OK) { 245 PRINTK("Suspend TaskHi Failed.\n"); 246 return LOS_NOK; 247 } 248 PRINTK("TaskHi LOS_TaskResume Success.\n"); 249 return LOS_OK; 250} 251 252/* Entry function of the lower-priority task */ 253UINT32 ExampleTaskLo(VOID) 254{ 255 UINT32 ret; 256 PRINTK("Enter TaskLo Handler.\n"); 257 /* Delay the task for 2 ticks. The task is then suspended, and the remaining task with the highest priority (background task) will be executed.*/ 258 ret = LOS_TaskDelay(2); 259 if (ret != LOS_OK) { 260 PRINTK("Delay TaskLo Failed.\n"); 261 return LOS_NOK; 262 } 263 PRINTK("TaskHi LOS_TaskSuspend Success.\n"); 264 /* Resume the suspended task g_taskHiID.*/ 265 ret = LOS_TaskResume(g_taskHiID); 266 if (ret != LOS_OK) { 267 PRINTK("Resume TaskHi Failed.\n"); 268 return LOS_NOK; 269 } 270 PRINTK("TaskHi LOS_TaskDelete Success.\n"); 271 return LOS_OK; 272} 273/* Task test entry function, which is used to create two tasks with different priorities.*/ 274UINT32 ExampleTaskCaseEntry(VOID) 275{ 276 UINT32 ret; 277 TSK_INIT_PARAM_S initParam = {0}; 278 279 /* Lock task scheduling.*/ 280 LOS_TaskLock(); 281 PRINTK("LOS_TaskLock() Success!\n"); 282 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ExampleTaskHi; 283 initParam.usTaskPrio = TSK_PRIOR_HI; 284 initParam.pcName = "HIGH_NAME"; 285 initParam.uwStackSize = LOS_TASK_MIN_STACK_SIZE; 286 initParam.uwResved = LOS_TASK_ATTR_JOINABLE; 287 288 /* Create a task with a higher priority. The task will not be executed immediately after being created, because task scheduling is locked.*/ 289 ret = LOS_TaskCreate(&g_taskHiID, &initParam); 290 if (ret != LOS_OK) { 291 LOS_TaskUnlock(); 292 PRINTK("ExampleTaskHi create Failed! ret=%d\n", ret); 293 return LOS_NOK; 294 } 295 PRINTK("ExampleTaskHi create Success!\n"); 296 297 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ExampleTaskLo; 298 initParam.usTaskPrio = TSK_PRIOR_LO; 299 initParam.pcName = "LOW_NAME"; 300 initParam.uwStackSize = LOS_TASK_MIN_STACK_SIZE; 301 initParam.uwResved = LOS_TASK_STATUS_DETACHED; 302 303 /* Create a task with a lower priority. The task will not be executed immediately after being created, because task scheduling is locked.*/ 304 ret = LOS_TaskCreate(&g_taskLoID, &initParam); 305 if (ret!= LOS_OK) { 306 LOS_TaskUnlock(); 307 PRINTK("ExampleTaskLo create Failed!\n"); 308 return LOS_NOK; 309 } 310 PRINTK("ExampleTaskLo create Success!\n"); 311 312 /* Unlock task scheduling. The task with the highest priority in the Ready queue will be executed.*/ 313 LOS_TaskUnlock(); 314 ret = LOS_TaskJoin(g_taskHiID, NULL); 315 if (ret != LOS_OK) { 316 PRINTK("Join ExampleTaskHi Failed!\n"); 317 } else { 318 PRINTK("Join ExampleTaskHi Success!\n"); 319 } 320 while(1){}; 321 return LOS_OK; 322} 323``` 324 325The development is successful if the return result is as follows: 326 327``` 328LOS_TaskLock() Success! 329ExampleTaskHi create Success! 330ExampleTaskLo create Success! 331Enter TaskHi Handler. 332Enter TaskLo Handler. 333TaskHi LOS_TaskDelay Done. 334TaskHi LOS_TaskSuspend Success. 335TaskHi LOS_TaskResume Success. 336TaskHi LOS_TaskDelete Success. 337Join ExampleTaskHi Success! 338``` 339 340