force scheduler to allocate thread to specific pro

2020-07-24 03:56发布

问题:

Consider a case where we have multiple processor/cores and two threads. Is it possible to force the linux scheduler to always schedule the specific thread(both) to a specific processor at every instance of its execution. Is setting processor affinity to the threads, while creation, sufficient for this purpose

回答1:

If you look at the man page for taskset you can see the following statement:

The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs.

This means that setting the CPU affinity for a particular process will make sure that it's always run on that CPU.

There exist API's that allow you to set thread affinity for particular threads, and I would imagine that this too will be honored by the OS scheduler.

If you look at sched_setaffinity you'll see a line that says:

These restrictions on the actual set of CPUs on which the process will run are silently imposed by the kernel.

which means this will make sure your threads are only run on CPU's set by this function.