linux-0.11 schedule调度函数主要源码分析

schedule调度函数主要源码分析

// linux-0.11/kernel/sched.c
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;
		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
			if (*p)
			{
				(*p)->counter = ((*p)->counter >> 1) + (*p)->priority;
			}
	}
	switch_to(next);
}


    while (--i) {
		if (!*--p)				continue;
		if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
				c = (*p)->counter, next = i;
		}
        if (c) break;

这部分代码的目的是在所有就绪状态的任务进程中筛选出counter值最大的进程ID。之后如果counter值不为0则进入调度这个进程执行,如果counter值为0,则说明所有就绪状态的进程的时间片都已用完,需要重新调整所有进程的时间片。

for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
	if (*p)
	{
		(*p)->counter = ((*p)->counter >> 1) + (*p)->priority;
	}

这部分代码的目的是当所有就绪状态任务的时间片为0时,重新调整所有任务的时间片。
这里的时间片调整算法:c(n) = c(n-1)/2 + pro,例如:
c1 = pro; c2 = pro + pro/2; c3 = pro + pro/2 + pro/4;


  • 从上述算法可以发现这里的进程调度依靠的是时间片递减和优先级设定两个参数来决定执行下一个调度进程的。

猜你喜欢

转载自blog.csdn.net/m0_38099380/article/details/88375397