linux011中调度算法

来源:互联网 发布:电脑编程c语言的软件 编辑:程序博客网 时间:2024/06/05 04:34
/*调度函数*/void schedule(void){int i,next,c;struct task_struct ** p;/* check alarm, wake up any interruptible tasks that have got a signal *//* *这里检查每个任务的alarm值,如果已经过期,就设置SIGALRM信号,并设置alarm为0.唤醒任务为就 *绪状态*/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 && (*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;/**如果没有找到合适的任务,说明所有任务的时间片已经用完,这样重新设置所有任务的时间片*/for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)if (*p)(*p)->counter = ((*p)->counter >> 1) +(*p)->priority;}switch_to(next);}#define switch_to(n) {\               // struct {long a,b;} __tmp; \           //定义一个临时结构体,tmp.a是一个32位偏移值b是tss选择符 __asm__("cmpl %%ecx,_current\n\t" \   //比较是不是当前结构体"je 1f\n\t" \      // 是,就什么都不做"xchgl %%ecx,_current\n\t" \  //不是就调换,current<-task[nr],ecx<-current"movw %%dx,%1\n\t" \      //TSS[N]->tmp.b "ljmp %0\n\t" \      //调转到新任务"cmpl %%ecx,%2\n\t" \      //判断任务上次有没有使用math"jne 1f\n\t" \      // 没有什么都不做"clts\n" \      //使用,就清除标志位"1:" \      // ::"m" (*&__tmp.a),"m" (*&__tmp.b), \"m" (last_task_used_math),"d" _TSS(n),"c" ((long) task[n])); \}

原创粉丝点击