Communication between a parent and his children

Newbie question:

On Unix, in a program with a parent and multiple children:
- How can a parent alert children effectively to do some work ..?
- Or how can children wait for a parental signal to start doing some work?

EDIT:
This program is trying to do a complex computation in parallel, I have already used shared memory as a common workspace for all children to update the results and transfer data.
Now I need the parents to say "start" effectively with all of their children ... (many times)

thanks

+2


a source to share


2 answers


Your tag ipc

says it all. You need to learn interprocess communication:

  • Common memory.
  • semaphores.
  • Pipes.
  • Signals.
  • Memory mapped files.
  • sockets.

There are undoubtedly other possibilities, but this is a good start.

How effective each one is depends largely on your use case. If you only need to notify the child to do something, signals are likely to be used. If you need to pass some additional information between processes, it is probably a good idea to fully specify the requirements.




One thing you might want to consider is to bypass all the stuff between processes and just use streams. At least on Linux, threads are first class citizens for the scheduler. Older UNIX can differentiate (with user-mode threads), but this is not the case for Linux.

I found it easier to do this and your information is automatically shared (bearing in mind that you still need to protect shared things with mutexes, etc.).




Another possibility, if you are already tied to shared memory, is to use signals. Assuming you've allocated sections of this memory for each child (and they know where they are), signals are probably the fastest way to notify a bunch of children to be working in parallel.

If your kids are just waiting in a loop select

with (for example) a 30 second timeout for intermittent work, the signal will cause an immediate shutdown with help EINTR

. This gives you efficient CPU utilization while still maintaining an immediate response.

+4


a source


Check out the Beej IPC Guide to see how it all works ... it's a great guide. Beej also covers sockets on the same site as well.



+1


a source







All Articles