/MY PROGRAM ARE COMPILED IN FEDORA15 ..WITH 4.6 gcc compiler...../
/*A PROGRAM TO CREATE PIPE TO COMMUNICATE BETWEEN TWO PROCESS*/ #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<unistd.h> #include<string.h> int main() { int ret; int fd[2];//take an array whose lowest possible no. i.e.fd[0] is read from one end of pipe& other fd[1] will alwys be for write on other end pipe ret=pipe(fd);//pipe will be created if(ret==0) printf("pipe created\n"); return 0; }
/*PROGRAM SHOWING HOW TO USE PIPE*/ #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<unistd.h> //#include<string.h> int main() { int ret,count,i; char buff[3]; char b=0; int arr; //b=malloc(sizeof(char)*3); int fd[2]; ret=pipe(fd); if(ret==0) printf("pipe created\n"); buff[0]= 2;buff[1]=3;buff[2]=4;//put values in buffer printf("buff[0]=%d buff1]=%d buff[2]=%d\n",buff[0],buff[1],buff[2]); count=write(fd[1],buff,3);//here is no need to open pipe specially using open system call like fifo printf("count1=%d\n",count); //ret=pipe(fd); i=0; while(i<3) { i++; arr=fd[0]; count=read(arr,&b,1);//read from pipe one by one //for(i=0;i>-15;i--) printf("count=%d =%d\n",count,b);//print 2 3 4 with %d ...i.e.collection of characters..not string...dnot try with %s }