-Pipes are basically a tunnel to pass information from one process to another. -When u create a pipe a memory is allocated in RAM viz. is accessible through its read and write file descriptor.
-Pipes are of 64k and works just like a circular queue.
To understand its working : Assume it like a tube that is connecting to two processes. There is one way sending of data at a time.
* Pipes have two file descriptor one is read file descriptor and another one write file descriptor Which basically used for reading the data from one file to another file and vice-versa.
* Pipes are half Duplex, where one can receive or send at a time only.
For bater understanding about pipe read these examples:->
Example no. 1.:
#include<stdio.h> #include<unistd.h> int main() { int fp[2],fp1[2],ret,ret1; ret = pipe(fp); ret1 = pipe(fp1); printf("fdr :=%d\nfdw:= %d\n",fp[0],fp[1]); printf("ret :=%d\n",ret); printf("fdr1 :=%d\nfdw:= %d\n",fp1[0],fp1[1]); printf("ret1 :=%d\n",ret1); return 0; }
Example no. 2:
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> int main() { int fp[2],ret,countr,countw; char buf[20]="my name is ankit",buf1[20]; ret = pipe(fp); printf("fdr :=%d\nfdw:= %d\n",fp[0],fp[1]); printf("ret :=%d\n",ret); countw=write(fp[1],buf,strlen(buf)); if(countw==-1) { perror("write"); EXIT_FAILURE; } countr=read(fp[0],buf1,20); if(countr==-1) { perror("read"); EXIT_FAILURE; } printf("countw :=%d\ncountr:= %d\n",countw,countr); printf("data :=%s\n",buf1); return 0; }
Above example shows : * Generation of pipe or how to create a pipe. *File descriptor given by pipe. *And how to write into the pipe and how to read from the pipe