EDIT: Using fflush(stdout) does not seem work.
The program takes a command line argument when run and gives the man page of the argument in a new Xterm window.
eg: ./a.out ls should give a new window displaying the man page of ls.
NOTE: The only difference between the two programs is line #11.
The code sample given below works as expected.
int main(int argc, char* argv[])
{
int fd[2];
pipe(fd);
pid_t pid = fork();
if(pid > 0)
{
dup2(fd[1], 1);
close(fd[0]);
execlp("echo", "echo", argv[1], NULL);
wait(NULL);
}
else
{
dup2(fd[0], 0);
close(fd[1]);
execlp("xargs", "xargs", "xterm", "-hold", "-e", "man", NULL);
exit(0);
}
return 0;
}
But the code below doesn't work the same:
int main(int argc, char* argv[])
{
int fd[2];
pipe(fd);
pid_t pid = fork();
if(pid > 0)
{
dup2(fd[1], 1);
close(fd[0]);
printf("%s", argv[1]);
fflush(stdout);
wait(NULL);
}
else
{
dup2(fd[0], 0);
close(fd[1]);
execlp("xargs", "xargs", "xterm", "-hold", "-e", "man", NULL);
exit(0);
}
return 0;
}
The PARENT process pipes the command line argument and the CHILD process takes stdin from the pipe which exec command uses to run.
Shouldn't printf() also give output onto stdout?