Lab 10 Preparation
Read Section 15.5 on pages 514 to 518 in Stevens/Rago to prepare for Lab 10 on FIFOs (named pipes). Named pipes are often called FIFOs: First-In, First-Out; because whatever is written into one "end" of the pipe is read out from the other "end" in the same order.
Pipes (there's another kind of pipe that we'll look at soon) have 2 ends. You are already used to pipes on the command line, in examples such as:
grep "some reg ex here" one or more files | less
where you are piping the output (stdout here) of one process into the input (stdin) of another. In this example, grep is the process on one end of the pipe, and the less program is the process on the other end. Each time that grep writes a line to its stdout, less reads it from its stdin.
For this week's exercise, you will use named pipes to pass information between processes, similar to the above. FIFOs look like files in the file system. They are not. The entry in the file system is simply a convenient way for each process to find the FIFO for passing data between processes that are otherwise unrelated. The other kind of pipe is used between "relatives", parent and child processes.
You create the file system entry using mkfifo(1) or mkfifo(3). Because it looks just like a file, you must also give it file permissions. Now a process can open it for either input or output. The second process had better open it in the opposite direction, or nothing much is going to happen! Because it looks like a file, you can also use your FIFO from the command line.
This will write to a FIFO named fifoname, created by mkfifo -m 600 fifoname, perhaps:
cat some.file > fifoname &
or even
echo "something to look at" > fifoname &
And this will read from the same FIFO:
cat < fifoname &
Both have been put in the background. Be sure you understand why. Try similar commands from two different xterm windows without putting them in the background. Does that work the same way? Remember that you must have a process at each end, and not just a file, and that both processes have to be running at the same time for anything to travel through the FIFO.
Creating a FIFO (it's removed like a normal file) from the shell:
prw------- 1 <user> <group> 0 <date <time> fifoname
Note that in the ls -l output the file type is shown as p (for pipe), and that the file size is 0. The file size will always be 0, because there is nothing there! There is only a directory entry for the name and an i-node for the permissions. No data block is every allocated, and no data is ever kept. When a FIFO is opened, a buffer is allocated -- essentially it's the same buffer for the reading end of the FIFO and the writing end. The operating system manages who can read/write when, and where in the buffer it comes from/goes to.
When you have familiarized yourself with FIFO use from the shell, to understand how they can be used, proceed to writing your two small programs. Be sure to keep them simple -- you only have two hours in the lab.