问题
I am trying to use dup2 to communicate between parent and child in the following code.
#include <stdio.h>
#include <unistd.h>
void main(int argv, char *argc) {
int testpipe[2];
pipe(testpipe);
int PID = fork();
if (PID == 0) {
dup2(testpipe[0], 0);
close(testpipe[1]);
execl("./multby", "multby", "3", NULL);
close(testpipe[0]);
close(0);
} else {
dup2(testpipe[1], 1);
close(testpipe[0]);
printf("5");
fclose(stdout);
close(testpipe[1]);
wait(NULL);
}
}
I understand that fclose is needed to write the output into stdout. However, in the case I do not use stdout or stdin, (that is, I do not use file descriptors 1 and 0), is there anyway for me to get the file pointer being reference by a file descriptor so that I can get the file pointer as an argument for fclose?
来源:https://stackoverflow.com/questions/60254987/how-do-i-get-the-file-pointer-that-is-being-referenced-by-a-file-descriptor