I want to run node.js as a subprocess and feed it input. Using C, here is some sample code of mine that does that.
The issue I have is that although the subprocess's stdout is still directed to the terminal, I see nothing after having fed the subprocess stdin a print 'Hello World' line. Even if I fflush() the pipe, I see nothing on output. However, if I close the pipe's input, then the 'Hello World' appears on the terminal.
The subprocess seems to simply buffer - why is that? I would like to eventually redirect the subprocess stdout to another pipe and read it in from main().
int main(int argc, char* argv[]) {
int toNode[2]; pipe(toNode); pid_t child_pid = fork(); if (child_pid == 0) { // child // close write end close(toNode[1]); // connect read end to stdin dup2(toNode[0], STDIN_FILENO); // run node executable char* arg_list[] = { "/usr/bin/node", NULL}; execvp(arg_list[0], arg_list); fprintf(stderr, "process failed to start: %s\n", strerror(errno)); abort(); } else { // parent FILE* stream; // close read end close(toNode[0]); // convert write fd to FILE object stream = fdopen(toNode[1], "w"); fprintf(stream, "console.log('Hello World');\n"); fflush(stream); //close(toNode[1]); waitpid(child_pid, NULL, 0); } return 0; }