CST8204- Lab Exercise 11

fork() and wait()

Use your input and output programs from the previous lab as a model to create a single program that combines both actions by forking a child process to act as one end of a named pipe.

First, use mkfifo(3) to create a named pipe. In the parent (original) process, fork a child. Now have the parent and the child each open one end of the FIFO, the parent for output and the child for input.

Read the contents of a file and write it to the pipe in the parent process, the same as last week's input program. At end of file, wait(2) for the child to complete and then unlink(2) or remove(3) the FIFO.

In the child process, read the FIFO until end of file, writing the lines read into a second file, just like last week's output program, using exit(2) upon completion. You can test that the two files are identical by using diff(1) or cmp(1) to compare them.

Be sure to test with non-trivial files, not just one or two lines!

Because you are once again only going to use text files, fopen/fclose with fgets/fputs can be used for ease of programming, although you can use fread/fwrite (or even open/close/read/write) if you wish. Remember that you may need to #define _XOPEN_SOURCE 600 before any of your #include statements.