A way of test out GIO Subprocess if it's running/finished

cannot find out if there’s some flag in GSubprocess structure that indicates the state of subprocess (running/finished), or some correspondent function to test out if a subprocess is still running or finished. I suppose it has to be something obvious that I’ve missed from its doc. Can you please give some hints on this topic?

You want g_subprocess_wait() or g_subprocess_wait_check(), or the async variants. :slight_smile:

I’ve seen them, for example look at this flow:

GSubprocess *proc = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE, &err, PING, ..., NULL);

// at some point there's a user request to stop it
// and there's a stopper on this event
void stop_proc(GSubprocess *proc) {
   if (!proc) return;    
   g_subprocess_send_signal(proc, SIGTERM);
   // sleep_msec(100);
   if (is_proc_still_running(proc)) g_subprocess_force_exit(proc); 
   GError *err = NULL;                           
   g_subprocess_wait(proc, NULL, &err);
   if (err) { WARN("subprocess wait", err); } else {                                          
     int rc = g_subprocess_get_status(proc);
     g_print("finished (rc=%d)\n", rc); // LOG

i.e. does it need to register callback somewhere with
g_subprocess_wait_check_async (subprocess, cancellable, callback, user_data);
just to set a flag in ‘callback’ that indicates the subprocess finish?
Isn’t there any simpler way to get is_subprocess_still_running(GSubprocess *proc)?

Yes, that’s how you’re going to want to do it. I see all of the related functions cannot be used until after wait has returned.

that’s how you’re going to want to do it

to not add extra code just for one bool flag, I thought that maybe there’s some flag already exist in that subprocess structure (that indicates a subprocess state)

Thank you for hints

You can call g_subprocess_get_identifier() to see if it returns a non-NULL value. If you want to wait for the process to finish, you should use g_subprocess_wait_check_async() or the synchronous variant, though

1 Like

Thank you very much for suggestion, you’re right, g_subprocess_get_identifier() one call suits much better for my case

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.