Child inherits the parent's file descriptors
hi,
thanks for replying. Here's is the sample code that i used to test the feature. thanks.
#include <unistd.h>
#include <stdio.h>
int outfd[2];
int infd[2];
int oldstdin, oldstdout;
main()
{
pipe(outfd);
pipe(infd);
oldstdin = dup(0);
oldstdout = dup(1);
close(0);
close(1);
dup2(outfd[0], 0);
dup2(infd[1],1);
if(!fork())
{
char buf[100];
scanf("%s",buf);
printf("Hello %s",buf);
}
else
{
char input[100];
close(0);
close(1);
dup2(oldstdin, 0);
dup2(oldstdout, 1);
write(outfd[1],"World\n",6);
input[read(infd[0],input,100)] = 0;
printf("%s\n",input);
}
}
|