简体   繁体   English

无法获得处理器关联性(Linux)

[英]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.

相关问题 itk 3.20.1处理器关联 - itk 3.20.1 Processor affinity 为 MATLAB 引擎设置处理器关联 (Windows 7) - Set processor affinity for MATLAB engine (Windows 7) 设置处理器对进程及其影响的亲和力 - Setting processor affinity for a process and its Effects 找出哪个模块集处理器相似性掩码 - Finding out which module set processor affinity mask 如何使用Ryzen 7在Windows上修复Qt 5.9线程的处理器亲和力 - How to fix processor affinity for Qt 5.9 Thread on Windows with Ryzen 7 SetThreadAffinityMask 设置的处理器亲和性是否也适用于子线程? - Does a processor affinity set by SetThreadAffinityMask also apply to children threads? 有没有办法使用boost线程库设置线程关联到处理器核心? - Is there a way to set thread affinity to a processor core with the boost thread library? 我无法使一个非常基本的命令提示符文本处理器正常工作。 ofstream()的问题 - I'm having trouble getting a very basic command prompt text processor working. Problems with ofstream() 无法在C ++程序中获取Linux&#39;dd&#39;命令的输出 - Having trouble getting the output of Linux 'dd' command in C++ program 如何在Linux下的C / C ++中为计时器设置亲和力? - How to set affinity for a timer in C/C++ under Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM