Linux进程调度时机

我们之前有分析过,update_process_times会判断当前进程的时间片是否用完,如果用完了会设置TIF_NEED_RESCHED,在合适的时机,调用schedule函数做实际的进程调度。
进程调度的时机,主要分成以下四种:

  1. 直接调用schedule
  2. 间接调用schedule,如果msleep函数
  3. 中断返回时检测TIF_NEED_RESCHED,如果设置则调用schedule
  4. 系统调用返回用户空间的时候检测TIF_NEED_RESCHED,如果设置该标记,则调用schedule。
    直接调用schedule没什么可说的,下面主要分析后面四种调度时机的代码。

间接调用schedule


void msleep(unsigned int msecs)
{
   unsigned long timeout = msecs_to_jiffies(msecs) + 1;

   while (timeout)
   	timeout = schedule_timeout_uninterruptible(timeout);
}

首先计算超时时间,并调用 schedule_timeout_uninterruptible函数,该函数返回剩余的超时时间。

signed long __sched schedule_timeout_uninterruptible(signed long timeout)
{
	__set_current_state(TASK_UNINTERRUPTIBLE);
	return schedule_timeout(timeout);
}

首先设置当前进程的状态为TASK_UNINTERRUPTIBLE,然后调用 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
		 * 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值