天天看點

linux0.11程序排程分析

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。

繼續閱讀