Communicate between two processes using PIPE – Network programming

Posted on November 27, 2014 by

A pipe is a system call that creates a unidirectional communication link between two file descriptors. The pipe system call is called with a pointer to an array of two integers. Upon return, the first element of the array contains the file descriptor that corresponds to the output of the pipe (stuff to be read). The second element of the array contains the file descriptor that corresponds to the input of the pipe (the place where you write stuff). Whatever bytes are sent into the input of the pipe can be read from the other end of the pipe.

Code

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
	char buffer[20];
	int pfd[2];
	if(pipe(pfd) == -1){
		printf("Pipe Creation Failed"); 
		exit(0);
	}
	else printf("Pipe Created Successfully\n");

	printf("Writing to pipe\n");
	write(pfd[1],"Hello World",11);
	
	printf("Reading from pipe to buffer\n");
	read(pfd[0],buffer,11);
	
	printf("Buffer : %s\n",buffer);
}

Raw Code

This website is made possible by displaying online advertisements to our visitors.
Please consider supporting by disabling your ad blocker.
loading comments...