关于schedule_timeout

schedule_timeout这个函数除了对当前进程调用schedule之外,还有一个功能,如同其名字中暗示的,在指定的时间到期后(timeout了)将进程唤醒。我们知道,进程一旦进入睡眠状态,就会从cpu的run queue中移走,直觉是系统将不会维护散落到系统各处(等待队列等)的这些睡眠进程的时间信息,那么如何在指定的时间到期时唤醒这些进程呢?Linux内核使用了timer机制来完成,timer不依赖于进程,依赖于处理器的中断,当然关于timer的内部实现的机制可以成为另一个帖子了,这里就不多说。看看schedule_timeout的源码:

signed long __sched schedule_timeout(signed long timeout)
{
	struct timer_list timer;
	unsigned long expire;

	switch (timeout)
	{
	case MAX_SCHEDULE_TIMEOUT:
		/*
		 * These two special cases are useful to be comfortable
		 * in the caller. Nothing more. We could take
		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
		 * but I' d like to return a valid offset (>=0) to allow
		 * the caller to do everything it want with the retval.
		 */
		schedule();
		goto out;
	default:
		/*
		 * Another bit of PARANOID. Note that the retval will be
		 * 0 since no piece of kernel is supposed to do a check
		 * for a negative retval of schedule_timeout() (since it
		 * should never happens anyway). You just have the printk()
		 * that will tell you if something is gone wrong and where.
		 */
		if (timeout < 0) {
			printk(KERN_ERR "schedule_timeout: wrong timeout "
				"value %lx\n", timeout);
			dump_stack();
			current->state = TASK_RUNNING;
			goto out;
		}
	}

	expire = timeout + jiffies;

	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
	__mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
	schedule();
	del_singleshot_timer_sync(&timer);

	/* Remove the timer from the object tracker */
	destroy_timer_on_stack(&timer);

	timeout = expire - jiffies;

 out:
	return timeout < 0 ? 0 : timeout;
}
EXPORT_SYMBOL(schedule_timeout);

我们关注的主要是default那块,因为前一个case指定的时间太漫长,不知道猴年马月才到期呢吧?!defalt case其实蛮简单,expire = timeout + jiffies;
用当前jiffies加上函数调用时的timeout值构成到期时间点,然后setup_timer_on_stack创建了一个timer,这个timer到期后将调用process_timeout,传入的参数是当前进程的指针current,然后调用schedule,函数将停留在schedule上(当前进程被调度出处理器,同时从cpu的run queue中移走,一个新的进程被调度运行起来)。但是不管进程怎么切换,游离于进程世界之外的timer在每次时钟中断都有相应,当这个timer的时间到期后,将调用timer上的回调函数。

schedule_timeout为timer安装的回调是process_timeout,核心代码就是wake_up(current)类似的样子,这样因为schedule_timeout而睡眠的进程将进入到cpu的run queue,当它被调度时,schedule函数返回,除了删除已经不再使用的timer之外,一个比较重要的步骤是timeout = expire - jiffies,这个主要是用来判断进程被唤醒的原因,因为有可能在timer没到期时,其他进程唤醒了该进程,这种情况下timeout = expire - jiffies算出的timeout是>0的,所以反映到函数的返回值上就是,0意味着进程因为时间timeout而被唤醒,一个正数意味着进程在时间尚没有timeout的情况下被唤醒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值