File: spawn.md

package info (click to toggle)
glib2.0 2.85.1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 66,344 kB
  • sloc: ansic: 541,342; python: 9,624; sh: 1,572; xml: 1,482; perl: 1,222; cpp: 535; makefile: 316; javascript: 11
file content (76 lines) | stat: -rw-r--r-- 2,645 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Title: Spawning Processes
SPDX-License-Identifier: LGPL-2.1-or-later
SPDX-FileCopyrightText: 2014 Red Hat, Inc.
SPDX-FileCopyrightText: 2017 Philip Withnall

# Spawning Processes

GLib supports spawning of processes with an API that is more
convenient than the bare UNIX [`fork()`](man:fork(2)) and
[`exec()`](man:exec(3)).

The `g_spawn_…()` family of functions has synchronous ([func@GLib.spawn_sync])
and asynchronous variants ([func@GLib.spawn_async],
[func@GLib.spawn_async_with_pipes]), as well as convenience variants that take a
complete shell-like command line ([func@GLib.spawn_command_line_sync],
[func@GLib.spawn_command_line_async]).

See [`GSubprocess`](../gio/class.Subprocess.html) in GIO for a higher-level API
that provides stream interfaces for communication with child processes.

An example of using [func@GLib.spawn_async_with_pipes]:
```c
const gchar * const argv[] = { "my-favourite-program", "--args", NULL };
gint child_stdout, child_stderr;
GPid child_pid;
g_autoptr(GError) error = NULL;

// Spawn child process.
g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL,
                          NULL, &child_pid, NULL, &child_stdout,
                          &child_stderr, &error);
if (error != NULL)
  {
    g_error ("Spawning child failed: %s", error->message);
    return;
  }

// Add a child watch function which will be called when the child process
// exits.
g_child_watch_add (child_pid, child_watch_cb, NULL);

// You could watch for output on @child_stdout and @child_stderr using
// #GUnixInputStream or #GIOChannel here.

static void
child_watch_cb (GPid     pid,
                gint     status,
                gpointer user_data)
{
  g_message ("Child %" G_PID_FORMAT " exited %s", pid,
             g_spawn_check_wait_status (status, NULL) ? "normally" : "abnormally");

  // Free any resources associated with the child here, such as I/O channels
  // on its stdout and stderr FDs. If you have no code to put in the
  // child_watch_cb() callback, you can remove it and the g_child_watch_add()
  // call, but you must also remove the G_SPAWN_DO_NOT_REAP_CHILD flag,
  // otherwise the child process will stay around as a zombie until this
  // process exits.

  g_spawn_close_pid (pid);
}
```

## Spawn Functions

 * [func@GLib.spawn_async_with_fds]
 * [func@GLib.spawn_async_with_pipes]
 * [func@GLib.spawn_async_with_pipes_and_fds]
 * [func@GLib.spawn_async]
 * [func@GLib.spawn_sync]
 * [func@GLib.spawn_check_wait_status]
 * [func@GLib.spawn_check_exit_status]
 * [func@GLib.spawn_command_line_async]
 * [func@GLib.spawn_command_line_sync]
 * [func@GLib.spawn_close_pid]