Thank you guys..... I got it fixed.
IT was happening cuz of the statement fclose(fp2) which i had written at the end of the parent process. As execution would never reach that place, i was having this problem.
The solution is we have to use the above mentioned statement at the end of the child process.
here goes the correct code.....
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int pfd[2],i=0,j=0;;
pid_t cpid;
char buf[50],c;
FILE *fp1,*fp2;
int ch;
fp1=fopen("source.c","r+");
if(fp1==NULL)
{
perror("Error opening source file\n");
}
while((ch=getc(fp1))!=EOF)
{
buf[i]=ch;
i++;
}
buf[i] = '\0';
fclose(fp1);
if (pipe(pfd) == -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0)
{ /* Child reads from pipe */
close(pfd[1]); /* Close unused write end */
fp2=fopen("dest.c","w+");
if(fp2==NULL)
{
perror("Error opening dest file\n");
_exit(1);
}
int i = 0;
while (buf[i]!='\0')
{
read(pfd[0], &buf[i], sizeof(buf[i]));
c=buf[i];
putc(c,fp2);
i++;
}
fclose(fp2);
close(pfd[0]);
_exit(EXIT_SUCCESS);
}
else
{ /* Parent writes to pipe */
j=0;
close(pfd[0]); /* Close unused read end */
while(buf[j]!='\0')
{
write(pfd[1], &buf[j], sizeof(buf[j]));
j++;
}
close(pfd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}
cheers
afser