Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
Sets the access, change, and modification times for the new file. It also sets the change and modification times for the directory that contains the new file.
mkfifo() creates a new FIFO special file, pathname. The file permission bits in mode are changed by the file creation mask of the process, and then used to set the file permission bits of the FIFO file being created. If pathname contains a symbolic link, mkfifo() fails. For more information on the file creation mask, see umask() — Set and retrieve file creation mask; for information about the file permission bits, see chmod() — Change the mode of a file or directory.
The owner ID of the FIFO file is set to the effective user ID of the process. The group ID of the FIFO file is set to the group ID of the owning directory. pathname cannot end in a symbolic link.
If successful, mkfifo() returns 0.
⁄* CELEBM17
This example uses mkfifo() to create a FIFO specail file named
temp.fifo and then writes and reads from the file before closing it.
*⁄
#define _POSIX_SOURCE
#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);
}
}
read 'FIFO's are fun!' from the FIFO