Named Pipe FIFO Sender Receiver

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 FIFO File
You can create a FIFO with the mkfifo(1) command or the mkfifo(3) function call. From the command line, issue
mkfifo [-m mode] fifoname The mode flags can be specified just as in chmod(1), or they will be set as 0666 (octal) or a=rw less your umask value. The mkfifo(3) function call is similar to chmod(2), and uses the include files <sys/stat.h> and <sys/types.h>:
int mkfifo(const char *name, mode_t mode) It returns 0 unless there is an error, in which case it returns –1 and sets errno.

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.

  • int open(const char *pathname, int flags);
  • int read(int fd, void *buf, size_t count);
  • int write(int fd, const void *buf, size_t count);
  • int close(fd);

Named Pipe FIFO Sender Receiver Example

sender.c

receiver.c