We help IT Professionals succeed at work.
Get Started

Pipes and fork

770 Views
Last Modified: 2013-12-26
I dont know what am i doing wrong, the parent process sends the lines of a file to the children them  the children count them and send them to the parent.
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUFFERSIZE 100

typedef struct array{
    int lines;
    int pid;
    }ARRAY;

int fd0[2];

void handler();

int main(int argc,const char ** argv)
{
    FILE *fp, *fp0, *fp1, *fp2, *fp3;
    ARRAY *p;
    char str[BUFFERSIZE];
    pid_t p1,p2,p3;
    int fd1[2],fd2[2],fd3[2], count1=0, count2=0, count3=0, line=0;
    if((p = (ARRAY*)malloc(sizeof(ARRAY)))==NULL){
        printf("ERROR ALLOCATING MEMORY\n");
        exit(1);
    }

    if (pipe(fd0) == -1){
    {
        perror("pipe call error");
        exit(1);
    }
    if (pipe(fd1) == -1)
    {
        perror("pipe call error");
        exit(1);
    }
    if (pipe(fd2) == -1)
    {
        perror("pipe call error");
        exit(1);
    }
    if (pipe(fd3) == -1)
    {
        perror("pipe call error");
        exit(1);
    }

    if ((p1=fork())==-1)
    {
        perror("Child fail");
        exit(1);
    }
    }
    else
    {
        if (p1==0)
        {
            close(fd0[0]);
            fp0 = fdopen(fd0[1], "w");
            if(fp0==NULL){perror("file pointer (0) of child (p1):");}
            close(fd0[1]);
            close(fd1[1]);
            fp1 = fopen(fd1[0], "r");
            if(fp1== NULL){perror("file pointer (1) of child (p1):");}
            close(fd1[0]);
            while(fgets(str, BUFFERSIZE, fp1)!= NULL)
            {
                printf("%d : %s\n", getpid(), str);
                count1++;
            }
            p->pid=getpid();
            p->lines=count1;
            fwrite(p, sizeof(ARRAY), 1, fp0);
            fclose(fp0);
            fclose(fp1);
            if(kill(getpid(), SIGUSR1)==-1){ perror("Kill fail child p1:"); exit(1);}
            exit(EXIT_SUCCESS);
        }
        else if ((p2=fork())==-1)
        {
            perror("Child fail");
            exit(1);
        }
        else
        {
            if (p2==0)
            {
                close(fd0[0]);
                fp0 = fdopen(fd0[1], "w");
                if(fp0==NULL){perror("file pointer (0) of child (p2):");}
                close(fd0[1]);
                close(fd1[1]);
                fp2 = fopen(fd2[0], "r");
                if(fp2==NULL){perror("file pointer (2) of child (p2):");}
                close(fd2[0]);
                while(fgets(str, BUFFERSIZE, fp2)!= NULL)
                {
                    printf("%d : %s\n", getpid(), str);
                    count2++;
                }
                p->pid=getpid();
                p->lines=count2;
                fwrite(p, sizeof(ARRAY), 1, fp0);
                fclose(fp0);
                fclose(fp2);
                if(kill(getpid(), SIGUSR1)==-1){ perror("Kill fail child p2:"); exit(1);}
                exit(EXIT_SUCCESS);
                }
            else if ((p3=fork())==-1)
            {
                perror("Child fail");
                exit(1);
            }
            else
            {
                if (p3==0)
                {
                    close(fd0[0]);
                    fp0 = fdopen(fd0[1], "w");
                    if(fp0==NULL){perror("file pointer (0) of child (p3):");}
                    close(fd0[1]);
                    close(fd3[1]);
                    fp3 = fopen(fd3[0], "r");
                    if(fp3==NULL){perror("file pointer (3) of child (p3):");}
                    close(fd3[0]);
                    while(fgets(str, BUFFERSIZE, fp3)!= NULL)
                    {
                        printf("%d : %s\n", getpid(), str);
                        count3++;
                    }
                    p->pid=getpid();
                    p->lines=count3;
                    fwrite(p, sizeof(ARRAY), 1, fp0);
                    fclose(fp0);
                    fclose(fp3);
                    if(kill(getpid(), SIGUSR1)==-1){ perror("Kill fail child p3:"); exit(1);}
                    exit(EXIT_SUCCESS);

                }else{ //parent code
                    if(signal(SIGUSR1,handler)==SIG_ERR)
                    {
                        perror("signal:");
                        exit(1);
                    }
                    if(fp==NULL){perror("fp:"); exit(1);}

                    if(fp1==NULL){perror("fp1:"); exit(1);}
                    close(fd1[0]);
                    fp1=fopen(fd1[1], "w");
                    close(fd1[1]);

                    if(fp2==NULL){perror("fp2:"); exit(1);}
                    close(fd2[0]);
                    fp2=fopen(fd2[1], "w");
                    close(fd2[1]);

                    if(fp3==NULL){perror("fp3:"); exit(1);}
                    close(fd3[0]);
                    fp1=fopen(fd3[1], "w");
                    close(fd3[1]);

                    while(fgets(str, BUFFERSIZE, fp)!=NULL)
                    {
                        if(line == 0)
                        {
                            fputs(str, fp1);
                            line++;
                        }else if(line == 1)
                                {
                                    fputs(str, fp2);
                                    line++;
                                }
                                else if(line == 2)
                                        {
                                            fputs(str, fp3);
                                            line=0;
                                        }
                    }
                    fclose(fp1);
                    fclose(fp2);
                    fclose(fp3);
                    waitpid(p1, NULL, 0);
                    waitpid(p3, NULL, 0);
                    waitpid(p3, NULL, 0);
                    close(fd0[0]);
                    }

                }
            }
        }
    }


void handler()
{
    FILE *fp0;
    ARRAY *p;
    fp0 = fdopen(fd0[0], "r");
    if(fp0== NULL){
        exit(1);
    }else{
        if((p = (ARRAY*)malloc(sizeof(ARRAY)))==NULL){
            printf("ERROR ALLOCATING MEMORY\n");
            exit(1);
            }
        fread(p, sizeof(ARRAY), 1, fp0);
        printf("PID %d == num linhas = %d \n", p->pid, p->lines);
    }
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2012
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE