You can lock your program to one core using
schedutils as described in this Cyberciti article:
sudo apt-get install schedutils
sudo taskset -c 3 -p 13545 # Lock PID 13545 to core 3
Other processes can still be scheduled on the same core, though. So the second thing to do is to make sure your command runs with the highest priority using the nice command (this will tell the Linux kernel that other processes should be pre-empted if necessary). Start your program in this way:
nice -n -20 your-program
There are some other possible reasons for your timing issues. These are not as easy to do anything about:
- If you are programming in Python there is a garbage collector that sometimes pauses your program to free up unused memory.
- Interrupts makes the CPU handle something else than you want. For example, network packets or other input/output.
- If your program is sleeping a lot there might be other processes that fill up the CPU caches (L1/L2 cache). This forces you to wait for RAM access.
- Even worse if your RAM is full so that your process gets swapped out to disk because SD cards are sloooow.
There are ways to make your process realtime, which means that it will run with certain timing guarantees. The problem with this is that everything else might be slower, and it is a complex topic. If you want to go down this rabbit hole I suggest you start reading up on real time processes in Linux.