From: acevest Date: Mon, 8 Nov 2021 05:46:33 +0000 (+0800) Subject: 修复让任务进入WAIT状态不断把TCB加入delay_tasks链表的问题 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=4a4588a18f92b1bee47462020b49cf4c403c005e;p=kernel.git 修复让任务进入WAIT状态不断把TCB加入delay_tasks链表的问题 --- diff --git a/include/sched.h b/include/sched.h index 7b7eb7d..c2abf48 100644 --- a/include/sched.h +++ b/include/sched.h @@ -31,4 +31,4 @@ extern task_union root_task; extern void load_cr3(task_union *tsk); extern list_head_t all_tasks; -extern list_head_t pend_tasks; \ No newline at end of file +extern list_head_t delay_tasks; \ No newline at end of file diff --git a/kernel/clock.c b/kernel/clock.c index 7639f13..a682214 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -21,16 +21,16 @@ unsigned int sys_clock() { return jiffies; } void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { jiffies++; - task_union *p = 0, *p1 = 0; - list_head_t *pos = 0, *t = 0; + if (jiffies % 100 == 0) { + printl(MPL_CLOCK, "clock irq: %d", jiffies); + } unsigned long iflags; irq_save(iflags); - int cnt = 0; - - list_for_each_entry_safe(p, p1, &pend_tasks, pend) { - // printk("cnt %d %d\n", cnt++, p->delay_cnt); + 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) { @@ -40,7 +40,4 @@ void clk_handler(unsigned int irq, pt_regs_t *regs, void *dev_id) { } irq_restore(iflags); - - // printd("^"); - // printl(MPL_CLOCK, "clock irq: %d", jiffies); } diff --git a/kernel/sched.c b/kernel/sched.c index 37b8529..f3ce8e3 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -47,7 +47,7 @@ extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAG // list_head_t delay_tasks; LIST_HEAD(all_tasks); -LIST_HEAD(pend_tasks); +LIST_HEAD(delay_tasks); void init_root_tsk() { int i; @@ -81,7 +81,7 @@ kmem_cache_t *task_union_cache; void setup_tasks() { INIT_LIST_HEAD(&all_tasks); - INIT_LIST_HEAD(&pend_tasks); + INIT_LIST_HEAD(&delay_tasks); init_root_tsk(); @@ -196,26 +196,20 @@ unsigned long schedule() { } } irq_restore(iflags); - + sel->sched_cnt++; sel->weight++; - printk("%08x %s weight %d state: %s\n", sel, sel->name, sel->weight, task_state(sel->state)); - - 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", sel == p ? ">" : " ", p->name, - p->pid, p, task_state(p->state), p->weight, p->sched_cnt); - if (sel->state == TASK_WAIT) { - asm volatile("xchg %bx, %bx"); - } - } - + // printk("%08x %s weight %d state: %s\n", sel, sel->name, sel->weight, task_state(sel->state)); task_union *prev = current; task_union *next = sel; if (prev != next) { // printk("switch to: %s:%d\n", next->name, next->pid); - sel->sched_cnt++; + list_for_each_safe(pos, t, &all_tasks) { + p = list_entry(pos, task_union, list); + printl(MPL_TASK_0 + p->pid * 2, " "); //清掉上一次显示的 '>' + printl(MPL_TASK_0 + p->pid * 2, "%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); + } context_switch(prev, next); } } diff --git a/kernel/setup.c b/kernel/setup.c index 32cc846..77d8a72 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -37,7 +37,7 @@ extern void reboot(); extern void cnsl_init(); extern void init_ttys(); -#define HZ 10 +#define HZ 100 #define CLOCK_TICK_RATE 1193180 #define LATCH ((CLOCK_TICK_RATE + HZ / 2) / HZ) @@ -83,7 +83,11 @@ void setup_kernel() { setup_irqs(); extern tty_t monitor_tty; - // tty_switch(&monitor_tty); + tty_switch(&monitor_tty); + // asm("sti"); + // while (1) { /* code */ + // } + return; // switch_printk_screen(); diff --git a/kernel/syscall.c b/kernel/syscall.c index b133db4..40a6e56 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -45,19 +45,29 @@ int sysc_pause(unsigned long tick) { return 0; } int sysc_test() { static unsigned int cnt = 0; - current->delay_cnt = root_task.sched_cnt % 40; + { // 这一段仅是测试代码 - unsigned long iflags; - irq_save(iflags); + // 因为现在sysc还没切进程 + // 所以current把自己加到delay_tasks后不会被立即换出 + // 下一次系统调用还可能走到这里 + // 所以这里就直接判断不是RUNNING就返回 + // 不再操作delay_tasks链表 + if (current->state != TASK_RUNNING) { + return 0; + } - current->state = TASK_WAIT; - // 现在sysc还没有实现进程切换 - // 这个要到下一次中断之后才生效 - list_add(&(current->pend), &pend_tasks); + current->delay_cnt = root_task.sched_cnt % 40; - irq_restore(iflags); + unsigned long iflags; + irq_save(iflags); - schedule(); + current->state = TASK_WAIT; + // 现在sysc还没有实现进程切换 + // 这个要到下一次中断之后才生效 + list_add(&(current->pend), &delay_tasks); + + irq_restore(iflags); + } // printl(MPL_TEST, "systest cnt %u current %08x cnt %u ", // cnt++, current, cnt);