#include #include #include /* to test how dup() works. Specifically, want to learn how the pipe of the */ /* form cmd1 > out works */ int main(void) { int fid; int exam; printf("hello before we are in\n"); /* this message actually goes to */ /* the screen */ fid = open("pipe.test", O_WRONLY | O_CREAT ); close(1); /* close the stdout */ dup(fid); /* dup() uses fd[1] (stdout) */ /* because the close(1) call was */ /* right before it. At this point, */ /* '1' becomes the lowest un-used */ /* file id number. Thus, it (1) */ /* will be used to 'dup' fid. This */ /* is exactly what we want Now all */ /* the output to stdout will be */ /* redirected to the file that */ /* 'fid' was associated with. */ close(fid); printf("hello now we are in\n"); /* this message actually is printed */ /* to the file 'pipe.test' */ }