views:

76

answers:

2

Newbie question:

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

EDIT:
This program tries to do a complex computation in parallel, I have already used shared memory as a common workspaces for all children to update results and for data transfer.
What I need now is the parent say "start" efficiently to all its children...(called many times)

Thanks

+3  A: 

Your ipc tag says it all. You need to look into inter-process communuication:

  • Shared memory.
  • Semaphores.
  • Pipes.
  • Signals.
  • Memory-mapped files.
  • Sockets.

No doubt there are other possibilities but that's a good start.

How efficient each is depends pretty much on your use case. If you only need to notify a child to do something, signals is probably what I'd use. If you need to transfer some more information between processes, it's probably a good idea to fully specify the requirements.


One thing you may want to consider is to bypass all the inter-process stuff altogether and just use threads. At least in Linux, threads are first class citizens to the scheduler. Older UNIXes may have made a distinction (with user-mode threads) but that is not the case with Linux.

I've found that it's simpler to do it that way and your information is automatically shared (keeping in mind you still need to protect shared stuff with mutexes and such).


Another possibility if you're already committed to shared memory is to use signals. Assuming you've set aside sections of that memory for each child (and they know where they are), signals are probably the quickest way to notify a bunch of children for parallel work.

If your children just wait around in a select loop with (for example) a 30-second timeout for doing periodic work, a signal will cause it to exit immediately with EINTR. That gives you your efficient CPU usage while still giving immediate response.

paxdiablo
I was looking for an efficient way to do it.. e.g I do not think sockets overhead are efficient in this case..
Betamoo
@Betamoo, see the update. It will depend on your _specific_ needs. And sockets can be plenty fast, it depends on the implementation. The data doesn't need to go out over the wire for local sockets.
paxdiablo
Please take a look on the edit..
Betamoo
@Betamoo, if no data transfer is involved (the child already has the shmem open), I would just use signals. They're about as efficient as they can get.
paxdiablo
@paxdiablo, good, but one thing remains.. each child must 'wait' for parent signal without wasting cpu cycles.. (so I do not want to use infinte loop for example)
Betamoo
@Betamoo, if your children use select (with no FDs), it will return with EINTR upon a signal. The select will give you efficiency re CPU usage but with immediate response where need be. I've used this method quite a bit, select with a 10s timeout to do periodic checking of things and signals for immediate response. I'll update the answer with info.
paxdiablo
@Betamoo: That's what `pause()` is for.
caf
+1  A: 

Have a look at Beej's Guide to IPC to understand how it all works...it is an excellent guide. Beej also covers sockets as well on that same site.

tommieb75