Use of Named Pipes in a C Program in Linux


A named pipe is really just a special kind of file (a FIFO file) on the local hard drive. Unlike a regular file, a FIFO file does not contain any user information. Instead, it allows two or more processes to communicate with each other by reading/writing to/from this file.

Creating a FIFO File
The easiest way to create a FIFO file is to use the mkfifo command. This command is part of the standard Linux utilities and can simply be typed at the command prompt of your shell. You may also use the mknod command to accomplish the same thing. This command requires an additional argument but otherwise it works pretty much the same. To learn more about these commands check out the man pages on them (just type man mkfifo or man mknod at the shell command prompt). The following example shows one way to use the mkfifo command:

prompt> mkfifo /tmp/myFIFO

That's it! It's pretty simple to set a file up for FIFO. There is also a system call that allows you to do this so you can put it in a program. You can read about that on your own if you would like.

Using a FIFO File
Since this named pipe looks like a file, you can use all the system calls associated with files to interact with it. In particular, you can use the open, read, write, and close system calls. The following are prototypes for each of these calls as you will need to use them.

To understand better what each of the return values and parameters are for, check each of these system calls out by looking at their respective man pages. Be sure to use the 2nd level of the man pages (type man 2 open, for example).

To give you an idea of how all of these can work together, here is a breif snippet showing how a sender might set up a connection and send a message.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define MAX_LINE 80

int main(int argc, char** argv) {
   char line[MAX_LINE];
   int pipe;

   // open a named pipe
   pipe = open("/tmp/myFIFO", O_WRONLY);

   // get a line to send
   printf("Enter line: ");
   fgets(line, MAX_LINE, stdin);

   // actually write out the data and close the pipe
   write(pipe, line, strlen(line));

	// close the pipe
   close(pipe);
   return 0;
}

Reading from the pipe is very similar. You need to open it up, use the read call to read information, and then close the pipe when everything is finished. More details on this process can be found on-line or by browsing various man pages. Please note that reading and writing to named pipes are blocking in nature. For example, if a process writes to a named pipe, it will get blocked until there is process willing to read that pipe and vice versa.

Check out the Linux Documentation Project website for additional information, specially the Linux programmer's guide available at LDP website and also the course website.


This help document is adapted after some modifications from an academic website