diff --git a/16_day/bootpack.c b/16_day/bootpack.c index 2d41a06..190a247 100644 --- a/16_day/bootpack.c +++ b/16_day/bootpack.c @@ -75,7 +75,7 @@ void HariMain(void) task_b[i]->tss.fs = 1 * 8; task_b[i]->tss.gs = 1 * 8; *((int *) (task_b[i]->tss.esp + 4)) = (int) sht_win_b[i]; - task_run(task_b[i]); + task_run(task_b[i], i + 1); } /* sht_win */ diff --git a/16_day/bootpack.h b/16_day/bootpack.h index 8a42a78..e518272 100644 --- a/16_day/bootpack.h +++ b/16_day/bootpack.h @@ -196,6 +196,7 @@ struct TSS32 { }; struct TASK { int sel, flags; /* sel用来存放GDT的编号*/ + int priority; /* 优先级 */ struct TSS32 tss; }; struct TASKCTL { @@ -207,6 +208,6 @@ struct TASKCTL { extern struct TIMER *task_timer; struct TASK *task_init(struct MEMMAN *memman); struct TASK *task_alloc(void); -void task_run(struct TASK *task); +void task_run(struct TASK *task, int priority); void task_switch(void); void task_sleep(struct TASK *task); diff --git a/16_day/fifo.c b/16_day/fifo.c index 12beeb5..0c51e4c 100644 --- a/16_day/fifo.c +++ b/16_day/fifo.c @@ -18,7 +18,7 @@ void fifo32_init(struct FIFO32 *fifo, int size, int *buf, struct TASK *task) } int fifo32_put(struct FIFO32 *fifo, int data) -/*给FIFO发送数据并储存在FIFO中*/ +/*向FIFO写入数据并累积起来*/ { if (fifo->free == 0) { /*没有空余空间,溢出*/ @@ -33,7 +33,7 @@ int fifo32_put(struct FIFO32 *fifo, int data) fifo->free--; if (fifo->task != 0) { if (fifo->task->flags != 2) { /*如果任务处于休眠状态*/ - task_run(fifo->task); /*将任务唤醒*/ + task_run(fifo->task, 0); /*将任务唤醒*/ } } return 0; diff --git a/16_day/mtask.c b/16_day/mtask.c index ac996b3..8fe0221 100644 --- a/16_day/mtask.c +++ b/16_day/mtask.c @@ -20,12 +20,13 @@ struct TASK *task_init(struct MEMMAN *memman) task = task_alloc(); task->flags = 2; /*活动中标志*/ + task->priority = 2; /* 0.02秒 */ taskctl->running = 1; taskctl->now = 0; taskctl->tasks[0] = task; load_tr(task->sel); task_timer = timer_alloc(); - timer_settime(task_timer, 2); + timer_settime(task_timer, task->priority); return task; } @@ -57,23 +58,29 @@ struct TASK *task_alloc(void) return 0; /*全部正在使用*/ } -void task_run(struct TASK *task) +void task_run(struct TASK *task, int priority) { - task->flags = 2; /*活动中标志*/ - taskctl->tasks[taskctl->running] = task; - taskctl->running++; + if (priority > 0) { + task->priority = priority; + } + if (task->flags != 2) { + task->flags = 2; /*活动中标志*/ + taskctl->tasks[taskctl->running] = task; + taskctl->running++; + } return; } -void task_switch(void) -{ - timer_settime(task_timer, 2); +void task_switch(void){ + struct TASK *task; + taskctl->now++; + if (taskctl->now == taskctl->running) { + taskctl->now = 0; + } + task = taskctl->tasks[taskctl->now]; + timer_settime(task_timer, task->priority); if (taskctl->running >= 2) { - taskctl->now++; - if (taskctl->now == taskctl->running) { - taskctl->now = 0; - } - farjmp(0, taskctl->tasks[taskctl->now]->sel); + farjmp(0, task->sel); } return; }