In this post I'm going to show you how to bind a process into a particular CPU in Ubuntu. Usually the OS manages the processes and schedules the threads. There is no guarantee on which CPU your process is running, OS will schedule it based on the resource availability.
But there is a way to specify the CPU and bind your process into a CPU.
Following is an sample to demonstrate how you can do that.
1. Sample code which consumes 100% CPU (for demo purposes)
2. Compile and run the above simple program
3. Use the 'htop' to view the CPU usage
In the above screen shot you can see that my sample process is running in the CPU 2. But its not guaranteed that it will always remain in CPU2. The OS might assign it to another CPU at some point.
4. Run the following command, it will assign the process 5982 permanently into 5th CPU (CPU # start at zero, hence the index 4 refers to 5th CPU.)
In the above screen shot you can see, that 100% CPU usage is now indicated in the CPU 5.