CST8204 - Lab Exercise 13

pipe() and dup() - redirection

Your input and output programs and your fork/wait program both copied a text input file to an output file, something that cat(1) or cp(1) can do quite readily, as you demonstrated last week.

This week, write a program that will create a pipe before it forks a child process. Use dup() or dup2() to redirect stdin and stdout in such a way that:



infile

stdin =>

parent process




child process



stdout =>

pipe

stdin =>





stdout =>

outfile








Note that the same I/O function can be used once redirections are established in both the parent and child. That's because the parent will read from stdin and write to stdout, while the child will also read from stdin and write to stdout .

You can even replace your I/O function in the child with an exec() for the cp program if you have time, once you have competed the read/write version, in order to examine the effect of your file redirections on an executed program.

Be sure to handle your processes correctly, with a suitable wait() in the parent.

Compare the input and output files with diff -s (for text files) or cmp (for any file, but especially binary files). Try both types of files, since read/write can readily handle both.

When you are ready (and well before the end of the lab class) demo your program(s).