URL: https://www.opennet.ru/cgi-bin/openforum/vsluhboard.cgi
Форум: vsluhforumID9
Нить номер: 567
[ Назад ]

Исходное сообщение
"I write shell for Unix, help me !!!"

Отправлено Dale Dugburg , 09-Мрт-02 19:34 
    How I can redirect child's output stream
in the pipe, what the parent process can read data from pipe.

Содержание

Сообщения в этом обсуждении
"RE: I write shell for Unix, help me !!!"
Отправлено Soldier , 10-Мрт-02 09:37 
> 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;
}


"RE: I write shell for Unix, help me !!!"
Отправлено Арлекин , 10-Мрт-02 11:03 
man dup2