Thread: Problem redirecting multiple pipes with multiple children

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Problem redirecting multiple pipes with multiple children

    Hi there, I've been working on a function that works like a pipeline of a shell but receives a directory, go over it an search for every file to send it to a filter, something like this in bash "cat dir/* | cmd_1 | cmd_2 | ... | cmd_N", The only problem i have with the code is the redirection of the pipe descriptors.

    Code:
      int main(int argc, char* argv[]){ 
    
           char** cmd;
           int Number_cmd;
            
           cmd = &(argv[2]); /*list of cmds*/
           Number_cmd = argc-2;       /*number of cmds*/
    
           filter();
           directory(argv[1]);      
           return 0;
          }    
    
        void filter(void){
    
            if(Number_cmd != 0){
             
              int p,i;
              int fd[2];
    
              for(i=0;i<Number_cmd;i++)
                  pipe(fd);
    
              for(p=(Number_cmd-1); p>=0; p--){
                switch(fork()){
                       case -1:
          
                          perror("fork");
                          exit(1);
    
                        case  0:       /* Child */
                           close(fd[1]);
                           close(0);
                           dup(fd[0]);
                           close(fd[0]); 
        
                          execlp(filter[p], filter[p], NULL);
                          perror("exec");
                          exit(1);
    
                        default:             /* Father */
                           close(fd[0]);          
                           close(1);
                           dup(fd[1]);                
                           close(fd[1]);
                           break;
                        }
                      }
                    } 
                  }
    
           void directory(char* directory_name){
                    DIR* dir = NULL;
                    struct dirent* ent;
                    char fich[1024];
                    char buff[4096];
                    int fd, reading;
                    struct stat sdata;
                   
                    dir = opendir(directory_name);
                          while((ent=readdir(dir))!=NULL){
                          
                           if(ent->d_name[0]=='.')
                                continue;
                        
                        fich[0]='\0'; 
                        strcat(fich, directory_name); 
                        strcat(fich, "/");          
                         strcat(fich, ent->d_name); 
                        stat(fich,&sdata)
        
                        if(S_ISDIR(sdata.st_mode))
                                 continue;
    
                        fd = open(fich, O_RDONLY)
                         while((reading= read(fd, buff, 4096)) > 0){
                           (write(1, buff, reading) < reading);
                              continue;
                               }
               
                          close(fd);  
                          }
                       closedir(dir);
                       }
    The code is seems to work fine except when i run it with more than one command in example ("./filter DIR wc rev") in this case it returns
    wc: standard input: Bad file descriptor
    wc: -: Bad file descriptor
    0 0 0

    Thank everyone in advance (sorry for my english i am not a native english speaker)
    Last edited by narib; 03-21-2014 at 04:14 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That code would not even compile, let alone link and run, or produce bad output. main() calls a non-existent function named filter(), and the function preparar_filtros() which does the forking is never called.

    Try providing a small sample of actual code that actually exhibits your problem.

    No amount of challenges with english justify not providing representative code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    Sorry it was my fault I was translating the code from Spanish to English and I forgot to change that function name, so sorry for the mistake, I apologize.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with multiple pipes.
    By LightsOut06 in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2010, 02:38 PM
  2. multiple forks, pipes?
    By alcaseltz in forum C Programming
    Replies: 2
    Last Post: 10-26-2007, 07:07 AM
  3. Problem with multiple pipes...
    By LightsOut06 in forum C++ Programming
    Replies: 0
    Last Post: 10-28-2005, 10:15 AM
  4. MFC: Multiple clicks shows multiple bitmaps - how?
    By BrianK in forum Windows Programming
    Replies: 0
    Last Post: 06-20-2004, 07:25 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

Tags for this Thread