gtpc2m4sC/C++ Language Support User's Guide

mkfifo-Make a FIFO Special File

This function creates a FIFO special file. A FIFO special file is a file that is typically used to send data from one process to another so that the receiving process reads the data in first-in-first-out (FIFO) format. A FIFO special file is also known as a named pipe.

Format

#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *path, mode_t mode)

path
The name of the new file.

mode
Specifies the file permission bits of the new file.

This function creates a new file called path. The file permission bits in mode are modified by the file creation mask of the process and then used to set the file permission bits of the pipe being created. For more information about the file creation mask, see umask-Set the File Mode Creation Mask; for information about the file permission bits, see chmod-Change the Mode of a File or Directory.

Note:
Because you cannot search or execute a FIFO special file, the mkfifo function removes the search and execute permission bits if you specify them.

The user ID of the new file is set to the effective user ID of the process. If the S_ISGID mode bit is set to OFF in the parent directory, the group ID of the new file is set to the group ID of the process; otherwise, the group ID of the new file is set to the group ID of the parent directory.

When it has completed successfully, the mkfifo function updates the st_mtime field in the new file and also updates the st_mtime field of the directory that contains the new file.

TPF deviation from POSIX

The TPF system does not support the atime (access time) or ctime (change time) time stamp.

Normal Return

A value of 0.

Error Return

A value of -1 and errno is set to one of the following:

EACCES
Search permission is denied on a component of path, or write permission is denied on the parent directory of the file to be created.

EEXIST
A file by that name already exists.

ELOOP
A loop exists in symbolic links. This error is issued if the number of symbolic links found while resolving the path argument is greater than POSIX_SYMLOOP.

ENAMETOOLONG
path is longer than PATH_MAX characters or some component of path is longer than NAME_MAX characters. For symbolic links, the length of the path name string substituted for a symbolic link exceeds PATH_MAX.

ENOENT
A component of path was not found or no path was specified.

ENOMEM
There is not enough storage space available.

ENOSPC
The directory or file system that was intended to hold a new file does not have enough space.

ENOTDIR
A component of path is not a directory.

Programming Considerations

Examples

The following example uses the mkfifo function to create a FIFO special file called temp.fifo and then writes and reads from the file before closing it.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
 
main() {
  char fn[]="temp.fifo";
  char out[20]="FIFO's are fun!", in[20];
  int rfd, wfd;
 
  if (mkfifo(fn, S_IRWXU) != 0)
    perror("mkfifo() error");
  else {
    if ((rfd = open(fn, O_RDONLY|O_NONBLOCK)) < 0)
      perror("open() error for read end");
    else {
      if ((wfd = open(fn, O_WRONLY)) < 0)
        perror("open() error for write end");
      else {
        if (write(wfd, out, strlen(out)+1) == -1)
          perror("write() error");
        else if (read(rfd, in, sizeof(in)) == -1)
          perror("read() error");
        else printf("read '%s' from the FIFO\n", in);
        close(wfd);
      }
      close(rfd);
    }
    unlink(fn);
  }
}   

Related Information

See TPF Concepts and Structures for more information about special files.

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.