Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

How to create named Pipe in Linux (Part 17/24)

By Ashish Vara

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 Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics Research robot samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Please help to identify the signal input port on board
  • Hotplugging UART
  • RF switch selection
  • sensitivity in filters and number of bits of system
  • MDO4104-6 Front Panel Not Working

RSS Electro-Tech-Online.com Discussions

  • software PWM
  • Combination of Wires of two DC motor giving Output current.
  • Passthrough charging-simple but impossible to achieve?
  • geared motor to revolve oval platform
  • None existant errors (MPLAB X)
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering