gudise
gudise

Reputation: 249

pipeline to bash in C

Suppose I have a program, say "master" #0, plus a number of slave programs #1, #2, #3... which work in such a way that #n's output is to be read by #n+1. I get #0 to open a pipeline to bash shell so all the slave programs are set to run in order (C code very much like this):

PROGRAM #0:

...
pipe=popen("bash","w");
fprintf(pipe,"./program#1 \n");
fprintf(pipe,"./program#2 \n");
fprintf(pipe,"./program#3 \n");
.
.
.
...

Then, since the slaves require way more time to end than the master to execute all the "fprintfs" commands, how does the computer manage the command line's accumulation? Is there a buffer to be filled? And, if I write down "fflush(pipe)" after every fprintf command, will I guarantee commands to be delivered in the right order to bash? Is this even safe?

Upvotes: 0

Views: 623

Answers (1)

One Guy Hacking
One Guy Hacking

Reputation: 1301

You are simply doing the equivalent of issuing the shell commands

% ./program#1
% ./program#2
% ./program#3

You aren't redirecting any input or output.

What you seem to want is to use the call

popen("bash -c './program#1 | ./program#2 | ./program#3'", "w")

(though this looks like something that would be easier done in a shell script than a C program, honestly)

Upvotes: 1

Related Questions