How To Communicate Between Processes Through Pipe
Pipe is a one way communication of data between one process to another process. In the form of Interprocess communication (IPC), it is one type of communication. Refer to the tutorial Pipe in Linux before learning this one.
Process creates a new pipe using pipe ( ) system call. It creates a pair of file descriptor: one for read channel and one for write channel. It can read from pipe by read ( ) system call with first file descriptor and can write to pipe by write ( ) system call with second file descriptor. It has half duplex pipes so each process must be closed before using another.
You can get more information of pipe ( ) system call in How to create pipe in Linux tutorial.
Fig. 1: Overview of Communication between Linux Processes
Create Simple Demo Pipe
This is the basic demo tutorial of communication between two processes through pipe. Pipe is the flow of data communication between two processes. It is an example where the parent process reads data from pipe which is input by child process. Before making program refer to the tutorial How to create first C program in Linux.
Pipe ( ) system call creates a pair of file descriptors. First file descriptor reads from pipe and second file descriptor writes in to pipe. These file descriptor may pass through fork ( ) system call.
fork ( ) system call creates new process from current running process. fork ( ) system call returns twice: once from child process and another time from parent process. It returns zero from child process and returns value with child PID from parent process. Fork ( ) creates a new process which shares the pipe with them. It takes no argument and returns process ID. You can learn more about Fork ( ) system call in Process in Linux tutorial.
//*****************************Pipe_demo1.c*********************************//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#define Max_length 1025
int main()
{
char WriteMsg[Max_length];
char ReadMsg[Max_length];
int file_d[2];
int result_pipe;
pid_t child_t;
result_pipe = pipe(file_d);
if(result_pipe < 0)
{
perror(“pipe”);
}
child_t = fork();
if(child_t >= 0)
{
if(child_t == 0)
{
memset (WriteMsg , 0 , sizeof(WriteMsg));
printf (“Enter the Message : “);
fgets (WriteMsg , sizeof(WriteMsg) , stdin);
write (file_d[1] , WriteMsg , strlen(WriteMsg));
exit(0);
}
else
{
memset (ReadMsg , 0 , sizeof(ReadMsg));
read (file_d[0] , ReadMsg , sizeof(ReadMsg));
printf(“Entered message : %sn ” , ReadMsg);
exit(0);
}
}
else
{
perror(“fork”);
exit(2);
}
return 0;
}
//*****************************Pipe_demo1.c*********************************//
Write or copy same code in your Linux text editor and save it as Pipe_demo1.c. You can save the file with any name of your choice.
memset ( ) is C library function which sets the first number of bytes of the block of memory pointed by array to the specified value. Here memset ( ) sets the WriteMsg and ReadMsg array with zero (NULL) value.
Declaration of memset ( ) function:
void *memset (void *str, int value, size_t byte);
str is pointer to the block of memory(i.e. WriteMsg [ ] or ReadMsg[ ]).
value is int value which fills allocated block of memory
byte is first number of byte which we want to set with specific byte.
fgets ( ) takes the data from input terminal and stores it in particular block of memory. In this tutorial, we take input data from input terminal and store it in WriteMsg array and then write into pipe through write ( ) function.
First pipe is created by pipe ( ) system call and it creates a pair of file descriptors. Child process is created by fork ( ) system call and if it’s successful, we can write data into pipe. After doing so, stop the child process and go back to the parent process which returns ID of child process. Parent process can read the message from pipe and display it on terminal screen.
Compiling The Source Code
After writing and checking it for error, next step is ready for compiler.
Compile Pipe_demo1.c with GCC by following line:
gcc Pipe_demo.c
After compilation, GCC will generate executable (runnable) file. Here we haven’t given the name of executable file so compiler automatically gives the a.out name to file. a.out which is an executable file of Pipe_demo1.c and is located in the same directory where source file is saved.
But a good programmer always gives name of executable file using following line instead of upper method:
gcc -o pipedemo1 Pipe_demo1.c
Now GCC will generate executable file named pipedemo1 instead of a.out. Before running the executable file, make sure that the executable file is generated within same directory where source file is located.
Running The Executable File
Type the file name followed by dot and a forward slash and press the ENTER key as following:
./pipedemo1
Following result will print on monitor screen:
Enter the Message : type your input message and press enter key
Entered message : output message
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.