0

I am trying to take the affinity mask and make a comma separated string from it using 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.

When I add the cpu numbers to the array and print it, the output is not correct.

I need the array to eventually output as a string in order to run the Linux command.

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.

miked23
  • 1
  • 4

1 Answers1

0

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:

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Ok, I figured out I forgot to actually set it to a core (CPU_SET(&mask)), but the string still will not print out. Any ideas? – miked23 Jul 10 '18 at 12:55
  • You don't need to do anything like that (how could that ever help?) Just insert the line of code above before your first loop and fix the rest of your code as detailed in my revised post and it should all start working. Oh, and please post code that at least compiles. Thx. – Paul Sanders Jul 10 '18 at 14:45
  • Sorry, @pang, but my post was just fine as it was. – Paul Sanders Aug 06 '18 at 23:18