Problems to communicate parent and children process with fork and pipe

0
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include"archivo.h"


int main(int argc, char  *argv[]){

    int pidM, pidm,leidos, pip_cont[2],pip_conv[2];
    char buff[BUFF], *archivo=NULL;


    if( (pipe(pip_conv)) ==-1) printf("Error en pipe pip_conv\n");

/*pa: Padre crear hijo h1****************************/
    if((pidM=fork()) < 0)printf("Error en pidM\n");

        /*h1: Lee de Pa y pasa a mayúscula*/
/*exe*/ if(pidM==0){

            close(pip_conv[1]);
            convierte_a_mayuscula(pip_conv[0]);
            close(pip_conv[0]);
            return 0; // exit(EXIT_SUCCESS);
        }   

/*!exe*/else{ 
            if( (pipe(pip_cont)) ==-1 )printf("Error en pipe pip_cont\n");

            /*pa: Padre crear hijo h2**************************/
            if( (pidm=fork()) <0 ) printf("Error en pidm\n");

                /*h2: lee de Pa y cuenta*/
        /*exe*/ if(pidm==0){ 
                    close(pip_cont[1]);
                    contar_palabras(pip_cont[0]);
                    close(pip_cont[0]);
                    return 0;//exit(EXIT_SUCCESS);  
                }

                /*pa: provee parte*/
        /*!exe*/else{  
                    close(pip_cont[0]);
                    proc_argto(pip_cont[1]);                    
                    close(pip_cont[1]);
                    sleep(5);
            }

            /*pa: provee parte*/
            close(pip_conv[0]);
            proc_argto(pip_conv[1]);            
            close(pip_conv[1]);
            sleep(5);

        }
    return 0;
}

    /*util.c*/
void proc_argto(int pip_fd){ 
  char *frase = "cadena de texto a manejar por hilos!!!\n";

char buf[500]; int f,d;

//Opcioón 1: lee la frase . Si lee y muesta por pipe   
     //write(pip_fd,frase,strlen(frase)); //Acá si lee

//Opcioón 2:  Leo desde un archivo

  d=open("reservadas.txt",O_RDONLY,S_IRUSR);
   while( (f=read(d,buf,sizeof(buf))) > 0 ){
        write(pip_fd,buf,f);// Acá no lee

//Opcioón 3: envío por entrada estandar de procesos
   while( (f=read(0,buf,sizeof(buf))) > 0 ){
        write(pip_fd,buf,f);// Acá no lee

 }


}

Good, I have a problem to communicate a parent process two children processes (fork ()), when, the parent writes * phrase and the children receive both by a pipe (one for each) the phrase, but when I want to read of a file fd "reserved.txt" or fd standard input (0) does not work for me. Any recommendation? Thanks!

    
asked by AlexSpain 05.06.2018 в 18:20
source

0 answers