I am using popen to execute a command under linux, then 4 process wile use the same output.
I am trying to duplicate the file descriptor again to pass it to each process.
here is my code:
FILE* file_source = (FILE*) popen(source_command, "r");
int fd = fileno(file_source);
fdatasync(fd);
int dest_fd[4], y, total = 4;
for (y = 0; y < total; y++) {
dest_fd[y] = dup(fd);
}
actually if total set to 1 it work fin, after changing total = 4 it does not work anymore.
this answer is too close to what i need:
link
Your current approach probably won’t do what you want. When you just duplicate the file descriptors, they all refer to the same pipe – no data is going to get duplicated. For each block of data sent by the source command, exactly one process is going to read it.
If you want to duplicate the data (like the
teeutility does), then you will need to explicitly do so:Error-handling is left as an exercise for the reader 😉