Program for IPC using named pipes (mkfifo())

Program for IPC using named pipes (mkfifo())

The third method for IPC is using mkfifo() function. mkfifo() creates a named pipe which can be used exactly like a file. So, if you know how to read/write in a file this is a convenient method for IPC

Syntax:

#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);

mkfifo() makes a FIFO file with the name specified by pathname and the permissions are specified by mode. On success mkfifo() returns 0 while on error it returns -1.

The entire IPC process will consist of three programs:
Program1: to create a named pipe
Program2: process that will write into the pipe (sender process)
Program3: process that will receive data from pipe (receiver process)


//Program1: Creating fifo/named pipe ( 1.c )

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>        
int main()
        {
               int res;
               res = mkfifo("fifo1",0777); //creates a named pipe with the name fifo1
               printf("named pipe created\n");
        }

//Now compile and run this program.

How it works?

This will simply create a named pipe (fifo1) with read, write and execute permission for all users. You can change this to whatever you prefer

Step 2 is to create a process which will use this pipe to send data. The below program will do that.

//Program2: Writing to a fifo/named pipe ( 2.c )

        #include<unistd.h>
        #include<stdio.h>
        #include<fcntl.h>
        int main()
        {
                    int res,n;
                    res=open("fifo1",O_WRONLY);
                    write(res,"Message",7);
                    printf("Sender Process %d sent the data\n",getpid());
        }

//Compile this program as
//gcc -o 2 2.c
//Note: If you run this you will not see any output

How it works?

The above code opens the pipe created previously in writing mode (because it wants to send data). Then it uses “write” system call to write some data into it. Finally, it prints a message using printf. But when you compile and run it, it won’t run because by default the sender runs in BLOCKING mode which means that until the receiver is not there the sender process gets blocked. Hence, you need a receiver process also.

The third step is to create the receiver process. The below program does so.

//Program 3: Reading from the named pipe ( 3.c )

        #include<unistd.h>
        #include<stdio.h>
        #include<fcntl.h>
        int main()
        {
                    int res,n;
                    char buffer[100];
                    res=open("fifo1",O_RDONLY);
                    n=read(res,buffer,100);
                    printf("Reader process %d started\n",getpid());
                    printf("Data received by receiver %d is: %s\n",getpid(), buffer);
        }

//Compile the program as
// gcc -o 3 3.c

How it works?

This program connects to the pipe in reading mode and reads the data into buffer and prints it. But again this program will not run. Because the receiver is BLOCKED until the sender is there.

Therefore, run both the object files simultaneously as
$./2 & ./3
and you will see the output as

Program for IPC using named pipes (mkfifo())
Named pipes

Viva Questions on Program for IPC using named pipes (mkfifo())

Q1. Is named pipe bidirectional or unidirectional pipe?
Q2. What is the default mode of a named pipe : blocking or non-blocking?
Q3. What is meant by Blocking-Send?
Q4. Can a process create two named pipes for communication with two different processes?

Relevant Programs on IPC using

Leave a Reply

Your email address will not be published. Required fields are marked *