From: acevest Date: Sun, 7 Nov 2021 16:54:28 +0000 (+0800) Subject: 让user任务进入wait状态 X-Git-Url: http://zhaoyanbai.com/repos/FAQ?a=commitdiff_plain;h=fd38627d5c387621cc43260b133f5436267f88fe;p=kernel.git 让user任务进入wait状态 --- diff --git a/include/list.h b/include/list.h index 475e7f3..9ab99fb 100644 --- a/include/list.h +++ b/include/list.h @@ -64,7 +64,11 @@ static inline void _list_del(list_head_t *prev, list_head_t *next) { prev->next = next; } -static inline void list_del(list_head_t *entry) { _list_del(entry->prev, entry->next); } +static inline void list_del(list_head_t *entry) { + _list_del(entry->prev, entry->next); + entry->prev = NULL; + entry->next = NULL; +} static inline void list_del_init(list_head_t *entry) { _list_del(entry->prev, entry->next); diff --git a/include/sched.h b/include/sched.h index d1cb999..7b7eb7d 100644 --- a/include/sched.h +++ b/include/sched.h @@ -29,3 +29,6 @@ void wake_up(wait_queue_head_t *wqh); 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 diff --git a/include/task.h b/include/task.h index 38f9250..ff6559a 100644 --- a/include/task.h +++ b/include/task.h @@ -63,13 +63,15 @@ typedef union task_union { char name[TASK_NAME_SIZE]; - list_head_t list; + list_head_t list; // 所有进程串成一个链表 + + list_head_t pend; // 某些条件串成一个链表 wait_queue_head_t wait; unsigned int sched_cnt; - unsigned int cnt; // debug only + int delay_cnt; // debug only }; unsigned char stack[TASK_SIZE]; diff --git a/kernel/clock.c b/kernel/clock.c index 5dc04ca..7639f13 100644 --- a/kernel/clock.c +++ b/kernel/clock.c @@ -11,6 +11,7 @@ */ #include +#include #include static unsigned int jiffies = 0; @@ -20,6 +21,26 @@ 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; + + 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); + + p->delay_cnt -= p->delay_cnt == 0 ? 0 : 1; + + if (0 == p->delay_cnt) { + p->state = TASK_RUNNING; + list_del(&p->pend); + } + } + + irq_restore(iflags); + // printd("^"); // printl(MPL_CLOCK, "clock irq: %d", jiffies); } diff --git a/kernel/sched.c b/kernel/sched.c index 43e3432..37b8529 100644 --- a/kernel/sched.c +++ b/kernel/sched.c @@ -43,7 +43,11 @@ void load_cr3(task_union *tsk) { LoadCR3(tsk->cr3); } extern pde_t __initdata init_pgd[PDECNT_PER_PAGE] __attribute__((__aligned__(PAGE_SIZE))); -list_head_t all_tasks; +// list_head_t all_tasks; +// list_head_t delay_tasks; +LIST_HEAD(all_tasks); + +LIST_HEAD(pend_tasks); void init_root_tsk() { int i; @@ -56,8 +60,8 @@ void init_root_tsk() { root_task.priority = 100; strcpy(root_task.name, "root"); - INIT_LIST_HEAD(&all_tasks); list_add(&root_task.list, &all_tasks); + // INIT_LIST_HEAD(&root_task.next); // TODO // for(i=0; ipid == pid) break; + if (p->pid == pid) { + break; + } } irq_restore(iflags); @@ -180,8 +189,6 @@ unsigned long schedule() { continue; } - // printd("%08x %s weight %d\n", p, p->name, p->weight); - float ratio = (float)(p->weight * 1.0) / (p->priority * 1.0); if (ratio < min_ratio) { sel = p; @@ -191,7 +198,17 @@ unsigned long schedule() { irq_restore(iflags); sel->weight++; - printd("%08x %s:%d weight %d\n", sel, sel->name, sel->pid, 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"); + } + } task_union *prev = current; task_union *next = sel; @@ -199,11 +216,6 @@ unsigned long schedule() { 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, "%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); - } context_switch(prev, next); } } diff --git a/kernel/setup.c b/kernel/setup.c index 295ef64..32cc846 100644 --- a/kernel/setup.c +++ b/kernel/setup.c @@ -17,6 +17,7 @@ #include #include #include +#include extern void init_mm(); extern void setup_gdt(); @@ -81,7 +82,8 @@ void setup_kernel() { setup_tasks(); setup_irqs(); - printk("fuck\tfu\tsad\tasdfa\t\taa\n"); + extern tty_t monitor_tty; + // tty_switch(&monitor_tty); return; // switch_printk_screen(); diff --git a/kernel/syscall.c b/kernel/syscall.c index 70156d3..b133db4 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -44,7 +44,21 @@ int sysc_pause(unsigned long tick) { return 0; } int sysc_test() { static unsigned int cnt = 0; - current->cnt++; + + current->delay_cnt = root_task.sched_cnt % 40; + + unsigned long iflags; + irq_save(iflags); + + current->state = TASK_WAIT; + // 现在sysc还没有实现进程切换 + // 这个要到下一次中断之后才生效 + list_add(&(current->pend), &pend_tasks); + + irq_restore(iflags); + + schedule(); + // printl(MPL_TEST, "systest cnt %u current %08x cnt %u ", // cnt++, current, cnt); // printk("systest cnt %u current %08x cnt %u\n",cnt++, current, cnt);