### Method

# Gio Subprocess communicate

since: 2.40

#### Declaration [src]

    gboolean
    g_subprocess_communicate (
      GSubprocess* subprocess,
      GBytes* stdin_buf,
      GCancellable* cancellable,
      GBytes** stdout_buf,
      GBytes** stderr_buf,
      GError** error
    )

#### Description [src]

Communicate with the subprocess until it terminates, and all input and output has been completed.

If `stdin_buf` is given, the subprocess must have been created with `G_SUBPROCESS_FLAGS_STDIN_PIPE`. The given data is fed to the stdin of the subprocess and the pipe is closed (ie: EOF).

At the same time (as not to cause blocking when dealing with large amounts of data), if `G_SUBPROCESS_FLAGS_STDOUT_PIPE` or `G_SUBPROCESS_FLAGS_STDERR_PIPE` were used, reads from those streams. The data that was read is returned in `stdout` and/or the `stderr`.

If the subprocess was created with `G_SUBPROCESS_FLAGS_STDOUT_PIPE`, `stdout_buf` will contain the data read from stdout. Otherwise, for subprocesses not created with `G_SUBPROCESS_FLAGS_STDOUT_PIPE`, `stdout_buf` will be set to `NULL`. Similar provisions apply to `stderr_buf` and `G_SUBPROCESS_FLAGS_STDERR_PIPE`.

As usual, any output variable may be given as `NULL` to ignore it.

If you desire the stdout and stderr data to be interleaved, create the subprocess with `G_SUBPROCESS_FLAGS_STDOUT_PIPE` and `G_SUBPROCESS_FLAGS_STDERR_MERGE`. The merged result will be returned in `stdout_buf` and `stderr_buf` will be set to `NULL`.

In case of any error (including cancellation), `FALSE` will be returned with `error` set. Some or all of the stdin data may have been written. Any stdout or stderr data that has been read will be discarded. None of the out variables (aside from `error`) will have been set to anything in particular and should not be inspected.

In the case that `TRUE` is returned, the subprocess has exited and the exit status inspection APIs (eg: g_subprocess_get_if_exited(), g_subprocess_get_exit_status()) may be used.

You should not attempt to use any of the subprocess pipes after starting this function, since they may be left in strange states, even if the operation was cancelled. You should especially not attempt to interact with the pipes while the operation is in progress (either from another thread or if using the asynchronous version).

Available since: 2.40

#### Parameters

`stdin_buf`

  Type: `GBytes`

  Data to send to the stdin of the subprocess, or `NULL`.

  The argument can be `NULL`.
  The data is owned by the caller of the method.

`cancellable`

  Type: `GCancellable`

  A `GCancellable`.

  The argument can be `NULL`.
  The data is owned by the caller of the method.

`stdout_buf`

  Type: `GBytes`

  Data read from the subprocess stdout.

  The argument will be set by the function.
  The argument can be set to `NULL` by the method.
  The argument can be `NULL`.
  The caller of the method takes ownership of the returned data, and is responsible for freeing it.

`stderr_buf`

  Type: `GBytes`

  Data read from the subprocess stderr.

  The argument will be set by the function.
  The argument can be set to `NULL` by the method.
  The argument can be `NULL`.
  The caller of the method takes ownership of the returned data, and is responsible for freeing it.

`error`

  Type: `GError **`

  The return location for a recoverable error.

  The argument can be `NULL`.
  If the return location is not `NULL`, then you must initialize it to a `NULL` `GError*`.
  The argument will be left initialized to `NULL` by the method if there are no errors.
  In case of error, the argument will be set to a newly allocated `GError`; the caller will take ownership of the data, and be responsible for freeing it.

#### Return value

Type: `gboolean`

`TRUE` if successful.