1# Scheduler 2 3 4## Basic Concepts 5 6The OpenHarmony LiteOS-A kernel uses the preemptive scheduling mechanism for tasks. The tasks with a higher priority are scheduled first, and the tasks with the same priority are scheduled using the time slice polling. The system runs based on the real-time timeline from the startup, which ensures good real-time performance of the scheduling algorithm. 7 8The OpenHarmony scheduling algorithm is embedded with the tickless mechanism, which ensures lower power consumption and on-demand response to tick interrupts. This minimizes useless tick interrupt response time and further improves the real-time performance of the system. 9 10OpenHarmony supports **SCHED_RR** (time slice round robin) for process scheduling and **SCHED_RR** and **SCHED_FIFO** (first in, first out) for thread scheduling . 11 12Threads are the minimum scheduling units in OpenHarmony. 13 14 15## Working Principles 16 17OpenHarmony uses process priority queue and thread priority queue for scheduling. The process priority ranges from 0 to 31, and there are 32 process priority bucket queues. Each bucket queue corresponds to a thread priority bucket queue. The thread priority ranges from 0 to 31, and a thread priority bucket queue also has 32 priority queues. 18 19**Figure 1** Scheduling priority bucket queue 20 21![](figures/scheduling-priority-bucket-queue.png "scheduling-priority-bucket-queue") 22 23The OpenHarmony system starts scheduling after the kernel initialization is complete. The processes or threads created during running are added to the scheduling queues. The system selects the optimal thread for scheduling based on the priorities of the processes and threads and the time slice consumption of the threads. Once a thread is scheduled, it is deleted from the scheduling queue. If a thread is blocked during running, the thread is added to the corresponding blocking queue and triggers scheduling of another thread. If no thread in the scheduling queue can be scheduled, the system selects the thread of the KIdle process for scheduling. 24 25**Figure 2** Scheduling process 26 27![](figures/scheduling-process.png "scheduling-process") 28 29 30## Development Guidelines 31 32 33### Available APIs 34 35| API| Description| 36| -------- | -------- | 37| LOS_Schedule | Triggers system scheduling.| 38| LOS_GetTaskScheduler | Obtains the scheduling policy of a task.| 39| LOS_SetTaskScheduler | Sets the scheduling policy for a task.| 40| LOS_GetProcessScheduler | Obtains the scheduling policy of a process.| 41| LOS_SetProcessScheduler | Sets scheduling parameters, including the priority and scheduling policy, for a process.| 42 43 44### How to Develop 45 46> **NOTE** 47> 48> Scheduling cannot be triggered during the system initialization process. 49