Google経由で質問/結果が見つからないため、次のことができるかどうかわかりません。fork() の stdout をパイプに変更してから、通常の stdout に戻したいと考えています。
これは私が持っているものです:
最初の実行可能ファイル:
int main()
{
int fd[2]; //Used for pipe
int processID;
if(pipe(fd) == -1)
{
printf("Error - Pipe error.\n");
exit(EXIT_FAILURE);
}
if((processID = fork()) == -1)
{
fprintf(stderr, "fork failure");
exit(EXIT_FAILURE);
}
if(processID == 0)
{
int newFD = dup(STDOUT_FILENO);
char newFileDescriptor[2];
sprintf(newFileDescriptor, "%d", newFD);
dup2 (fd[1], STDOUT_FILENO);
close(fd[0]);
execl("./helloworld", "helloworld", newFileDescriptor, NULL);
}
else
{
close(fd[1]);
char c[10];
int r = read(fd[0],c, sizeof(char) * 10);
if(r > 0)
printf("PIPE INPUT = %s", c);
}
}
こんにちは世界
int main(int argc, char **argv)
{
int oldFD = atoi(argv[1]);
printf("hello\n"); //This should go to pipe
dup2(oldFD, STDOUT_FILENO);
printf("world\n"); //This should go to stdout
}
望ましい出力:
world
PIPE OUTPUT = hello
実際の出力:
hello
world