Name: Anonymous 2011-10-25 10:51
Hey /prog/. I am trying to access stdin and stdout of a child program. Here is what I did.
This works for stdin of child, I can access it and send strings from parent to child. however reverse does not work.
and ./c is
This works for stdin of child, I can access it and send strings from parent to child. however reverse does not work.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
void main(void){
pid_t pid;
int i;
char str[40];
int fd1[2];
int fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if(pid == 0){
dup2( fd1[1], 1 );
close(fd1[0]);
dup2( fd2[0], 0 );
close(fd2[1]);
execl("./c","c", 0);
}
close(fd1[1]);
close(fd2[0]);
FILE *out = fdopen (fd2[1], "w");
FILE *inp = fdopen (fd1[0], "r");
for(i=0; i<10; i++){
fprintf(out, "hello%d\n", i);
printf("sent\n");
fscanf(inp, "%s", str);
printf("cli:%s\n", str);
}
close(fd2[1]);
close(fd1[0]);
}and ./c is
#include <stdio.h>
void main(){
int i;
char t[20];
for(i=0; i<10; i++){
scanf("%s", t);
printf("!%s!\n", t);
}
}