On Tue, Jun 7, 2016 at 1:42 AM, kang joni <[email protected]> wrote: > I'm new to libuv, I have several question. > I want to redirect process stdout to process caller printf, why on read_apipe > function buf.base printf is left undefined? > > My child process code > <html> > <body><code> > #ifdef _WIN32 > #include <windows.h> > #else > #include <unistd.h> > #endif > #include <iostream> > > int main() > { > std::cout <<"init..\n"; > #ifdef _WIN32 > Sleep(500); > #else > sleep(500); > #endif > std::cout<<"selesai..\n"; > return 0; > } > </code> > </body> > </html> > My parent process code > > <html> > <body><code> > #include <stdio.h> > #include <stdlib.h> > #include "uv.h" > > uv_loop_t *loop; > uv_process_t child_req; > uv_process_options_t options; > uv_pipe_t apipe; > > void on_child_exit(uv_process_t *req, int exit_status, int term_signal) { > fprintf(stderr, "Process exited with status %d, signal %d %s\n", > exit_status, term_signal,(char*)req->data); > > uv_close((uv_handle_t*) req, NULL); > } > > uv_buf_t alloc_buffer(uv_handle_t *handle, size_t suggested_size) { > printf("alloc_buffer called, requesting a %lu byte buffer\n"); > > return uv_buf_init((char*) malloc(suggested_size), suggested_size); > } > > void read_apipe(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) { > printf("read %li bytes in a %lu byte buffer\n", nread, buf.len); > if (nread + 1 > buf.len) return; > buf.base[nread] = '\0'; // turn it into a cstring > printf("read: |%s|", buf.base); > } > > int main(int argc, char *argv[]) { > printf("spawn_test\n"); > loop = uv_default_loop(); > int r; > char* args[3]; > args[0] = strdup("dummy"); > args[1] = NULL; > args[2] = NULL; > > uv_pipe_init(loop, &apipe, 0); > uv_pipe_open(&apipe, 0); > > options.stdio_count = 3; > uv_stdio_container_t child_stdio[3]; > child_stdio[0].flags = UV_IGNORE; > child_stdio[1].flags = UV_CREATE_PIPE | UV_READABLE_PIPE|UV_WRITABLE_PIPE; > child_stdio[1].data.stream = (uv_stream_t *) &apipe; > child_stdio[2].flags = UV_INHERIT_FD; > child_stdio[2].data.fd = 1; > options.stdio = child_stdio; > > options.exit_cb = on_child_exit; > options.file = args[0]; > options.args = args; > > if ((r=uv_spawn(loop, &child_req, &options))) { > fprintf(stderr, "%s\n", uv_strerror(r)); > return 1; > } > uv_read_start((uv_stream_t*)&apipe, alloc_buffer, read_apipe); > > return uv_run(loop, UV_RUN_DEFAULT); > } > </code> > </body> > </html> > > parent process stdout output > > spawn_test > alloc_buffer called, requesting a 2003623535 byte buffer > read -4092 bytes in a 2686504 byte buffer > Process exited with status 0, signal 0 (null) > > > Why read function yielded -4092 bytes? > How to print child process stdout to parent stdout exactly?
Am I correct you're targeting libuv v0.10? Can I ask what for? It's old and effectively unmaintained. You should really be using the latest, v1.9.1. nread < 0 indicates an error but you're not currently handling that in your read callback. -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/libuv. For more options, visit https://groups.google.com/d/optout.
