-1

Linux has many threads and processes executing across (lets say 2) CPU cores. I would like my single-threaded C/C++ application to be the only thread on CPU0. How would I "move" all the other threads to use CPU1?

I know I can use the Linux CPU scheduling functions to set affinity for a thread:

int sched_setaffinity(pid_t pid,size_t cpusetsize,cpu_set_t *mask);

but how I can push all the other threads on to CPU1? Is there a relatively simple way of doing this?

Would I have to get a list of all the pids, iterate through setting them all to CPU1 and then set my application thread to CPU0?

user997112
  • 29,025
  • 43
  • 182
  • 361

1 Answers1

0

Your idea about doing this seems to be correct. However I would like to mentioned few points regarding this which should be understood carefully.

1.sched_setaffinity() is kind of request to kernel/scheduler(not the command) to select which CPUs is that process/thread allowed to execute. Actual scheduling of a process does depends on many other complicated factor.

2.You have mentioned that you may iterate through all PID's. This is not a good idea as by doing this you may try to change the scheduling for kernel services and init process. In all probability program would not have sufficient rights to do it for these processes but still we should not try to alter attributes of these processes as we do not know the impact.

Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48