Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active August 25, 2025 16:24
Show Gist options
  • Select an option

  • Save Miqueas/e6d008f2ca8a86d90802e41c89d9baae to your computer and use it in GitHub Desktop.

Select an option

Save Miqueas/e6d008f2ca8a86d90802e41c89d9baae to your computer and use it in GitHub Desktop.
[C + Gio] Subprocess

GSubprocess examples in C.

Quick start:

// Build command: gcc -o gsubprocess gsubprocess.c `pkgconf --cflags --libs gio-2.0`
#include <gio/gio.h>

int
main(int argc, char** argv)
{
  GSubprocess* sub = g_subprocess_new(
    G_SUBPROCESS_FLAGS_NONE,
    NULL,
    "echo", "Hello world!", NULL
  );

  gboolean success = g_subprocess_wait(sub, NULL, NULL);
  g_object_unref(sub);

  return 0;
}

Read output:

#include <gio/gio.h>

int
main(int argc, char** argv)
{
  GSubprocess* sub = g_subprocess_new(
    G_SUBPROCESS_FLAGS_STDOUT_PIPE,
    NULL,
    "echo", "Hello world!", NULL
  );

  gboolean success = g_subprocess_wait(sub, NULL, NULL);

  if (success) {
    GInputStream* stdout_pipe = g_subprocess_get_stdout_pipe(sub);
    GDataInputStream* data = g_data_input_stream_new(stdout_pipe);
    gchar* line = g_data_input_stream_read_line(data, NULL, NULL, NULL);

    while (line != NULL) {
      g_print("%s\n", line);
      line = g_data_input_stream_read_line(data, NULL, NULL, NULL);
    }

    g_object_unref(data);
    g_free(line);
    data = NULL;
    stdout_pipe = NULL;
  }
  
  g_object_unref(sub);

  return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment