Chinaunix

标题: 实时进程? [打印本页]

作者: _nosay    时间: 2016-03-24 18:09
标题: 实时进程?
本帖最后由 _nosay 于 2016-03-24 18:15 编辑

书上说,只要还有实时进程在执行,就轮不到非实时进程执行,所以我做了一个实验。

① 写一个程序,设置调度策略为SCHED_RR,代码如下:
  1. #include <stdio.h>
  2. #include <sched.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <string.h>

  7. int main()
  8. {
  9.         struct sched_param param;

  10.         param.sched_priority = sched_get_priority_max(SCHED_RR);
  11.         printf("priority: %d\n", param.sched_priority);

  12.         if (sched_setscheduler(getpid(), SCHED_RR, &param) < 0)
  13.         {
  14.                 printf("%s\n", strerror(errno));
  15.                 return -1;
  16.         }

  17.         printf("%d\n", sched_getscheduler(getpid()));

  18.         while (1) {
  19.                 // always TASK_RUNNING !!
  20.         }

  21.         return 0;
  22. }
复制代码
② 再写一个程序,不设置调度策略,即按默认策略SCHED_OTHER调度,代码如下:
  1. #include <stdio.h>
  2. #include <sched.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>

  5. int main()
  6. {
  7.         printf("%d\n", sched_getscheduler(getpid()));

  8.         return 0;
  9. }
复制代码
③ 编译程序①,用root身份执行(死循环),编译程序②,并执行。
   期望:程序②等待程序①被kill时,才有机会执行printf()。
   结果:不符合期望,程序②顺利执行完毕了。



请问如何理解“只要还有实时程序在执行,非实时程序就没有机会执行”?


http://www.cnblogs.com/zhaoyl/archive/2012/09/04/2671156.html
作者: _nosay    时间: 2016-03-25 09:18
本帖最后由 _nosay 于 2016-06-03 15:07 编辑

因为我实验的机器有4个核,调用sched_setaffinity()指定它们在同一个核执行,就达到期望了
  1.         cpu_set_t mask;

  2.         CPU_ZERO(&mask);
  3.         CPU_SET(1, &mask);

  4.         if (sched_setaffinity(getpid(), sizeof(mask), &mask) < 0)
  5.         {
  6.                 printf("%s\n", strerror(errno));
  7.                 return -1;
  8.         }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2