C restore stdin to the actual standard input after using dup2

645 Views Asked by At

how would I make it so that the fd of stdin returns to its original "file destination"? Could this be accomplished by originally forking, doing what I need to using the child process and then killing that child, leaving me back to the parent with the original file descriptors for things? Are file descriptors shared between parents and children actively?

1

There are 1 best solutions below

3
ikegami On

That would work. Changes to the child's stdin have no effect on the parent.

Stuff like file position is shared as long as the fds refer to the same stream because that's a property of the stream. But re-opening it or dupping onto it associates the descriptor with a different stream.