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?
A parent process may have multiple child processes, but a child process only one parent process. On the success of a fork() system call: The Process ID (PID) of the child process is returned to the parent process. 0 is returned to the child process.
The inter communication between a child process and a parent process can be done through normal communication schemes such as pipes, sockets, message queues, shared memories.
3.3 When a process creates a new process using the fork() operation, which of the following state is shared between the parent process and the child process? Answer: Only the shared memory segments are shared between the parent process and the newly forked child process.
There are other ways for process to share resources, it is by using named pipes, and named files. A file can be memory mapped so this effectively just creates a shared buffer space. This is all relative to IPC : Inter Process Communication.
Would it be possible to run the child in a thread rather than in its own process?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With