The Linux kernel’s scheduling mechanism supports various scheduling policies, categorized by priority from high to low: Deadline Scheduling (SCHED_DEADLINE): the highest priority, based on deadline scheduling, suitable for tasks with strict timing requirements. Real-time Scheduling includes SCHED_FIFO and SCHED_RR. SCHED_FIFO: first-in, first-out, with no time slice limit, until voluntarily relinquished or preempted by a higher-priority process. SCHED_RR: time-slice round-robin, with a time slice limit. Normal Scheduling includes SCHED_NORMAL and SCHED_BATCH. SCHED_NORMAL: the default policy, using the Completely Fair Scheduler (CFS) algorithm. SCHED_BATCH: optimized for CPU-intensive non-interactive tasks. Idle Scheduling (SCHED_IDLE): the lowest priority, used only when there are no other runnable processes. The lower the priority value, the higher the priority. Deadline Processes: priority is determined by the deadline. Real-time Processes: priority range 1-99, with higher values indicating higher priority. Normal Processes: priority range 100-139, with lower values indicating higher priority, adjustable via the nice value. CFS is the default scheduler for normal processes, ensuring fairness through virtual run time: each process’s virtual run time records its entitled CPU time, and CFS always selects the process with the smallest virtual run time to run, efficiently managing processes through a red-black tree, achieving O(log n) time complexity. The Linux kernel abstracts five scheduling classes, arranged by priority: Stop Scheduling Class (stop_sched_class): the highest priority, used for CPU hot-plugging, Deadline Scheduling Class (dl_sched_class), Real-time Scheduling Class (rt_sched_class), Fair Scheduling Class (fair_sched_class), and Idle Scheduling Class (idle_sched_class). The SCHED_DEADLINE scheduling policy in the Linux kernel is designed for real-time tasks with strict timing requirements, based on the Earliest Deadline First (EDF) algorithm, and implements bandwidth isolation between tasks through the Constant Bandwidth Server (CBS) mechanism. The core algorithm principle of SCHED_DEADLINE uses three key parameters to schedule tasks: Runtime: the execution time required by the task per cycle (in microseconds), Period: the length of the task’s cycle (in microseconds), Deadline: the time by which the runtime must be completed from the start of the cycle. The scheduling process: when a task is awakened, the scheduler uses the CBS algorithm to calculate the “scheduling deadline,” and then, based on these scheduling deadlines, uses the EDF algorithm to select the task with the earliest deadline for execution. Through appropriate “admission control” strategies, it ensures that tasks receive runtime before their deadlines. The scheduling class SCHED_DEADLINE belongs to the dl_sched_class scheduling class, which in the Linux kernel’s scheduling class hierarchy has a priority just below the stop scheduling class, higher than the real-time scheduling class and the completely fair scheduling class. The scheduling class is defined by the struct sched_class structure, which includes a series of function pointers, such as: enqueue_task: to add a task to the ready queue, dequeue_task: to remove a task from the queue, pick_next_task: to select the next task to run. The priority system in Linux’s priority hierarchy: Deadline Processes (SCHED_DEADLINE): the highest priority, scheduled based on deadlines, with priority determined by the deadline; the earlier the deadline, the higher the logical priority. To view the current scheduling policy and process information of the system, you can run the command: ps -eo pid,comm,cls,pri,rtprio,ni | grep -E “DL|DEADLINE”. Below is a C language example program using the SCHED_DEADLINE scheduling policy, demonstrating how to create a deadline scheduling task and set its parameters.
#define _GNU_SOURCE // Simulate CPU-intensive work volatile int counter = 0; for (int i = 0; i < 1000000; i++) { counter++; } iteration++; printf("Iteration %d: Executed time = %lld nanoseconds\n", iteration, execution_time); // Brief sleep to simulate I/O wait usleep(10000); // 10ms } printf("Deadline scheduling task completed execution\n");}int main() { printf("=== SCHED_DEADLINE Scheduling Demonstration Program ===\n\n"); // Display initial scheduling information printf("Initial scheduling status:\n"); display_scheduling_info(); printf("\n"); // Check if running with root privileges (SCHED_DEADLINE requires privileges) if (geteuid() != 0) { printf("Warning: Root privileges are required to set SCHED_DEADLINE scheduling policy\n"); printf("Please run this program with sudo\n\n"); } // Set SCHED_DEADLINE parameters // Parameter units: nanoseconds int runtime = 10000000; // 10ms runtime int period = 30000000; // 30ms period int deadline = 20000000; // 20ms deadline printf("Setting SCHED_DEADLINE scheduling parameters:\n"); printf(" Runtime: %d nanoseconds (%d ms)\n", runtime, runtime/1000000); printf(" Period: %d nanoseconds (%d ms)\n", period, period/1000000); printf(" Deadline: %d nanoseconds (%d ms)\n", deadline, deadline/1000000); printf("\n"); // Attempt to set deadline scheduling if (set_deadline_scheduling(runtime, period, deadline) == 0) { printf("Successfully set SCHED_DEADLINE scheduling policy!\n\n"); // Display updated scheduling information printf("Updated scheduling status:\n"); display_scheduling_info(); printf("\n"); // Execute deadline scheduling task deadline_task_work(); } else { printf("Failed to set SCHED_DEADLINE, will run demonstration task with normal scheduling policy\n\n"); deadline_task_work(); } printf("\nProgram execution completed\n"); return 0;