tags:

views:

61

answers:

1

Hello

Is it possible to use glib event loops and glib io channels for IPC in one parent - many child process model?

Parent and children must be able to send each other 'commands'.

Can you point me to some tutorials or examples?

+1  A: 

Yes. But it doesn't contain a complete IPC solution in itself and is probably not perfectly compatible with every IPC implementation out there. With parent and children I guess you mean server and clients? Generally you open some sort of network connection or create pipe for communication and you get a file descriptor (even if you use a high level library that hides this). You can pass this file descriptor to glib and get a callback then data is available for reading (or the connection closed). Some popular IPC methods like CORBA and DBUS already has glib integration so you don't even need to bother with file descriptors and such. The glib event loop is described here. It might seem utterly complex compared to just using poll() directly but on the other hand it is very portable.

The basic usage is to create a source with g_source_new() and add it to your main context with g_source_attach() and then add your file descriptor to the source with g_source_add_poll().

If you havn't decided on glib yet you might want to check out libevent with does the same thing as glib but is (IMHO) much easier to use. It is also significantly better at handling 1000+ simultanious clients (at least in Linux, other operating systems may not even support that). But on the other hand it's not as portable and will probably only work on fairly posix compatible systems.

Grim
No, not server and clients. I have parent (master) process and about 10 children. I want children to listen to parent commands (e.g. die(), reload_config(), do_this(), do_that()) and parent to listen to children commands (e.g. i_have_finished()). I don't need 1000 simultaneous clients, only about 10. And commands are rare.
Marko Kevac
If the children are local processes started by the master then it's a simple task that glib alone can solve (if the children are remote you need to set that up yourself but otherwise it's not very different). You can use g_spawn_async_with_pipes() to start a child process and get the file descriptiors (to be used as decribed above). You also get a PID which can be used to get a callback then the child terminates with g_child_watch_add().
Grim