I am trying to figure out how pipes work so I have this code:
Code:
int main(){
int fdpipe[2];
pipe(fdpipe);
if(fork()!=0){//parent
dup2(fdpipe[1],1);
close(fdpipe[0]);
printf("mplamplampla\n");
int status;
waitpid(-1,&status,0);
}
else{
dup2(fdpipe[0],0);
close(fdpipe[1]);
char buf[100];
read(stdin,buf,7);
printf("read from input:%s\n",buf);
}
}
what i am trying to do is having the parent process write something to the pipe and child process read from the pipe. But the child process just hangs without printing any output.
Now, if i change the
Code:
printf("mplampla\n")
with
Code:
write(1,"mplampla\n",10);
works fine. child prints:
read from input:mplampla
Isn't printf writing to file with filedescriptor 1, output?
thanks