/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/*A file in which a single pipe is created*/
//sometimes before finishing even parent...the process that is replaced in child is returning zero to main...on execution....so terminal prints command line ..& got ready to take another command...but parent runs further on...also returns zero..but command line on terminal is already is printed
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
int main()
{
int ret,fd[2];
char wr[4],rd[4];
ret=pipe(fd);
if(ret==0)
printf("pipe is created\n");
sprintf(wr,"%d",fd[1]);
sprintf(rd,"%d",fd[0]);
printf("rd=%s wr=%s\n ",rd,wr);
ret=fork();
if(ret==0)
{
printf("in child\n");
execl("./file1","file1",rd,wr,NULL);
printf("done\n");
}
else
{
sleep(1);
printf("in parent\n");
execl("./file2","file2",rd,wr,NULL);
printf("done\n");
}
return 0;
}
//By using pipes .....we can make sooo many files to communicate each other........
/* file1 */ #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<string.h> #include<unistd.h> int main(int argc,char* argv[]) { int fd,ret,count,wfd; char chat[17]; char buff[11]="i m indian"; //printf("buff=%s\n",buff); printf("argv[1]=%s\n",argv[1]); printf("argv[2]=%s\n",argv[2]);\ sscanf(argv[1],"%d",&fd); sscanf(argv[2],"%d",&wfd); printf("fd=%d wfd==%d\n",fd,wfd);//now i have read & write descripters......so i may read & write into it......any other file who have these read or write descriptors...may read or write data into it....so these read & write descriptors are ..keys of that pipe into which somedata is written.. ret=fork(); if(ret==0) { count=read(fd,chat,17); printf("count %dbytes=%s\n",count,chat); } else { sleep(1); count=write(wfd,buff,11); printf("count=%d\n",count); } return 0; }