- At first, run this command by superuser.
This will assign CPU cores other than #3 for all processes.
But it will cause some errors, I'm not sure it is really perfect.
ps -e -o pid= | sudo xargs -n 1 taskset -p -a 0xFFFFFFF7 2> /dev/null > /dev/null
- And then, run those codes in your program. This will assign CPU core #3 exclusively for the thread.
// Get current thread ID
pthread_t tid = pthread_self();
// Set the CPU affinity mask of the thread
cpu_set_t cpuset;
CPU_ZERO( &cpuset );
CPU_SET( 3, &cpuset );
pthread_setaffinity_np( tid, sizeof( cpuset ), &cpuset );
// Set the scheduling parameters of the thread
struct sched_param params;
params.sched_priority = sched_get_priority_max(SCHED_FIFO);
pthread_setschedparam( tid, SCHED_FIFO, ¶ms );