Regular pipes are the method used to "pipe" the output of one program into the input of another; they are created in memory via a system call and do not exist on any filesystem. Named pipes act like regular pipes but are accessed via a file, called a FIFO special file. Unrelated processes can access this file and communicate.
—Robert Love, Linux System Programming, 2nd Edition, Chapter 1, pp. 14-15.
Pipes and FIFOs provide efficient ways to do file-like input and output between processes on the same host machine. Their biggest benefit is the ability to treat the endpoints as formatted input and output streams, just like you would read and write from files.
In this studio, you will:
pipe()
system call
mkfifo()
function
getline()
function to read a line of data at a time from
a file into a dynamically allocated buffer, and then send it over a FIFO
As a refresher on Linux processes, re-read (skim)
LPI pp. 515-522 and 563-571 and LKD pp. 29-33 and 38-40, and
please run the following commands on the Linux command line (on your Raspberry Pi
or on one of the Linux Lab machines) for more documentation on the
fork
and exec
library functions:
man 2 fork
man 3 exec
You may want to also skim the slides and studio exercises to refresh your understanding of parent and child processes.
Please complete the required exercises below. We encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is allowed.
As you work through these exercises, please record your answers, and when finished upload them along with the relevant source code to the appropriate spot on Canvas. If you work in a group with other people, only one of you should please upload the answers (and any other files requested) for the studio, and if you need to resubmit (e.g., if a studio were to be marked incomplete when we grade it) the same person who submitted the studio originally should please do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
pipe()
system call to create a
program with communicating child and parent processes. Once created, pipes
cannot be shared between unrelated processes. Thus, the standard way to use
this system call is first to call pipe()
, which will
create separate pipe endpoints for reading and writing, and then to call
fork()
, which will create a child process. Thus, the child will
inherit the pipe endpoints from the parent, and both processes now will have
access to the pipe.
On your Raspberry Pi, create a C program that:
pipe()
to create a pipe with read and write file descriptors (see
man 2 pipe
for details)
fork()
to create a child process
close()
, and the parent process should close its copy of the write
file descriptor similarly
Pipe I/O, like file I/O in C, is comparatively low level. There are two
approaches you can use to read from and write to a pipe. You could use
the integer file descriptors returned by pipe()
directly
with the read()
and write()
system calls, which are
documented at man 2 read
and man 2 write
.
However you might find it easier to instantiate a FILE*
stream variable,
which allows you to work with your pipe like it was any other file object (i.e. you
write to the pipe with fprintf()
and read from the pipe with input
operations such as fgets()
or fscanf()
. You can open a
file stream from a file descriptor with the fdopen()
function. You can
Read more about the difference
between file descriptors and file streams here. Of course, all of the above
functions have informative man pages as well.
Hint: Blocking functions that read the pipe will block indefinitely until
they are able to return successfully or the write-end of the pipe is closed by
the writer. The writer should write all of it's data and then call
close()
on its file descriptor when it is done. This allows you to
write code such as while( fgets() != NULL )
to do open-ended reads,
on the pipe, which will eventually fail and return once the write-end is closed.
Build and run your program on your Raspberry Pi. As the answer to this exercise, copy and paste the terminal output that is produced by running your program.
PIPE_BUF
sized data).
To begin, create a new file for your program code. This program should:
mkfifo()
, giving it both read and write permissions (S_IRUSR | S_IWUSR per the table on page 295 of the
LPI textbook). This is documented
at man 3 mkfifo
. Be careful not to confuse this function with
the program
called mkfifo
that is documented at man 1 mkfifo
(which is also the man page you get if you say just man mkfifo
).
Note: if the FIFO already exists, the call to mkfifo()
will
fail and errno
(declared in errno.h
, which you can find in the /usr/include/
directory on the machine on which you are compiling in case you would like to browse through it and the other files it includes, to see the symbols and functions it declares) will be set to EEXIST
- in that case your program should simply continue to the next step.
fopen()
(run the command man 3 fopen
to see return values, parameters, etc. for that call).
while
loop that continually attempts to read from the
FIFO until EOF is read. Whenever something is read, print it to the standard output.
Build and run your program: it will create a FIFO that will appear in the
filesystem. In a separate terminal, write some data into the FIFO and validate
that it is read by your program correctly. For example, if your FIFO is named
my_ao_fifo
, the command echo "my_message" > my_ao_fifo
will insert the string "my_message" into the FIFO. Test your program with larger
amounts of input as well, for example: cat my_file.c > my_ao_fifo
.
As the answer to this exercise, describe how you tested your program and why you think it is working correctly.
fopen( fifo_name, "w" )
. Your active object will not ever
write to this stream, but the fact that it is held open will prevent your
program from automatically quitting.
Next, modify the active object to read from the FIFO with the function
fscanf()
. This allows you to perform formatted input. Your program
should read integer inputs from the pipe, double the input, and then print out the
original and the new values. Your input stream should not have any non-integer characters
and you do not need to handle this case: test your program by passing integers into your FIFO.
Finally for this exercise, we want to write a set of programs that concurrently use the FIFO to request that the active object perform work. Write two programs that
open the write end of the FIFO with fopen(fifo_name, "w")
and
insert integer values with fprintf()
. One of these programs should only insert even numbers,
and the other should only insert odd numbers. Run these programs
long enough to verify that they can both write concurrently to the FIFO without
problem. Examine the output of your active object to verify correctness.
As the answer to this exercise, please explain briefly (and show output fragments as evidence) why you think your program is working correctly based on that output.
Please see the getline man page for different ways to have getline manage buffer allocation, resizing, etc. as needed in case the length of a line from the file (or FIFO).
Test your new FIFO writer program with the active object reader program from the previous exercise, using files with different line lengths. As the answer to this exercise, please explain briefly (and show output fragments as evidence) why you think your program is working correctly based on that output.