内核进程的调度与进程切换

进程被创建到了链表中,如何再进行进一步的调用和调用?

进程调度

        void schedule(void);        进程调度

        switch_to(next);        进程切换函数

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 && (*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;
//找到当前counter最大值
			if ((*p)->state == TASK_RUNNING && (*p)->counter > c)
				c = (*p)->counter, next = i;
		}
		if (c) break;
//如果task链表中所有进程的时间片为0,则进行时间片重分配
		for(p = &LAST_TASK ; p > &FIRST_TASK ; --p)
			if (*p)
				(*p)->counter = ((*p)->counter >> 1) +
						(*p)->priority;  //counter = counter/2+pri
	}
	switch_to(next);
}

进程状态:

运行状态                           #define TASK_RUNNING        0  可以被运行,就绪状态,只有这种状态才能被运行

可中断睡眠状态                 #define TASK_INTERRUPTIBLE  1  可以被信号中断,使其变成RUNING

不可中断睡眠状态             #define TASK_UNINTERRUPTIBLE    2  只能被wakeup唤醒变成RUNNING

暂停状态                           #define TASK_STOPPED        4   被相关STOP信号暂停

僵死状态                          #define TASK_ZOMBIE     3   进程停止运行,但是没有被父进程进行回收

进程切换:

将进程切换为当前执行进程

1.将需要切换的进程赋值给当前执行进程

2.切换进程上下文(TSS等信息),堆栈信息

#define switch_to(n) {\
struct {long a,b;} __tmp; \
__asm__("cmpl %%ecx,_current\n\t" \  //比较一下是否为当前进程
	"je 1f\n\t" \
	"xchgl %%ecx,_current\n\t" \
	"movw %%dx,%1\n\t" \
	"ljmp %0\n\t" \
	"cmpl %%ecx,%2\n\t" \
	"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])); \
}

内核进程的调度与进程切换_第1张图片

进程睡眠函数:

void sleep_on(struct task_struct **p)
{
	struct task_struct *tmp;

	if (!p)
		return;
	if (current == &(init_task.task))
		panic("task[0] trying to sleep");
	tmp = *p;
	*p = current;
	current->state = TASK_UNINTERRUPTIBLE;
	schedule();
	if (tmp)
		tmp->state=0;
}

内核进程的调度与进程切换_第2张图片

你可能感兴趣的:(linux内核,linux,运维,服务器)