Chapter Contents

Previous

Next
mkfifo

mkfifo



Create FIFO Special File

Portability: POSIX.1 conforming


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <sys/stat.h>

int mkfifo(char *pathname, mode_t mode);


DESCRIPTION

mkfifo creates a new USS FIFO special file. pathname is the special filename, and mode is the set of file permission bits for the new FIFO file. The owner ID of pathname is set to the effective user ID of the process. The group ID of pathname is set to the owning directory's group ID.


RETURN VALUE

mkfifo returns 0 if it is successful and -1 if it is not successful.


EXAMPLE

This example creates a FIFO and uses it to read and print a message from itself. Note that this example depends on the length of the message being less than the POSIX PIPE_BUF constant. A longer message causes this example to deadlock:

/* This program must be compiled with the posix option */

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

main()
{
   int rc;
   int fifofd;
   char input[_POSIX_PIPE_BUF];
   rc = mkfifo("named.pipe", S_IRUSR | S_IWUSR);
   if (rc != 0) {
      perror("mkfifo failure");
      exit(EXIT_FAILURE);
   }
   /* Open the FIFO for read. */
   fifofd = open("named.pipe", O_RDONLY);
   if (fifofd < 0){
      perror("open failure");
      remove("named.pipe");
      exit(EXIT_FAILURE);
   }
   rc = system("echo >named.pipe "
               "Talking to yourself is educational!");
   if (rc != 0) {
      fprintf(stderr, "echo failed with status code %d\n", rc);
      remove("named.pipe");
      exit(EXIT_FAILURE);
   }
   rc = read(fifofd, input, _POSIX_PIPE_BUF);
   if (rc < 0) {
      perror("read failure");
      remove("named.pipe");
      exit(EXIT_FAILURE");
   }
   puts("Something I just read:");
   fwrite(input, 1, rc, stdout);   /* Read input from the FIFO. */
   close(fifofd);
   remove("named.pipe");
   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

creat , mkdir , mknod , pipe , umask


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.