Search

Inter process communication using named pipes(FIFO) in linux

In the last post we saw the how to create pipes for communication between processes in linux.

One of the major disadvantage of pipes is that the they can not be accesed using their names by any other process other than child and the parent as they do not get listed in the directory tree.

The work around for this porblem is to create a named pipe which is also called as a FIFO, which stands for First in First out, meaning the data that is written into the pipe first will be read out first always.

The fifos get listed in the directory tree and any process can access it using its name by providing the approproiate path.

fifo are created using the function mkfifo() which takes as arguments

1. The name of the fifo that has to be created
2. The permissions for the file.

Once the file is created, it needs to be opened using the system call open() and the data can be read and written from the file using read() and write system calls.

One of the examples you can think of using a named pipe is communication between a server and a client. If ther are two fifos one of the server and the other of the client, then the client can send request to the server on the server fifo which the server will read and respond back with the reply on the client's fifo.

Another advantage of a fifo over the pipes is that fifo are birectoinal, that is the same fifo can be read from as well and written into.

Here is an example the demostrates the working of fifos.

First we will create two fifos one called the server and the other called the client.

create_fifo.c:



Save the above program as create_fifo.c and compile is using gcc as follows



If the compilation is successfull, run the program as follows



If there are no errors, then two fifo should have got created in your current working directory. To confirm this run the command the "ls -l" in your current workind directory and you should see some thing like this.



If you notice the first column of the row containing fifo_server and fifo_client, you can see the letter "p" which signifies that it is a pipe.

Now to use these pipes, we will write two program's one to represent the server and other to represent the client. The server will read from the pipe fifo_server to which the client will send a request. On reciveing the request, the server will send the information to the client on fifo_client.

server.c



The above code,server.c, reads the choice from fifo_server to which the client writes and depending on the request,the server responds with the relevant data by writing to fifo_client.

Save the file and server.c and compile it as follows



client.c:



The above code,client.c, sends a request to the server by writing to the pipe fifo_server, and recieves the reply from the server by reading the pipe fifo_client.

Save the file and server.c and compile it as follows



To see the pipe in operation, open two terminals and go the folder where the pipes have been created.

Note: The server.c and client.c codes assume that the pipes exist in the same directory as the executables of server and client are, if they exist in any other directory you will have to provide the path to the same in the system call open().

Terminal 1:



The terminal will go into a wait state with the cursor blinking, waiting for request from client.

Terminal 2:



The client will prompt to make a choice of request to be sent to the server, enter number 1,2 or 3



This number will be sent to the server and the client will go into a wait state, waiting for the server to respond.

After a few seconds you should see the reponse from the server being printed on the client terminal.

Note: wait for 10 seconds for the output to appear as the server has a sleep for 10 seconds, this is required to allow some time for the client to start its read operation on the fifo_client pipe.

for eg if we entered option 2, the response would be

output on client terminal:



Ouput on server terminal :



Thus we were able to communicate between the two processes using the fifos, even though the pipes were not created by them.

To use the pipes with out writing the server and client code we can use "cat" and "echo" commands.

Terminal 1:



Terminal 2:



We should be able to see the "hello world" being printed on the Terminal 1.

Follow by Email