hi all
I am trying to be much familier with pipes
I tried to write a c program that generates the following output
child will print the 0th character and send it to parent
a : from child .. waiting for parent to print same character
a : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
b : from child .. waiting for parent to print same character
b : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
c : from child .. waiting for parent to print same character
c : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
d : from child .. waiting for parent to print same character
d : from parent .. waiting for next character from child
child will print the 0th character and send it to parent
e : from child .. waiting for parent to print same character
e : from parent .. waiting for next character from child
but what I get was this :(
child will print the 0th character and send it to parent
a : from child .. waiting for parent to print same character
a : from parent .. waiting for next character from child
and here is my code
Code:#include<stdio.h>
#include<string.h>
int main (void)
{
int fd[2],nbytes,i;
int childpid;
char *f1[5]={"a","b","c","d","e"};
char f2[2];
pipe(fd);
childpid=fork();
if(childpid==0)//start child process
{
for(i=0;i<5;i++)
{
printf("child will print the %dth character and send it to parent\n",i);
printf("%s : from child .. waiting for parent to print same character\n",f1[i]);
close(fd[0]);
write(fd[1],f1[i],(strlen(f1[i])+1));
exit(0);
}
}
else
{
wait(NULL);
close(fd[1]);
nbytes=read(fd[0],f2,sizeof(f2));
printf("%s : from parent .. waiting for next character from child\n",f2);
}
return 0;
}
I need your help to correct my code and thanx ;)
