Missed a few lines of code...
int pfd1[2],pfd2[2];
char buf[5];
...
pipe(pfd1);
pipe(pfd2);
...
if ( ( pid=fork())==0 ) {
// Child code
// Duplicate STDIN to pipe
close(0);
dup2(pfd1[0],0);
close(pfd1[0]);
close(pfd1[1]);
// Duplicate STDOUT to pipe
close(1);
dup2(pfd2[1],1);
close(pfd2[0]);
close(pfd2[1]);
// Execute process
execlp("enfant","enfant",0
_exit(0);
}
// Parent code...
// Close the "other" ends of the pipes - they are not needed.
close(pfd1[0]);
close(pfd2[1]);
// Write to child
write(pfd1[1],"12",2);
// Read from child
read(pfd2[0],buf,5);
...
// When child is finished
// Close the pipes.
close(pfd1[1]);
close(pfd2[0]);
Main Topics
Browse All Topics





by: zebadaPosted on 2002-11-25 at 10:31:24ID: 7495142
Try this...
int pfd1[2],pfd2[2];
...
pipe(pfd1);
pipe(pfd2);
...
if ( ( pid=fork())==0 ) {
/* Duplicate STDIN to pipe */
close(0);
dup2(pfd1[0],0);
close(pfd1[0]);
close(pfd1[1]);
/* Duplicate STDOUT to pipe */
close(1);
dup2(pfd2[1],1);
close(pfd2[0]);
close(pfd2[1]);
}