]> Zhao Yanbai Git Server - kernel.git/commitdiff
时间片轮转调度
authoracevest <zhaoyanbai@126.com>
Sun, 11 Jan 2026 02:01:38 +0000 (10:01 +0800)
committeracevest <zhaoyanbai@126.com>
Sun, 11 Jan 2026 02:01:38 +0000 (10:01 +0800)
include/task.h
kernel/ap.c
kernel/clock.c
kernel/fork.c
kernel/irq.c
kernel/sched.c

index 886346146e537a3048b7fd52e463cc0fe9b71508..729bcbcab28c1d600fcc0b9ec2aaa4e9c741c3db 100644 (file)
@@ -57,9 +57,9 @@ typedef union task_union {
         uint32_t eip;
 
         int ticks;
-        uint32_t turn;  // 时间片用完次数
-        uint32_t priority;
-        uint64_t jiffies;
+
+        int priority;
+        int64_t jiffies;
 
         volatile int need_resched;
 
index 617ba76db90b40c9468d8b4313d98e4155c9329a..f2a79aa4e1b7e9e892a8f60bb083848305d968f9 100644 (file)
@@ -306,7 +306,7 @@ const char* task_state(unsigned int state) {
 void print_all_tasks() {
     extern task_t* monitor_tasks[];
 
-    printl(MPL_TASK_TITLE, "         NAME      STATE TK/PI REASON     SCHED     KEEP      TURN");
+    printl(MPL_TASK_TITLE, "         NAME      STATE TK/PI REASON     SCHED     KEEP");
 
     for (int i = 0; i < 10; i++) {
         task_t* p = monitor_tasks[i];
@@ -315,7 +315,7 @@ void print_all_tasks() {
             continue;
         }
 
-        printl(MPL_TASK_0 + p->pid, "a%08x %-6s:%u %s %02u/%02u %-10s %-9u %-9u %-9u",
+        printl(MPL_TASK_0 + p->pid, "%08x %-6s:%u %s %02d/%02u %-10s %-9u %-9u",
                p,                     //
                p->name,               //
                p->pid,                //
@@ -324,8 +324,7 @@ void print_all_tasks() {
                p->priority,           //
                p->reason,             //
                p->sched_cnt,          //
-               p->sched_keep_cnt,     //
-               p->turn                //
+               p->sched_keep_cnt      //
         );
     }
 }
index 21fe47adcfd79cbd07f228e1a28a91740b526db6..9ed4e5fbac8cec53b57bbc8b9f539a9d50b65a3b 100644 (file)
@@ -31,17 +31,6 @@ void clk_handler(unsigned int irq, pt_regs_t* regs, void* dev_id) {
 
     current->jiffies = jiffies;
 
-    // 中断目前虽然不能嵌套,但依然可以打断前一个中断的下半部分处理
-    // 若前一个时钟中断将这个值减到0,会将该进程设置为need_resched,同时分配新的时间片值,以在下半部处理完后再重新调度就绪进程运行
-    // 而如果其下半部分需要处理的事情很多,处理时间过长,两个时钟中断之间的时间还不足以处理完
-    // 那么下一个时钟中断是完全可以打断还没处理完的下半部逻辑
-    // 打断后该时钟中断不应该继续减少该进程的时间片,因为这会造成该进程在后续的调底中少了实际的运行时间
-    if (1 == current->need_resched) {
-        // 这种情况必然已经发生了该时钟中断打断了下半部处理程序
-        // 反之时钟中断打断了下半部处理程序不一定need_resched就为1
-        return;
-    }
-
 #if ENABLE_CLOCK_IRQ_WAIT
     if (enable_clock_irq_delay) {
         return;
@@ -51,14 +40,6 @@ void clk_handler(unsigned int irq, pt_regs_t* regs, void* dev_id) {
 
     current->ticks--;
 
-    if (0 == current->ticks) {
-        current->need_resched = 1;
-        current->ticks = current->priority;
-        current->turn++;
-    }
-
-    assert(current->ticks >= 0);  // 防止ticks被减到0后再减溢出
-
     if (reenter == 0) {
         add_irq_bh_handler(clk_bh_handler, NULL);
     }
index b11182b31bdd721806f90f5b5a12096a7e68ad80..278031b84d0b2a7d4de40c0f7fe6398a84b4199b 100644 (file)
@@ -84,7 +84,7 @@ int do_fork(pt_regs_t* regs, unsigned long flags) {
     tsk->ppid = current->pid;
     tsk->priority = current->priority;
     tsk->ticks = tsk->priority;
-    tsk->turn = 0;  //
+
     tsk->need_resched = 0;
     tsk->sched_cnt = 0;
     tsk->sched_keep_cnt = 0;
index 3525f2ddddf8aee310350d7b0fd43b81e64cc9f3..95d27f241b965816861be97a8677ba87041b2666 100644 (file)
@@ -88,9 +88,9 @@ __attribute__((regparm(1))) void irq_handler(pt_regs_t* regs) {
     reenter--;
 
     // 考察如果不需要调度程序,直接退出
-    if (current->need_resched == 0) {
-        return;
-    }
+    // if (current->need_resched == 0) {
+    //     return;
+    // }
 
     // if (irq != 0) {
     //     return;
index 279ced23bd325eff01a51e0800d61faa23eb2e8b..648b8f3d303d34a41271c7b35f321d29f28a996e 100644 (file)
@@ -62,7 +62,6 @@ void init_root_task() {
     root_task.priority = 7;
     root_task.ticks = root_task.priority;
     root_task.vma_list = NULL;
-    root_task.turn = 0;
     root_task.need_resched = 0;
     root_task.sched_cnt = 0;
     root_task.sched_keep_cnt = 0;
@@ -150,12 +149,11 @@ task_t* find_task(pid_t pid) {
 }
 
 void schedule() {
-    task_t* root = &root_task;
+    // task_t* root = &root_task;
     task_t* sel = 0;
     task_t* p = 0;
     list_head_t *pos = 0, *t = 0;
 
-    assert(current->ticks >= 0);
     assert(current->priority <= TASK_MAX_PRIORITY);
 
     unsigned long iflags;
@@ -165,64 +163,40 @@ void schedule() {
         current->state = TASK_READY;
     }
 
-    list_for_each_safe(pos, t, &all_tasks) {
+    // task_t *head = current;
+    list_for_each_safe(pos, t, &current->list) {
         p = list_entry(pos, task_t, list);
-
-        if (p == &root_task) {
-            continue;
-        }
-
-        assert(p->state != TASK_RUN);
-
         if (TASK_READY != p->state) {
             continue;
         }
-
-        if (sel == 0) {
-            sel = p;
+        if (&root_task == p) {
             continue;
         }
 
-#if 1
-        if (p->priority > sel->priority) {
-            sel = p;
-        } else if (p->priority == sel->priority) {
-            int64_t a = p->jiffies + (p->priority - p->ticks);
-            int64_t b = sel->jiffies + (sel->priority - sel->ticks);
-            if (a < b) {
-                sel = p;
-            }
-        }
-#endif
-#if 0
-        // 考察三个量
-        // priority 越大越优先
-        // jiffies  越小越优先
-        // (priority - ticks) 表示已经使用的量,越小越优先
-        // 实际简化表达式为 ticks - jiffies(选择最大的)
-        // 先这样写,后续可以在各项添加系数
-        // 这个方法的缺点是对后面新加入的进程非常不友好
-        int64_t a = sel->priority - sel->jiffies - (sel->priority - sel->ticks);
-        int64_t b = p->priority - p->jiffies - (p->priority - p->ticks);
-        if (a < b) {
-            sel = p;
-        } else if (a == b) {
-            if (sel->priority < p->priority) {
-                sel = p;
-            }
+        sel = p;
+        break;
+    }
+
+    if (sel == 0) {
+        if (current->state != TASK_READY) {
+            sel = &root_task;
+            root_task.ticks = root_task.priority;
+            root_task.state = TASK_READY;
+        } else {
+            sel = current;
         }
-#endif
     }
 
     task_t* prev = current;
-    task_t* next = sel != 0 ? sel : root;
-
-    prev->need_resched = 0;
+    task_t* next = sel;
 
     next->state = TASK_RUN;
     next->reason = "";
 
     if (prev != next) {
+        if (prev->ticks <= 0) {
+            prev->ticks = prev->priority;
+        }
         next->sched_cnt++;
         context_switch(prev, next);
     } else {