In a single core system, the OS allows multiple processes to run by sharing CPU time with the multiple processes. This is called concurrency, which gives the illusion of multiple processes executing at once, but is in fact just using a scheduler to give each process dedicated time on the CPU. The time associated with switching processes on a single core is overhead caused by context switching.
When a multi-core processor is available to an OS (e.g., Linux), the scheduler will do its best to allow processes to run simultaneously (by placing processes on different cores) in addition to running concurrently (different processes on the same core).
To control which core a process runs on, we can tell the scheduler to give a process a certain affinity towards a given set of CPUs.
Using taskset
, we can get/set the CPU affinity of a particular process. Consider the following example.