After dup2(myfd,1);
, file descriptor 1 doesn't refer to the original standard output any more. There is no hidden place where the kernel may look for a "true" stdout when you call dup2(1,1)
(which is a noop).
You can dup
stdout into a new descriptor, keep it and redirect back to it when you want:
int oldstdout = dup(1);
....
dup2(oldstdout,1);
close(oldstdout);