Skip to content

Instantly share code, notes, and snippets.

@Qix-
Created January 21, 2020 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Qix-/07e567300ef75d2351aba598c0a4e9b3 to your computer and use it in GitHub Desktop.
Save Qix-/07e567300ef75d2351aba598c0a4e9b3 to your computer and use it in GitHub Desktop.
Tests the ability to pipe between multiple processes uving libuv and without a dedicated read/write buffer copy
#include <uv.h>
#include <unistd.h>
extern char *environ[];
static int uv_perror(int r, const char *msg) {
printf("%s: %s (%s)\n", msg, uv_strerror(r), uv_err_name(r));
return 1;
}
static void on_exit(uv_process_t *p, long long status, int sig) {
printf("<process %d exited (status=%lld, sig=%d)>\n", p->pid, status, sig);
}
int main(void) {
uv_loop_t *loop = uv_default_loop();
static char *argv1[] = {
"/usr/bin/rev", NULL
};
static char *argv2[] = {
"/usr/bin/base64", NULL
};
static char cwd[PATH_MAX];
uv_process_options_t opt1;
uv_process_options_t opt2;
opt1.cwd = getcwd(cwd, sizeof(cwd));
opt1.env = environ;
opt1.exit_cb = &on_exit;
opt1.flags = UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS;
opt1.stdio_count = 3;
memcpy(&opt2, &opt1, sizeof(opt1));
static uv_stdio_container_t stdio1[3];
static uv_stdio_container_t stdio2[3];
stdio1[0].flags = UV_INHERIT_FD;
stdio1[0].data.fd = 0;
stdio1[2].flags = UV_INHERIT_FD;
stdio1[2].data.fd = 2;
stdio2[1].flags = UV_INHERIT_FD;
stdio2[1].data.fd = 1;
stdio2[2].flags = UV_INHERIT_FD;
stdio2[2].data.fd = 2;
uv_pipe_t p1_out;
int r = uv_pipe_init(loop, &p1_out, 1);
if (r != 0) {
return uv_perror(r, "could not create pipe");
}
stdio1[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
stdio1[1].data.stream = (uv_stream_t *) &p1_out;
stdio2[0].flags = UV_INHERIT_STREAM;
stdio2[0].data.stream = (uv_stream_t *) &p1_out;
opt1.args = argv1;
opt1.file = argv1[0];
opt2.args = argv2;
opt2.file = argv2[0];
opt1.stdio = stdio1;
opt2.stdio = stdio2;
uv_process_t p1, p2;
r = uv_spawn(loop, &p1, &opt1);
if (r != 0) {
return uv_perror(r, "could not start first process");
}
r = uv_spawn(loop, &p2, &opt2);
if (r != 0) {
return uv_perror(r, "could not start second process");
}
return uv_run(loop, UV_RUN_DEFAULT);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment