Question

When we use dup to redirect STDOUT to a pipe we do:

close(1); dup(fd[1]);
close(fd[0]);
close(fd[1]);
execlp("ls","-al",(char *) NULL);

but we are closing both ends end of the pipe. then how the STDOUT can be written to the pipe?

Was it helpful?

Solution

You're not closing both ends of the pipe, in the example code. You're closing fd[0] and fd[1]. Initially, closing those would have been enough to close both ends of the pipe, but it's not after you duplicated fd[0]. You'd have to close the duplicated fd also to close all your references to the pipe. That would be silly though: you're keeping an end open precisely so that ls can write to.

Perhaps your confusion is about close() is closing? It closes the fd, the reference to one of the ends of the pipe. It doesn't close the pipe itself: that's what shutdown() would do. (If you don't call shutdown, the pipe is automatically closed when every fd referring to it has been closed.) So, because the duplicated fd is still open, the process can write to the pipe (which isn't closed, because only two of the three references to it were closed).

OTHER TIPS

Because once the file descriptor number 1 (e.g. standard out) is closed, that number is available for further dup or open calls.

You should check the result of your close and dup syscalls.

Of course closing both ends of a pipe is non-sense, unless you do something useful before (i.e. reading or writing on appropriate ends).

See open(2), dup(2), pipe(2), close(2) man pages. Read the Advanced Linux Programming book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top