本文介绍了使用"sched_setaffinity()";在Linux内核中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sched_setaffinity上的很多帖子,但在内核空间中使用此帖子几乎没有.

Lots of posts on sched_setaffinity, but almost none on using this in Kernel Space.

我正在使用内核4.14.79.

I am on Kernel 4.14.79.

我尝试使用用户空间方法以以下形式调用sched_setaffinity:

I tried using the User Space method of calling sched_setaffinity in the form of:

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

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

But upon trying to compile the Kernel, I got errors and realized this form will not work in Kernel Space.

我看到sched_affinity是在sched.h中定义的,其格式为:

I see that sched_affinity is defined in sched.h and has the form:

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

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

But I am confused on how to correctly create the new_mask parameter with the correct CPU number. The documentation on it is not very helpful.

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

Can someone show an example of how to use this function in Kernel Space to set a process to a specific CPU?

推荐答案

在浏览内核文件以查找cpumask出现的位置后,我自己找到了答案.

Found an answer myself after digging through Kernel files looking for where cpumask makes an appearance.

您可以使用以下两个功能:

You can use these two functions:

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);

这篇关于使用"sched_setaffinity()";在Linux内核中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:00