#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

static void watch_pid (GPid pid, gint status, gpointer data);

static GMainLoop* main_loop;

int main (int argc, char *argv[]) {
 GPid pid;
 GError *error = NULL;
 gboolean status;
 gchar *args[2];

 args[0] = g_build_filename ("C:", "Windows", "notepad.exe", NULL);
 args[1] = NULL;

 status = g_spawn_async_with_pipes ( NULL,
                                     args,
                                     NULL,
                                     G_SPAWN_DO_NOT_REAP_CHILD,
                                     NULL,
                                     NULL,
                                     &pid,
                                     NULL,
                                     NULL,
                                     NULL,
                                     &error);

 g_print ("Command: %s\n", args[0]);
 if (status == FALSE) {
   g_print ("%s\n", error->message);
   g_free (args[0]);

   system("PAUSE");

   return EXIT_FAILURE;
 }

 g_child_watch_add (pid, watch_pid, (gpointer)args[0]);
 main_loop = g_main_loop_new (NULL, FALSE);
 g_main_loop_run (main_loop);

 g_free (args[0]);

 system("PAUSE");

 return EXIT_SUCCESS;
}

static void watch_pid (GPid pid, gint status, gpointer data) {
 char *cmd = (char *)data;

 if (status != 0) {
   /* error */
   g_print ("Command \'%s\' failed\n", cmd);
 }
 else {
   /* success */
   g_print ("Command \'%s\' exited\n", cmd);
 }
 g_spawn_close_pid(pid);

 g_main_loop_quit (main_loop);
}