Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep parent and child process on same core

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?

like image 245
ngupta Avatar asked Jan 23 '13 03:01

ngupta


People also ask

Can a parent and child process both have the same PID?

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.

How can a parent process and its child process communicate with each other?

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.

What states are shared between parent and child process?

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.

How do parent and child process share resources?

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.


1 Answers

Would it be possible to run the child in a thread rather than in its own process?

like image 88
JackCColeman Avatar answered Nov 04 '22 19:11

JackCColeman