Re: Spawning Processes



I've used the Glib g_spawn_async_with_pipes() function, and it works
well enough; I can't speak to its Glibmm wrappers. Whether or not the
g_spawn() are more appropriate than your own fork()/exec() depends on
how you wish to do your communication. If your gtkmm host program needs
to provide interaction with your subprocess binaries via those binaries'
stdin, then g_spawn() might be convenient. OTOH if what you need to do
is spawn a pipeline of subprocesses, each one piped to the next, and
just need to read the stderr and stdout of the last in the chain, you
might be better off with fork()/exec(), redirecting the last child
process's stderr and stdout to files, and reading those files from your
host program. 

There are advantages to letting the filesystem do your IO buffering. On
the downside you have to provide a temporary directory for each instance
of your host program -- I embed its pid in the tmp directory's name --
for to write the final stderr/stdout files, and remember to clean up the
tmp directory when you are done. On the upside, you can exec a shell
command e.g. something like execl(/bin/sh, "-c \"program1 | program2 |
program3 > /tmp/mydir/prog.stdout 2> /tmp/mydir/prog.stderr\"") or
whatever, and let the shell handle all the intermediate pipe buffering,
as well as buffer the final output to the files. This can be a
consideration if there is lots of output.

You could do something similar with g_spawn(), and read the shell's
final stdout and stderr pipes directly from your host, if that is what
you desire. But if you decide to redirect the subprocess's stderr and
stdout to files, you are probably better off with fork()/exec(), and
freopen().



On Thu, 2006-08-10 at 14:13 +0200, Javier Aranega wrote:
> Hi !
> 
> I write an application that finally has to make some calls to binaries
> with some parameters that I collect with the gtkmm interface that I've
> created. I have some communication between the different binaries, the
> standard output of some of them is the input of others. Now I work
> directly with fork/exec C functions and pipes, but I have regarded de
> spawn Glib functions
> (http://gtkmm.org/docs/glibmm-2.4/docs/reference/html/group__Spawn.html) and perhaps are interesting for my application. Someone have some extra information or some example to work with that?
> 
> Thanks a lot
> 
> Javi Ar�ga
> Universidad de Alicante (Spain)
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]