How would I go about piping the stdout of a child process to the stdin of the parent process ?
Ive got it working for fixed data lengths below
main()
{
int pfd[2], n;
pid_t cpid;
char *msg= "This is a message\n";
char buf[30];
pipe(pfd);
if((cpid= fork()) == -1)
{
perror("fork");
}
if(cpid== 0)
{
close(pfd[0]);
write(pfd[1], msg, strlen(msg));
}
else
{
close(pfd[1]);
n = read(pfd[0], buf, sizeof(buf));
printf("Received: %s", buf);
}
}
I need to use an exec function in the child, so it will die and therefore I can't save it in a buffer etc So I have to link the stdout of the child to the stdin of the parent.
Ive got it working for fixed data lengths below
main()
{
int pfd[2], n;
pid_t cpid;
char *msg= "This is a message\n";
char buf[30];
pipe(pfd);
if((cpid= fork()) == -1)
{
perror("fork");
}
if(cpid== 0)
{
close(pfd[0]);
write(pfd[1], msg, strlen(msg));
}
else
{
close(pfd[1]);
n = read(pfd[0], buf, sizeof(buf));
printf("Received: %s", buf);
}
}
I need to use an exec function in the child, so it will die and therefore I can't save it in a buffer etc So I have to link the stdout of the child to the stdin of the parent.