【问题标题】:Using "sched_setaffinity()" in Linux Kernel在 Linux 内核中使用“sched_setaffinity()”
【发布时间】:2019-03-06 00:46:50
【问题描述】:

sched_setaffinity 上有很多帖子,但几乎没有关于在内核空间中使用它的帖子。

我在内核 4.14.79 上。

我尝试使用用户空间方法调用sched_setaffinity,形式为:

cpu_set_t my_set;        
CPU_ZERO(&my_set);       
CPU_SET(7, &my_set);     
sched_setaffinity(0, sizeof(cpu_set_t), &my_set);

但是在尝试编译内核时,我遇到了错误并意识到这种形式在内核空间中不起作用。

我看到sched_affinitysched.h 中定义并且具有以下形式:

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

但我对如何正确创建具有正确 CPU 编号的 new_mask 参数感到困惑。它的文档不是很有帮助。

有人可以举例说明如何在内核空间中使用此函数将进程设置为特定的 CPU 吗?

【问题讨论】:

    标签: linux kernel system-calls


    【解决方案1】:

    在挖掘内核文件以查找cpumask 出现的位置后,我自己找到了答案。

    你可以使用这两个功能:

    cpumask_clear(struct cpumask *dstp) //clear the mask you are about to use

    cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) //set the cpu number

    所以我用的是这样的:

    struct cpumask mask;  
    cpumask_clear(&mask); 
    cpumask_set_cpu(cpuNumber, &mask); 
    sched_setaffinity(pid, &mask); 
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多