免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2485 | 回复: 4
打印 上一主题 下一主题

[进程管理] 进程睡眠时其分配到的时间片剩余时钟节拍数会减少吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-10 16:13 |只看该作者 |倒序浏览
进程睡眠时其分配到的时间片剩余时钟节拍数会减少吗?

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
2 [报告]
发表于 2013-01-10 17:03 |只看该作者
回复 1# Mr__key
这个应该不会吧。

看过原理,没有看过具体的这块代码,抽时间看看。

   

论坛徽章:
0
3 [报告]
发表于 2013-01-10 17:37 |只看该作者
回复 2# 瀚海书香
进程睡眠后相当于自动放弃了cpu资源,再接下来的进程管理调度会根据该进程其优先级及平均睡眠时间重新分配其时间片。

   

论坛徽章:
6
金牛座
日期:2013-10-08 10:19:10技术图书徽章
日期:2013-10-14 16:24:09CU十二周年纪念徽章
日期:2013-10-24 15:41:34狮子座
日期:2013-11-24 19:26:19未羊
日期:2014-01-23 15:50:002015年亚洲杯之阿联酋
日期:2015-05-09 14:36:15
4 [报告]
发表于 2013-01-10 19:26 |只看该作者
回复 3# Mr__key
进程睡眠后相当于自动放弃了cpu资源,再接下来的进程管理调度会根据该进程其优先级及平均睡眠时间重新分配其时间片。

不知道你看的那个版本的代码???

刚才看了一下2.6.32的内核代码:
如果进程在还有时间片的情况下调用sleep,那么对应的在内核通过alarm系统调用,将当前进程放到等待队列上,调用schedule()。
当睡眠时间到了以后,timer触发default_wake_function,最后对应的函数是try_to_wake_up()。
try_to_wake_up函数
  1. /***
  2. * try_to_wake_up - wake up a thread
  3. * @p: the to-be-woken-up thread
  4. * @state: the mask of task states that can be woken
  5. * @sync: do a synchronous wakeup?
  6. *
  7. * Put it on the run-queue if it's not already there. The "current"
  8. * thread is always on the run-queue (except when the actual
  9. * re-schedule is in progress), and as such you're allowed to do
  10. * the simpler "current->state = TASK_RUNNING" to mark yourself
  11. * runnable without the overhead of this.
  12. *
  13. * returns failure only if the task is already active.
  14. */
  15. static int try_to_wake_up(struct task_struct *p, unsigned int state,
  16.                           int wake_flags)
  17. {
  18.         int cpu, orig_cpu, this_cpu, success = 0;
  19.         unsigned long flags;
  20.         struct rq *rq, *orig_rq;

  21.         if (!sched_feat(SYNC_WAKEUPS))
  22.                 wake_flags &= ~WF_SYNC;

  23.         this_cpu = get_cpu();

  24.         smp_wmb();
  25.         rq = orig_rq = task_rq_lock(p, &flags);
  26.         update_rq_clock(rq);
  27.         if (!(p->state & state))
  28.                 goto out;

  29.         if (p->se.on_rq)
  30.                 goto out_running;

  31.         cpu = task_cpu(p);
  32.         orig_cpu = cpu;

  33. #ifdef CONFIG_SMP
  34.         if (unlikely(task_running(rq, p)))
  35.                 goto out_activate;

  36.         /*
  37.          * In order to handle concurrent wakeups and release the rq->lock
  38.          * we put the task in TASK_WAKING state.
  39.          *
  40.          * First fix up the nr_uninterruptible count:
  41.          */
  42.         if (task_contributes_to_load(p))
  43.                 rq->nr_uninterruptible--;
  44.         p->state = TASK_WAKING;
  45.         task_rq_unlock(rq, &flags);

  46.         cpu = p->sched_class->select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
  47.         if (cpu != orig_cpu)
  48.                 set_task_cpu(p, cpu);

  49.         rq = task_rq_lock(p, &flags);

  50.         if (rq != orig_rq)
  51.                 update_rq_clock(rq);

  52.         WARN_ON(p->state != TASK_WAKING);
  53.         cpu = task_cpu(p);

  54. #ifdef CONFIG_SCHEDSTATS
  55.         schedstat_inc(rq, ttwu_count);
  56.         if (cpu == this_cpu)
  57.                 schedstat_inc(rq, ttwu_local);
  58.         else {
  59.                 struct sched_domain *sd;
  60.                 for_each_domain(this_cpu, sd) {
  61.                         if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  62.                                 schedstat_inc(sd, ttwu_wake_remote);
  63.                                 break;
  64.                         }
  65.                 }
  66.         }
  67. #endif /* CONFIG_SCHEDSTATS */

  68. out_activate:
  69. #endif /* CONFIG_SMP */
  70.         schedstat_inc(p, se.nr_wakeups);
  71.         if (wake_flags & WF_SYNC)
  72.                 schedstat_inc(p, se.nr_wakeups_sync);
  73.         if (orig_cpu != cpu)
  74.                 schedstat_inc(p, se.nr_wakeups_migrate);
  75.         if (cpu == this_cpu)
  76.                 schedstat_inc(p, se.nr_wakeups_local);
  77.         else
  78.                 schedstat_inc(p, se.nr_wakeups_remote);
  79.         activate_task(rq, p, 1);
  80.         success = 1;

  81.         /*
  82.          * Only attribute actual wakeups done by this task.
  83.          */
  84.         if (!in_interrupt()) {
  85.                 struct sched_entity *se = &current->se;
  86.                 u64 sample = se->sum_exec_runtime;

  87.                 if (se->last_wakeup)
  88.                         sample -= se->last_wakeup;
  89.                 else
  90.                         sample -= se->start_runtime;
  91.                 update_avg(&se->avg_wakeup, sample);

  92.                 se->last_wakeup = se->sum_exec_runtime;
  93.         }

  94. out_running:
  95.         trace_sched_wakeup(rq, p, success);
  96.         check_preempt_curr(rq, p, wake_flags);

  97.         p->state = TASK_RUNNING;
  98. #ifdef CONFIG_SMP
  99.         if (p->sched_class->task_wake_up)
  100.                 p->sched_class->task_wake_up(rq, p);

  101.         if (unlikely(rq->idle_stamp)) {
  102.                 u64 delta = rq->clock - rq->idle_stamp;
  103.                 u64 max = 2*sysctl_sched_migration_cost;

  104.                 if (delta > max)
  105.                         rq->avg_idle = max;
  106.                 else
  107.                         update_avg(&rq->avg_idle, delta);
  108.                 rq->idle_stamp = 0;
  109.         }
  110. #endif
  111. out:
  112.         task_rq_unlock(rq, &flags);
  113.         put_cpu();

  114.         return success;
  115. }
复制代码
该函数将进程的状态设置为TASK_RUNNING,并将进程插入本地CPU队列,不会修改时间片,顶多会根据平均睡眠时间修改优先级。
   

论坛徽章:
0
5 [报告]
发表于 2013-01-11 09:35 |只看该作者
回复 4# 瀚海书香


    嗯,你说的不错,进程的时间片都只会在时间片用完后调用task_timeslice来重填其时间片,

睡眠进程唤醒后,会继续使用睡眠前剩余的时间片!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP