[英]Trouble getting processor affinity (linux)
I am trying to take the affinity mask and make a comma separated string from it using CPU_ISSET(). 我正在尝试使用亲缘性掩码,并使用CPU_ISSET()从中使用逗号分隔字符串。 Then, I need to add the string "taskset -c" in front of it and executable behind it in order to create a Linux command. 然后,我需要在其前面添加字符串“ taskset -c”,并在其后添加可执行文件,以创建Linux命令。
When I add the cpu numbers to the array and print it, the output is not correct. 当我将cpu编号添加到数组并打印时,输出不正确。
I need the array to eventually output as a string in order to run the Linux command. 我需要数组最终以字符串形式输出,以便运行Linux命令。
int main() {
cpu_set_t mask;
int temp[FILENAME_MAX]
for(int i = 0; i < CPU_SETSIZE; i++)
{
if(CPU_ISSET(i, &mask))
{
temp[i] = i;
}
}
for(int i: temp)
os << i;
string str(os.str());
cout << str;
return 0;
}
output is a ton of zeros and then random numbers. 输出是一吨的零,然后是随机数。
You forgot to load up your mask, which you can do like so: 您忘了装上口罩了,可以这样做:
sched_getaffinity (getpid (), CPU_SETSIZE, &mask);
That's all. 就这样。
Edit: 编辑:
I didn't notice before but those loops are flawed, even after your edit. 我之前没有注意到,但是即使您进行了编辑,这些循环还是有缺陷的。 For the first one, do something like: 对于第一个,请执行以下操作:
for (int i = 0; i < CPU_SETSIZE; i++)
temp [i] = (CPU_ISSET (i, &mask)) ? i : -1;
And for the second, do: 第二,执行以下操作:
for (int i : temp)
{
if (i >= 0)
os << i;
}
And, of course, declare temp
as: 并且,当然,将temp
声明为:
int temp [CPU_SETSIZE];
Then you should start to get some sensible results. 然后,您应该开始获得一些明智的结果。
Recommendation: pay more attention to detail. 建议:多注意细节。 You're not going to get anywhere if you don't. 如果不这样做,您将一事无成。 And accepting this answer would be nice as I have tidied everything up for you. 接受这个答案会很好,因为我已经为您整理了所有东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.