How I can redirect child's output stream
in the pipe, what the parent process can read data from pipe.
> How I can redirect child's
>output stream
>in the pipe, what the parent
>process can read data from
>pipe.I do not know if it is realy what you need, but programm bellow
writes some data to one file descriptor in the child and read them
from another file descriptor in the parent. May be this will be helpfull
for you somehow.#include <stdio.h>
#include <sys/wait.h>void main(void) {
int fds[2];
int pid,res;
char buf[512];
FILE *f;memset(buf,0,sizeof buf);
if (pipe(fds)<0) return;
switch (pid=fork()) {
case 0: //Here the child
close(fds[0]);
f=popen("ls /usr/","r");
if (f==NULL) return;
while(fgets(buf,sizeof(buf)-1,f)!=NULL) {
write(fds[1],buf,strlen(buf)+1);
}
pclose(f);
close(fds[1]);
puts("Child finished");
return;
case -1: return;}
// Parent now
close(fds[1]);
while(waitpid(pid,NULL,WNOHANG)<=0) {
if (read(fds[0],buf,sizeof(buf)-1)>0)
printf("Got from child:%s",buf);
}
puts("Parent finished");
return;
}
man dup2