The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]




Версия для распечатки Пред. тема | След. тема
Новые ответы [ Отслеживать ]
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, 09:37 , 10-Мрт-02 (1)
    > 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 !!!, !*! Арлекин, 11:03 , 10-Мрт-02 (2)



Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру