【问题标题】:Correct usage of sched_setaffinity with two parameters使用两个参数正确使用 sched_setaffinity
【发布时间】:2014-12-03 13:07:22
【问题描述】:

我最近发现了 sched_setaffinity 方法的这个原型:

extern long sched_setaffinity(pid_t pid, const struct cpumask *new_mask);

我正在尝试使用它来设置任务的关联性,但它似乎不起作用。这是我的实现:

#define _GNU_SOURCE 
#include <linux/sched.h>
#include <linux/cpumask.h>

int set_aff (pid_t pid, int core)
{
   static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS);
   struct cpumask *const task_cpumask = to_cpumask(cpu_possible_bits);
   cpumask_set_cpu(core, task_cpumask);

   sched_setaffinity(pid, task_cpumask);

   return 0;
}

我的代码编译没有任何错误,但是当我尝试调用此方法时,它会中断。有谁知道为什么?

有没有更好的方法从内核方法中执行此操作?

【问题讨论】:

  • 可以查看taskset实用程序源码gitorious.org/util-linux-ng/util-linux-ng/source/…
  • 你想知道什么?为什么你的代码会中断或如何编写功能?
  • 我想知道如何对功能进行编码,因为我是否以错误的方式实例化了某些东西。我在上面从内核调度程序函数设置亲和力的实现导致我的设备的启动序列在某个点后停止并不断重启。
  • 这能回答你的问题吗? Using "sched_setaffinity()" in Linux Kernel

标签: linux-kernel kernel task


【解决方案1】:

你应该试试这个:

    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(1, &cpuset);
    if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset)) 
            exit(1);

【讨论】:

  • 当我从用户空间运行该方法时,此实现有效。我正在尝试从内核空间设置亲和力。内核空间对于 sched_setaffinity() 方法有一个不同的原型,它需要 2 个参数 pid 和一个 struct cpumask。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 2015-02-26
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多