Running in Real-Time

In C/C++, you can make a process run on a specified CPU and set its priority.

#include <sched.h></code>

// set CPU affinity so we can only run on CPU 7
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(7, &mask);
sched_setaffinity(0, sizeof(mask), &mask); // 0 here means set the affinity of this PID

// set real-time process priority and first-in, first-out policy
struct sched_param params;
params.sched_priority = 50; // higher number = higher priority
sched_setscheduler(0, SCHED_FIFO, &params);

I did more testing on the code I wrote yesterday, and found that running my utility with an entire real-time system was impacting my performance. I used the code above to make my utility run on a dedicated CPU and with real-time priority. This resulted in good performance–I found that messages were being sent late only about .5% of the time–which is acceptable for this application since the requirements specify approximately a rate of 114 Hz.

Read more about sched_setaffinity and sched_setscheduler.