In this lesson, you will learn how the Linux kernel organizes its scheduling logic through "scheduler classes" and what is required to define a new one.
The Architecture of Scheduling
At the heart of the Linux kernel is the scheduler, which decides which process gets the CPU next. Rather than having one giant function that handles every possible task, Linux uses a modular approach called sched_class. Think of this as a tiered hierarchy: when the kernel needs to pick the next process, it asks each scheduler class in order of priority—from the highest-priority "Real-Time" classes down to the default "Completely Fair Scheduler" (CFS) and finally to the "Idle" class.
Requirements for a New Class
If you were to implement a custom scheduler class, you cannot simply write a loop; you must satisfy a strict "contract" with the kernel. You have to implement a set of function pointers defined in the sched_class structure. These include:
enqueue_task: How your class handles a process becoming ready to run.dequeue_task: How you remove a process if it blocks or finishes.pick_next_task: The core logic—this is where your algorithm decides which task in your class should execute next.task_tick: A function called periodically (on every timer interrupt) to allow your scheduler to update its statistics and perform preemption checks.
Here is a simplified look at how this structure is declared in the kernel:
struct sched_class {
const struct sched_class *next; // Link to the next lower-priority class
void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
struct task_struct * (*pick_next_task) (struct rq *rq);
void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
// ... many more callbacks
};
When you define your custom class, you register it in this linked list. If your scheduler is active, the kernel will query your pick_next_task function. If you return NULL (meaning you have no work), the kernel automatically falls through to the next class in the list, ensuring that the system continues to run smoothly even if your specific scheduler is idle.
Key Takeaway: A custom scheduler class is a plugin that implements a specific set of callback functions, allowing the kernel to offload the decision-making process for a specific group of tasks to your custom logic.