ptg10805159
Section 15.5 FIFOs 553
#include <sys/stat.h>
int mkfifo(const char *path,mode_tmode);
int mkfifoat(intfd,const char *path,mode_t mode);
Both return: 0 if OK,−1 on error
The specification of the mode argument is the same as for the open function
(Section 3.3). The rules for the user and group ownership of the new FIFO arethe same
as we described in Section 4.6.
Themkfifoatfunction is similar to themkfifofunction, except that it can be used
to create a FIFO in a location relative to the directory represented by thefd file
descriptor argument. Like the other*atfunctions, thereare three cases:
- If thepathparameter specifies an absolute pathname, then thefdparameter is
ignored and themkfifoatfunction behaves like themkfifofunction. - If thepathparameter specifies a relative pathname and thefdparameter is a
valid file descriptor for an open directory,the pathname is evaluated relative to
this directory. - If thepathparameter specifies a relative pathname and thefdparameter has the
special valueAT_FDCWD,the pathname is evaluated starting in the current
working directory,andmkfifoatbehaves likemkfifo.
Once we have usedmkfifoormkfifoatto create a FIFO, we open it usingopen.
Indeed, the normal file I/O functions (e.g.,close,read,write,unlink)all work
with FIFOs.
Applications can create FIFOs with themknodandmknodatfunctions. Because POSIX.1
originally didn’t includemknod,themkfifofunction was invented specifically for POSIX.1.
Themknodandmknodatfunctions areincluded in the XSI option in POSIX.1.
POSIX.1 also includes support for themkfifo( 1 )command. All four platforms discussed in
this text provide this command. As a result, we can create a FIFO using a shell command and
then access it with the normal shell I/O redirection.
When weopenaFIFO, the nonblocking flag (O_NONBLOCK)affects what happens.
•Inthe normal case (withoutO_NONBLOCK), anopenfor read-only blocks until
some other process opens the FIFO for writing. Similarly,anopenfor write-
only blocks until some other process opens the FIFO for reading.
•IfO_NONBLOCKis specified, anopenfor read-only returns immediately.But an
openfor write-only returns−1witherrnoset toENXIOif no process has the
FIFO open for reading.
As with a pipe, if wewriteto a FIFO that no process has open for reading, the signal
SIGPIPEis generated. When the last writer for a FIFO closes the FIFO, an end of file is
generated for the reader of the FIFO.
It is common to have multiple writers for a given FIFO. This means that we have to
worry about atomic writes if we don’t want the writes from multiple processes to be