Monday 24 June 2019

How to communicate with a C-written subprocess ?

I hope it's ok to post questions like this here.In NodeJS (using Electron, so console.log goes on the window console), I want to spawn a child process that's a C program, and read/write into its stdio to communicate.This is the Javascript :const { spawn } = require('child_process'); var utfEncoder=new TextEncoder("utf-8"); var utfDecoder=new TextDecoder("utf-8"); var process = spawn("./src/test-c"); process.stdout.on('data', (data) => { console.log(utfDecoder.decode(data)); }); model.send = function(s) { model.process.stdin.write(utfEncoder.encode(s)); } And the C :int main() { char buffer[100]; printf("ready !"); fflush(stdout); for (int a = 0; a < 100; ++a) { scanf("%s", buffer); printf("I read: %s", buffer); fflush(stdout); } return 0; } And the "ready !" never appears anywhere. But it works when the program only prints a message every second :int main() { for (int a = 0; a < 10; ++a) { printf("ready !"); fflush(stdout); sleep(1); } return 0; } With this program the message "ready !" appears every second on the window console, but with the first one nothing happens (not even the first message before scanf).What am I doing wrong here ?

Submitted June 24, 2019 at 02:10PM by Amaury__

No comments:

Post a Comment