The program as it is generates:
>Parent1
>Parent2
>In Child
>Parent3
Successfull...
If I remove the comment from line 31 - just below "//THE PROBLEM:" so the "gets(buff);" is executed it gives:
>Parent1
>Parent2
and... waits (both parent and child processes running - both waiting).
I would like to be able to read from pipe in child so I know what parent has sent.
How to do this so it does not "wait forever"?? Please help...
int main(char* argv, char** argc)
{
int toChild[2], fromChild[2];
pid_t pid;
if(pipe(toChild) != 0){
perror("pipe() toChild");
exit(1);
}
if(pipe(fromChild) != 0){
perror("pipe() fromChild");
exit(1);
}
if((pid = fork()) == -1)
{
perror("fork");
return 1;
}
if(pid == 0)
{/* CHILD */
close(toChild[1]); //child won't write to child... :)
close(fromChild[0]); //child won't read it's own messages
dup2(toChild[0], STDIN_FILENO);
dup2(fromChild[1], STDOUT_FILENO);
char buff[5];
//THE PROBLEM:
//gets(buff);
puts(">In Child");
return 0;
}
else
{/* PARENT */
close(toChild[0]); //paren't won't read his own messages...
close(fromChild[1]); //paren't won't write to parent... :)
//create streams:
FILE *toChildStream;
FILE *fromChildStream;
if ((toChildStream = fdopen(toChild[1], "w")) == NULL)
return 1; //error: could not create stream
if ((fromChildStream = fdopen(fromChild[0], "r")) == NULL)
return 1; //error: could not create stream
puts(">Parent1");
fputs(">Some text", toChildStream); //parent writes to child
puts(">Parent2");
char buff[10];
while ( fgets(buff, 10, fromChildStream) != NULL) //read from child
puts(buff);
puts(">Parent3");
}
printf("\n\nSuccessfull...");
return 0;
}