EmbLogic's Blog

Pipe(a brief introduction)

What is pipe in our day to day life?

Pipe is basically a connector to send any thing from one place to another.

It has same meaning in Linux C. We create pipe to connect data flow from one process to another. It is a very paramount aspect of IPC – Inter Process Communication.Pipe is half duplex.

To create a simple pipe with C, we make use of the pipe() system call. It takes a single argument, which is an array of two integers, and if successful, the array will contain two new file descriptors to be used for the pipeline.

the prototype of pipe function is:

#include<unistd.h>

int pipe(int fd[2]);

After creating a pipe, the process typically spawns a new process . The first integer in the array (element 0) is set up and opened for reading, while the second integer (element 1) is set up and opened for writing. Once we have established the pipeline, we then fork our new child process:

If the parent wants to receive data from the child, it should close fd1, and the child should close fd0. If the parent wants to send data to the child, it should close fd0, and the child should close fd1. Since descriptors are shared between the parent and child, we should always be sure to close the end of pipe we aren’t concerned with. On a technical note, the EOF will never be returned if the unnecessary ends of the pipe are not explicitly closed.

The real advantage of pipe is when you wish to pass data between two processes which have any ancestoral relation.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>