Kernel thread priority setting method is introduced

  • 2020-04-01 21:39:42
  • OfStack

Personally, I understand that kernel-level threads are the same as processes, and that the former are very different from POSIX threads (pthreads). Therefore, the kernel's process scheduling policies and system calls also apply to kernel-level threads.

There are three kinds of scheduling policies:

1. SCHED_NORMAL non-real-time scheduling policy, 100 ~ 139 by default, determined by nice;

2. SCHED_FIFO real-time scheduling policy, first come, first served. Once the CPU is used, it keeps running. Run until a higher priority task arrives or gives up

3. SCHED_RR real-time scheduling policy, time slice rotation. When the process runs out of time slices, the system redistributes the time slices and places them at the end of the ready queue. It can also be preempted by high priority.

The two real-time priorities range from 0 to max_rt_prio-1, with a default of 0 to 99.

Related system calls (from LKD, which may vary from kernel to kernel) :

The nice () Set the nice value for the process Sched_setscheduler () Sets the scheduling policy for the process The sched_getscheduler () Gets the scheduling policy for the process Sched_setparam () Set the real-time priority of the process Sched_getparam () Gets the real-time priority of the process Sched_get_priority_max () Gets the maximum real-time priority Sched_get_priority_min () Gets the minimum value of the real-time priority Sched_rr_get_interval ()
Gets the time slice value of the process Sched_setaffinity () Sets the affinity of the process's processor Sched_getaffinity () Gets the processor affinity of the process Sched_yield () Let go of the processor for a moment

You need to use the struct sched_param structure to set it up.

The following is part of the code I wrote in the kernel thread:


struct sched_param param;
    param.sched_priority = 99;
    sched_setscheduler(current, SCHED_FIFO, ¶m)//Returns -1 on error


Related articles: