next up previous contents
Next: 6.3.3 FIFO Operations Up: 6.3 Named Pipes (FIFOs Previous: 6.3.1 Basic Concepts

6.3.2 Creating a FIFO

There are several ways of creating a named pipe. The first two can be done directly from the shell.

        mknod MYFIFO p
        mkfifo a=rw MYFIFO

The above two commands perform identical operations, with one exception. The mkfifo command provides a hook for altering the permissions on the FIFO file directly after creation. With mknod, a quick call to the chmod command will be necessary.

FIFO files can be quickly identified in a physical file system by the ``p'' indicator seen here in a long directory listing:

        $ ls -l MYFIFO
        prw-r--r--   1 root     root            0 Dec 14 22:15 MYFIFO|

Also notice the vertical bar (``pipe sign'') located directly after the file name. Another great reason to run Linux, eh?

To create a FIFO in C, we can make use of the mknod() system call:


  LIBRARY FUNCTION: mknod();

  PROTOTYPE: int mknod( char *pathname, mode_t mode, dev_t dev);
    RETURNS: 0 on success,
             -1 on error: errno = EFAULT (pathname invalid)
                                  EACCES (permission denied)
                                  ENAMETOOLONG (pathname too long)
                                  ENOENT (invalid pathname)
                                  ENOTDIR (invalid pathname)
                                  (see man page for mknod for others)

  NOTES: Creates a filesystem node (file, device file, or FIFO)

I will leave a more detailed discussion of mknod() to the man page, but let's consider a simple example of FIFO creation from C:

                mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);

In this case, the file ``/tmp/MYFIFO'' is created as a FIFO file. The requested permissions are ``0666'', although they are affected by the umask setting as follows:

                final_umask = requested_permissions & ~original_umask

A common trick is to use the umask() system call to temporarily zap the umask value:

                umask(0);
                mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);

In addition, the third argument to mknod() is ignored unless we are creating a device file. In that instance, it should specify the major and minor numbers of the device file.



Converted on:
Fri Mar 29 14:43:04 EST 1996