Create named Pipe in Linux
Here I will explain library function of named pipe and how to create FIFO or named pipe in C programming. Refer to the tutorial FIFO or named pipe in Linux before learning this tutorial where I had explained how to create FIFO from command terminal in FIFO or named pipe in Linux.
mkfifo ( ) function
mkfifo ( ) function creates FIFO or named pipe.
Synopsis of function is:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo (const char *pathname, mode_t mode);
It is similar to a pipe but created in different ways. Here mkfifo ( ) function creates the file in file system. mkfifo ( ) makes a file named pathname. Pathname is the location of FIFO file where it is created. Once you create the file, you can read or write it as defined in mode user permission what user wants to do with FIFO file.
When FIFO file is created successfuly, mkfifo ( ) returns zero and upon error it will return -1.
mkfifo ( ) returns -1 due to one of the following errors:
EACCES – This error will occur if search permission isn’t allowed to any directories in pathname.
EDQUOT –The user’s quota of disk blocks or inodes on the file system has been fully used (no more available or create).
EEXIST – This error will occur if pathname already exists.
ENAMETOOLONG – This error will occur when the total length of pathname is greater than PATH_MAX, or a single filename has a length greater than NAME_MAX.
ENOENT – This error will occur if a directory component in pathname does not exist or is a dangling symbolic link.
ENOTDIR – This error will occur if directory in pathname is not correct or not present.
EROFS – This error will occur if pathname refers to a read-only file system.
mknod ( ) function
The system call mknod( ) creates a file system node as named pipe. You can create file system node like file, device file, named pipe, socket etc. by mknod ( ) system call.
Synopsis of function is:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int mknod (const char *pathname, mode_t mode, dev_t dev);
The system call mknod ( ) creates file system node as specified pathname with attribute described in mode and dev. Once you have created the file, you can read or write as defined in mode. which seeks user permission as to what user wants to do with FIFO file. dev define the types of file system node (i.e. file, device file, socket, named pipe). The file type must be one of S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO, or S_IFSOCK to specify a regular file, character file, device block file, named pipe and socket respectively.
When FIFO file is created successfully, mknod ( ) returns zero and upon error it will return -1.
mknod ( ) returns -1 due to one of the following errors:
EACCES – This error will occur if search permission isn’t allowed to any directories in pathname.
EDQUOT –The user’s quota of disk blocks or inodes on the file system has been fully used (no more available or create).
EEXIST – This error will occur if pathname already exists.
ENOENT – This error will occur if a directory component in pathname does not exist or is a dangling symbolic link.
EFAULT – This error will occur if pathname points outside your accessible address space in memory.
EINVAL – This error will occur if mode request creates something other than a regular file, device special file, FIFO or socket.
ELOOP – This error is an indication of too many symbolic links encountered in resolving pathname.
ENAMETOOLONG – This error will occur if pathname was too long.
ENOMEM – This error will occur if sufficient kernel memory was not available.
ENOSPC – This error will occur if the device containing pathname has no more space for creating the new node.
ENOTDIR – This error will occur if directory in pathname is not correct or not present.
EROFS – This error will occur if pathname refers to a file on a read-only file system.
Let’s see how to create file in C language using the system call mkfifo ( ).
//************************************fifo.c**********************************//
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
int main()
{
int result;
result = mkfifo (“/home/ashish/EG/pipe_fifo_by_c” , S_IRUSR | S_IWUSR );
if(result == 0)
{
printf (“FIFO is successfully created…..n”);
}
else
{
if (errno == EEXIST)
{
printf (“Sorry!!! FIFO is already exist..n”);
}
else
{
printf(“FIFO is not createdn”);
perror(“FIFO error :n”);
exit(1);
}
}
}
//************************************fifo.c**********************************//
Save the file in directory and compile it. After successful compilation, run the executable file from command terminal. You may refer to the tutorial How to make first C program in Linux if you are not aware of compilation and execution process.
Here the system call mkfifo ( ) creates the named pipe file specified by pathname. In pathname, pipe_fifo_by_c is name of created named pipe file. S_IRUSR and S_IWUSR are attributes defining the permission of read and write to user.
Upon successful execution of mkfifo ( ), it will return zero and the following line will be printed on screen:
FIFO is successfully created…..
If not created successfully, it will return -1 and indicate the error occurred in creation of named pipe. Specific error type is returned by errno which is located in errno.h file. Here I have mentioned one error type in program as EEXIST. If FIFO file pipe_fifo_by_c is already created, it will be displayed on screen as follows:
Sorry!!! FIFO already exists..
If some other error will occur instead of EEXIST, it will be displayed on screen:
FIFO is not created
Here perror ( ) produces the message of error occurrence from the specific system call.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.