Pipeline a -bash in C

0

I have a question about how the computer uses an open pipeline with the bash shell. Suppose I have a "master" program # 0, and a series of slave programs # 1, # 2, # 3 ... And also, the result of # 1 is used by # 2, # 2 by # 3, and so on. If my program # 0 opens a pipeline sequentially with 1,2,3, etc and sends them running in a way like this:

PROGRAM # 0:

...

pipe=popen("bash","w");

fprintf(pipe,"./programa#1 \n");

fprintf(pipe,"./programa#2 \n");

fprintf(pipe,"./programa#3 \n");

.

.

.

...

So, how does the computer manage the fact that orders are "accumulated" sent to bash? Do you have a buffer? If after each fprintf I write fflush (pipe), will I guarantee that the commands arrive at the shell in the correct order?

    
asked by Guillermo. D. S. 27.08.2017 в 17:20
source

1 answer

0
  

How does the computer handle the fact that orders are "accumulated" sent to bash?

The computer does not handle the sent orders in any way. A bash receives always orders sequentially and limits them to chaining them in ... exact, an internal buffer.

  

Do you have a buffer?

Indeed, it has an internal buffer. Facing the programmer is not very different from writing to a file.

  

If after every fprintf I write fflush (pipe), will I guarantee that the commands reach the shell in the correct order?

With fflush you will only force the console refresh ... the messages will continue to arrive in random order because you are working with independent processes (parallel programming), then the messages will continue to be mixed ... if it reaches Improving the presentation a bit will be because, as I have said, the refresh of the console is an expensive process that facilitates the processes to be synchronized slightly ... but it is an operation that does not work miracles.

To guarantee a specific order you will have to implement blocks between the processes (look for information on traffic lights and music)

    
answered by 28.08.2017 / 08:57
source