Re: g_spawn callback when child exits



"Brian J. Murrell" <gtk-list-in interlinx bc ca> writes:
I am using g_spawn_async_with_pipes() to control a subprocess which
takes commands on stdin.  The parent needs to know when the child is
running and when it has exited however so that it does not get a
SIGPIPE trying to send a command to the process after it has exited.


Typically one handles this by setting the SIGPIPE action to SIG_IGN
so that you get an EPIPE error instead of SIGPIPE.
 
I guess I am going to have to install a signal handler to reap the
child when it has exited and toggle a flag so that the parent knows
not to send any more commands. 

May be a race condition in that:

 if (flag)
   /* SIGCHLD can come here */
   write_to_child ();
 
Could g_spawn_*() not install a SIGCHLD handler that calls a
registered callback when a child process exits?  Having g_spawn_*()
handle this would be much cleaner than the developer having to do it
themselves, no?

Because signal handlers are effectively global variables, we try to
avoid setting them up in libraries - they are reserved for application
use.

I think you normally get a G_IO_HUP condition on the file descriptor
when the other end closes, or you get read() returning immediately
with no data.

Another thing you can do to check if the child is running is to
waitpid() with WNOHANG, to do that you have to run with
G_SPAWN_DO_NOT_REAP_CHILD.

Havoc



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