From: acevest Date: Sat, 20 May 2023 16:44:37 +0000 (+0800) Subject: 重写delay系统调用;添加测试任务 X-Git-Url: http://zhaoyanbai.com/repos//%22http:/%22%29?a=commitdiff_plain;h=46fde345d5677994f028aeca8f208b1b48150e8c;p=kernel.git 重写delay系统调用;添加测试任务 --- diff --git a/include/sched.h b/include/sched.h index 6be6203..c0197dc 100644 --- a/include/sched.h +++ b/include/sched.h @@ -32,7 +32,6 @@ extern task_union root_task; extern void load_cr3(task_union *tsk); extern list_head_t all_tasks; -extern list_head_t delay_tasks; #define set_current_state(st) \ do { \ diff --git a/kernel/clock.c b/kernel/clock.c index bca611d..a759d3a 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -13,8 +13,9 @@ #include #include #include +#include -static unsigned int jiffies = 0; +volatile unsigned int jiffies = 0; unsigned int sys_clock() { return jiffies; } @@ -25,19 +26,19 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { printl(MPL_CLOCK, "clock irq: %d", jiffies); } - // unsigned long iflags; - // irq_save(iflags); + unsigned long iflags; + irq_save(iflags); task_union *p = 0; - task_union *t = 0; - list_for_each_entry_safe(p, t, &delay_tasks, pend) { - p->delay_cnt -= p->delay_cnt == 0 ? 0 : 1; - - if (0 == p->delay_cnt) { + list_head_t *t = 0; + list_head_t *pos = 0; + list_for_each_safe(pos, t, &all_tasks) { + p = list_entry(pos, task_union, list); + if (0 != p->delay_cnt && jiffies > p->delay_cnt && p->state == TASK_WAIT) { + p->delay_cnt = 0; p->state = TASK_READY; - list_del(&p->pend); } } - // irq_restore(iflags); + irq_restore(iflags); } diff --git a/kernel/sched.c b/kernel/sched.c index 54c267e..9a7e214 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -168,14 +168,36 @@ unsigned long schedule() { // printl(MPL_ROOT, "root:%d [%08x] cnt %u", root_task.pid, &root_task, root_task.cnt); #if 1 + bool need_reset_weight = true; list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_union, list); if (p->state != TASK_READY) { continue; } - if (p->weight >= p->priority) { + if (p->weight < p->priority) { + need_reset_weight = false; + break; + } + } + + if (need_reset_weight) { + list_for_each_safe(pos, t, &all_tasks) { + p = list_entry(pos, task_union, list); + if (p->state != TASK_READY) { + continue; + } p->weight = 0; + } + } + + list_for_each_safe(pos, t, &all_tasks) { + p = list_entry(pos, task_union, list); + if (p->state != TASK_READY) { + continue; + } + + if (p->weight > p->priority) { continue; } @@ -236,8 +258,8 @@ unsigned long schedule() { list_for_each_safe(pos, t, &all_tasks) { p = list_entry(pos, task_union, list); printl(MPL_TASK_0 + p->pid, " "); // 清掉上一次显示的 '>' - printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d sched %u", next == p ? ">" : " ", p->name, - p->pid, p, task_state(p->state), p->weight, p->sched_cnt); + printl(MPL_TASK_0 + p->pid, "%s%4s:%d [%08x] state %s weight %03d %03d sched %u", next == p ? ">" : " ", + p->name, p->pid, p, task_state(p->state), p->weight, p->priority, p->sched_cnt); } context_switch(prev, next); } diff --git a/kernel/syscall.c b/kernel/syscall.c index c837488..7749781 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -44,6 +44,7 @@ int sysc_none() { int sysc_pause(unsigned long tick) { return 0; } int sysc_test() { +#if 0 static unsigned int cnt = 0; { // 这一段仅是测试代码 @@ -74,6 +75,7 @@ int sysc_test() { // cnt++, current, cnt); // printk("systest cnt %u current %08x cnt %u\n",cnt++, current, cnt); return 0; +#endif } int sysc_debug(unsigned int v) { diff --git a/kernel/task_root.c b/kernel/task_root.c index 62736a1..9674138 100644 --- a/kernel/task_root.c +++ b/kernel/task_root.c @@ -47,6 +47,42 @@ void kernel_task(char *name, void *entry) { printk("kernel[%s] task pid is %d\n", name, pid); } +void taskA_entry() { + current->priority = 99; + + while (1) { + sysc_wait(600); + + for (int i = 0; i < 200; i++) { + asm("hlt;"); + } + } +} + +void taskB_entry() { + current->priority = 99; + + while (1) { + sysc_wait(200); + + for (int i = 0; i < 100; i++) { + asm("hlt;"); + } + } +} + +void taskC_entry() { + current->priority = 99; + + while (1) { + sysc_wait(2); + + for (int i = 0; i < 2; i++) { + asm("hlt;"); + } + } +} + // 从multiboot.S进入这里 void root_task_entry() { sti(); @@ -65,6 +101,10 @@ void root_task_entry() { kernel_task("disk", disk_task_entry); kernel_task("user", user_task_entry); + kernel_task("tskA", taskA_entry); + kernel_task("tskB", taskB_entry); + kernel_task("tskC", taskC_entry); + while (1) { asm("hlt;"); } diff --git a/kernel/wait.c b/kernel/wait.c index a89f2f2..e3d2df5 100644 --- a/kernel/wait.c +++ b/kernel/wait.c @@ -121,12 +121,30 @@ int debug_wait_queue_put(unsigned int v) { wake_up(&debug_wq); } +DECLARE_WAIT_QUEUE_HEAD(sysc_wait_queue_head); + +#if 1 +extern unsigned int jiffies; +int sysc_wait(unsigned long cnt) { + unsigned long flags; + irq_save(flags); + current->state = TASK_WAIT; + current->delay_cnt = jiffies + cnt; + irq_restore(flags); + + schedule(); +} +#else int sysc_wait(unsigned long pid) { task_union *p = find_task(pid); - if (p == 0) return 0; + if (p == 0) { + return 0; + } - if (p->state == TASK_EXITING) return 0; + if (p->state == TASK_EXITING) { + return 0; + } task_union *task = current; DECLARE_WAIT_QUEUE(wait, task); @@ -151,3 +169,4 @@ int sysc_wait(unsigned long pid) { return 0; } +#endif