Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e1e13ed)
raw | patch | inline | side by side (parent: e1e13ed)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Sun, 7 Nov 2021 16:54:28 +0000 (00:54 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Mon, 8 Nov 2021 02:22:40 +0000 (10:22 +0800) |
| include/list.h | patch | blob | history | |
| include/sched.h | patch | blob | history | |
| include/task.h | patch | blob | history | |
| kernel/clock.c | patch | blob | history | |
| kernel/sched.c | patch | blob | history | |
| kernel/setup.c | patch | blob | history | |
| kernel/syscall.c | patch | blob | history |
diff --git a/include/list.h b/include/list.h
--- a/include/list.h
+++ b/include/list.h
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
--- a/include/sched.h
+++ b/include/sched.h
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
--- a/include/task.h
+++ b/include/task.h
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
--- a/kernel/clock.c
+++ b/kernel/clock.c
*/
#include <printk.h>
+#include <sched.h>
#include <system.h>
static unsigned int jiffies = 0;
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
--- a/kernel/sched.c
+++ b/kernel/sched.c
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;
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; i<NR_OPENS; i++)
kmem_cache_t *task_union_cache;
void setup_tasks() {
+ INIT_LIST_HEAD(&all_tasks);
+ INIT_LIST_HEAD(&pend_tasks);
+
init_root_tsk();
task_union_cache = kmem_cache_create("task_union", sizeof(task_union), PAGE_SIZE);
irq_save(iflags);
list_for_each_safe(pos, tmp, &all_tasks) {
p = list_entry(pos, task_union, list);
- if (p->pid == pid) break;
+ if (p->pid == pid) {
+ break;
+ }
}
irq_restore(iflags);
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;
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;
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
--- a/kernel/setup.c
+++ b/kernel/setup.c
#include <io.h>
#include <printk.h>
#include <system.h>
+#include <tty.h>
extern void init_mm();
extern void setup_gdt();
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
--- a/kernel/syscall.c
+++ b/kernel/syscall.c
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);