linux0.11进程调度分析

来源:互联网 发布:西咸新区大数据交易所 编辑:程序博客网 时间:2024/04/30 13:06

10ms时钟中断 --> 时钟中断函数timer_interrupt,将jiffier加1 --> 调用do_timer函数,将当前进程counter计数减1,如果--counter大于0,则返回继续执行该任务,否则 --> 调用schedule函数,代码如下:

void schedule(void){int i,next,c;struct task_struct ** p;/* check alarm, wake up any interruptible tasks that have got a signal */for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)if (*p) {if ((*p)->alarm && (*p)->alarm < jiffies) {(*p)->signal |= (1<<(SIGALRM-1));(*p)->alarm = 0;}if (((*p)->signal & ~(_BLOCKABLE & (*p)->blocked)) &&(*p)->state==TASK_INTERRUPTIBLE)(*p)->state=TASK_RUNNING;}/* this is the scheduler proper: */while (1) {c = -1;next = 0;i = NR_TASKS;p = &task[NR_TASKS];while (--i) {if (!*--p)continue;if ((*p)->state == TASK_RUNNING && (*p)->counter > c)c = (*p)->counter, next = i;}if (c) break;    // 如果找到最大counter不为0的任务,则跳出while(1),切换到next任务,或者没有找到state==0的任务,则也跳出while(1),切换到任务0。for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)if (*p)(*p)->counter = ((*p)->counter >> 1) +(*p)->priority;}switch_to(next);}

注意:任务0并没有在任务结构指针数组中,FIRST_TASK 指向的是init_task。

0 0