Error in handling of fork fork waitpid, bash does not return, it remains waiting

0

They could help me find the error, where I should close the pipes and wait for the child processes. Further down in the lines / 1 // 2 // 3 // 4 / is the error.

Thank you!

int procesar_argumentos(char *archivo){
int stdo_conv, stdo_cont,lee_t,lee_v,no_hijos=1;    
int pid_cont,pid_conv,pip_cont[2],pip_conv[2];
char buff_t[500],buff_v[500];






archivo_t *a = malloc(sizeof(archivo_t));
if(a==NULL) return 1;

if(archivo==NULL){
    a->fd =0;
}else{
    if((a->fd=open(archivo,O_RDONLY,S_IRUSR))==-1){
        fprintf(stderr, "falla open!\n");
        return 1;
    }
} 
                /*while( (a->leid=read(a->fd,a->cont,sizeof(a->cont))) > 0 ){
                 write(1,a->cont,a->leid);
                }*


if( (pipe(pip_cont)) < 0 ){
    perror("pipe_cont");
    return 1;
}
switch(pid_cont=fork()){//fork 0:ok, -1:bum
    case -1: perror("ERROR cont\n");
            break;
    case  0: // Hijo cuen_t_a;
            close(pip_cont[1]);             
            while((a->leid=read(pip_cont[0],buff_t,sizeof(buff_t)))>0){
                write(1,buff_t,a->leid);
            }           
            close(pip_cont[0]); 
            //exit(EXIT_SUCCESS);//Preguntar

            break;
    default://PADRE !!!! -> contador
        /*  close(pip_cont[0]);
            while( (a->leid=read(a->fd,a->cont,sizeof(a->cont))) > 0 ){
                write(pip_cont[1],a->cont,a->leid);
                waitpid(pid_cont,&pid_cont,0);                  
            }               
            close(pip_cont[1]);*/
            //waitpid(pid_cont,&pid_cont,0);
            //wait(&pid_cont);

            if((pipe(pip_conv))<0){
            perror("pipe_conv");
            return 1;

            }                           

                switch(pid_conv=fork()){

                    case -1:perror("Error h1\n");
                        break;  
                    case  0:// Hijo con_v_ierte;
                        close(pip_conv[1]); 
                        while ((a->leid=read(pip_conv[0],buff_v,sizeof(buff_v)))>0){
                                write(1,buff_v,a->leid);
                                //;//Preguntar                  
                        }
                        close(pip_conv[0]);
                        //exit(EXIT_SUCCESS);                   
                        break;
                    default: //PADRE !!!! -> convetir


                        while( (a->leid=read(a->fd,a->cont,sizeof(a->cont))) > 0 ){
                            //write(1,a->cont,a->leid);
                            close(pip_cont[0]);     
                            write(pip_cont[1],a->cont,a->leid); 

                            close(pip_conv[0]);
                            write(pip_conv[1],a->cont,a->leid);                         

                        }


    /*1*/                   waitpid(pid_cont,&pid_cont,0);  
    /*2*/                   close(pip_cont[1]);

            /*3*/           waitpid(pid_conv,&pid_conv,0);
                /*4*/       close(pip_conv[1]);                         
                        //exit(EXIT_SUCCESS);
/*GRACIAS*/             break;
                }
            break;
}

//}

//no_hijos=0;





close(a->leid);
free(a);
//sleep(10);
return 0;
}

/ * HELLO WOULD HELP ME: THE PIPES DO NOT CLOSE AND NOT WHERE TO COLLECT THESE 4 LINES BELOW * /

    
asked by AlexSpain 11.06.2018 в 07:39
source

0 answers