Convener of 3 processes connected with pipe



  • Good afternoon. It is not possible to implement a conveer from the 3 processes connected with the pipe. There's nothing going on in the 3rd process. Convener of the 2nd trials is working.

    #include <iostream>
    #include <string>
    using namespace std;
    

    #include <sys/wait.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <errno.h>

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char* argv[]){
    //Инициализация имени архива и прототипа
    if (argc != 3){
    cerr << "Parameters count is not correct!" << endl;
    exit(1);
    }
    //порождаем дочерний процесс для выполнения ar.
    int fds[2];
    pipe(fds);
    if (fork()){
    //Перенаправляем вывод в программный канал
    dup2(fds[1],1);
    execl("/bin/ar", "ar", "-t", argv[1], NULL);
    }
    else { //дочерний процесс
    int fds2[2];
    pipe(fds2);
    if (fork()){
    dup2(fds[0],0);
    dup2(fds2[1],1);
    execl("/bin/grep", "grep", argv[2], NULL);
    }else {
    dup2(fds2[0],0);
    char buf[100];
    read(0,buf,100);
    cerr << buf << endl;
    }
    }
    return 0;
    }



    1. Depending on the distributive (e.g. me) ar may be located /usr/bin/Not in /bin/

    2. You've got a miscarriage, because the mass. buf filled with waste from the grid and will be released in addition to the reading bays and in order to read all the data may require a few read()

    3. Look after the launch. ps, engendered background processes that await entry. They're waiting for data from the pipe, because the other end of it is still open in some process (i.e. the same). Whatever happens, in processes where one of the ends of the pipe doesn't need a descriptor, it needs to be closed.

    With all this:

    #include <iostream>
    using namespace std;
    

    #include <sys/wait.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <errno.h>

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char* argv[]){
    //Инициализация имени архива и прототипа
    if (argc != 3){
    cerr << "Parameters count is not correct!" << endl;
    exit(1);
    }
    //порождаем дочерний процесс для выполнения ar.
    int fds[2];
    pipe(fds);
    if (fork()){
    close(fds[0]); //- здесь читать из трубы не будем
    //Перенаправляем вывод в программный канал
    dup2(fds[1],1);
    execl("/bin/ar", "ar", "-t", argv[1], NULL);
    }
    else { //дочерний процесс
    close(fds[1]); //- а дальше сюда не нужно писать
    int fds2[2];
    pipe(fds2);
    if (fork()){
    close(fds2[0]);
    dup2(fds[0],0);
    dup2(fds2[1],1);
    execl("/bin/grep", "grep", argv[2], NULL);
    } else {
    close(fds[0]);
    close(fds2[1]);
    dup2(fds2[0],0);
    char buf[100];
    int i;
    while((i=read(0, buf, sizeof buf))>0) write(2, buf, i);
    }
    }
    return 0;
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2