I understand that a process (parent) can be pinned to a core using sched_setaffinity
and then the forked process inherits the affinity and would also be pinned on the same core. However, I don't want to keep them pinned together to the same core forever. Ideally, what I want is for them to stay together on the same CPU i.e. if parent is migrated by the OS scheduler, the child should follow the parent and get migrated to the same CPU as parent.
One possible way is to have a shared variable
where parent updates its current CPU periodically. Then the child can look up this variable periodically and sched_setaffinity
to migrate to same CPU as the parent. However, this looks a bit hacky and may involve periods where they are running on separate CPUs. Any better ways to achieve this?