CST8204 - Lab Exercise 10
Named Pipes (FIFOs)
First, use the mkfifo, echo, cat, and other commands to explore the use of named pipes as described in the preparation document.
Now make a pair of programs, as you did with the near/far pair used for the Signals exercise, but using named pipes. Name these input and output, and create them (and a test FIFO using the mkfifo(1) command) as follows:
input takes two arguments, a FIFO name and a file name. It's a simple program, and just reads lines from the input file until the end and writes each line to the pipe. Test it alone by running a cat command in the background, like cat < fifoname & which will read from the pipe and list the output to stdout. Since you already know that cat works correctly, it's an effective test of input.
output is very similar, and also uses two arguments, the same FIFO name as input and an output filename. output reads from the pipe until end of file and writes the output (hence the name) into the file. You can then use cmp(1) or diff(1) to compare your input and output files. You can also test your output program separately as above with a command resembling cat some.file > fifoname &.
Once you have both programs working, test them together. Next, change input so that it will create a fifo using mkfifo(3) and display its name instead of using the already-created pipe. Then change output so that it will delete the fifo when it has finished reading it. Demo these last programs.
Because this pair of sample programs is only going to use text files (as you can tell from the use of cat to test them), I would recommend the use of fopen/fclose with fgets/fput for ease of programming, although you may use fread/fwrite (or even open/close/read/write) if you prefer.
mkfifo(1) [derived from info mkfifo]
creates FIFOs (also called "named pipes") with the specified names.
mkfifo [OPTION] NAME ...
A FIFO is a special file type that permits independent processes to communicate. One process opens the FIFO file for writing, and another for reading, after which data can flow.
-m MODE or --mode=MODE
Set the mode of created FIFOs to MODE, which is symbolic as in chmod and uses a=rw (read and write allowed for everyone) minus the bits set in the umask by default.
mkfifo(3) [derived from man 3 mkfifo]
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo ( const char *pathname, mode_t mode );
mkfifo() makes a FIFO special file with name pathname. mode specifies its permissions, modified by the process's umask in the usual way (mode & ~umask).
A FIFO special file is similar to a normal pipe, except that instead of being an anonymous communications channel, it is entered into the file system by using mkfifo(1) or mkfifo(3). Once you have created a FIFO special file in this way, any process can open it for reading or writing, in the same way as an ordinary file. However, it has to be open at both ends simultaneously before you can proceed to do any input or output operations on it. Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.
The normal, successful return value is 0, or -1 for an error in which case errno is also set.