Parent and child process end randomly (linux, c, fork())

Viewed 214

The task wast to sort in an ascending order the array from the input.dat, using fork and divide and conquer algorithm.

So i wrote a code using this, but the parent process end faster than the child. I have the following line in my code but the parents will not wait for their children.

while(wait(NULL)>0);

A little explanation for my code:

In the input.dat is an array with the following form:

(long of the array) (space) (first number) (space) (second number) ...

example:

6 2 3 1 4 5 6

So I read in the array, and I split it to two arrays created in two structs, for writhing into the pipe. After that I call the divitimp function , a little explanation for the form:

(in my code) divitimp(readwhilegoingdown(lefeleolvas),writewhilegoingdown(lefeleir),readwhilecomingup(felfeleolvas),writewhilecomingup(felfeleir))

After the divitimp function started I use fork, If it works(no errors) I look if it is the child or the parent process(the code is the same in the if and in the else), after that (in the if ) I read out one struct from the pipe if the array in the struct is not longer than two element I write the sorted struct to the writewhilecomingup pipe and not calling back the divitimp function, if the array is longer than 2, I split the array to two arrays created in two structs and i write them to writewhilegoingdown pipe and calling back the divitimp function.

After these I want to wait until the child process end and I use merge sort to sort the sorted arrays and then I write the results(eredmeny) in to the writewhilecomingup pipe.

After the divitimp function finished i go back to main and read the solution struct from the readwhilegoingup pipe, and write it to the output.dat.

The problem is the processes (parent and child) end in a random order.

Source code (very long sorry, but without the code I can't explain the problem):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef struct{
int n;
int a[100];
}TOMB;

void divitimp(int lefeleolvas, int lefeleir,int felfeleolvas,int felfeleir){
pid_t pid;
int i;
if ((pid = fork()) < 0){
    perror("fork hiba");
}
else{
    printf("pid= %i \n",pid);
    if(pid==0){
        TOMB valtozo;
        read(lefeleolvas,& valtozo, sizeof(valtozo));
        if(valtozo.n>2){
            TOMB sv,sv1;
            sv.n=valtozo.n/2;
            sv1.n=valtozo.n-valtozo.n/2;
            printf("Uj ismetles:%d ",sv.n);
            for(i=0;i<sv.n;i++){
                    sv.a[i]=valtozo.a[i];
                printf(" %d ",sv.a[i]);
            }
            int ai=sv1.n-1;
            printf(" *%d* ", sv1.n);
            for(i=0;i<sv1.n;i++){
                sv1.a[i]=valtozo.a[ai];
                ai++;
                printf(" %d ",sv1.a[i]);
            }
            printf("itt a vege\n");
            write(lefeleir,& sv,sizeof(sv));
            write(lefeleir,& sv1,sizeof(sv1));
            divitimp(lefeleolvas, lefeleir, felfeleolvas, felfeleir);
        }           
        else{
            if(valtozo.n==1){
                write(felfeleir,& valtozo,sizeof(valtozo));
            }
            else{
                if(valtozo.n==2);{
                    if(valtozo.a[0]<valtozo.a[1]){
                        write(felfeleir,& valtozo,sizeof(valtozo));
                    }
                    else{
                        int csere=valtozo.a[1];
                        valtozo.a[1]=valtozo.a[0];
                        valtozo.a[0]=csere;
                        write(felfeleir,& valtozo,sizeof(valtozo));

                    }
                }
            }
        }
    }
    else{
                    TOMB valtozo;
                    read(lefeleolvas,& valtozo, sizeof(valtozo));
                    if(valtozo.n>2){
                            TOMB sv,sv1;
                            sv.n=valtozo.n/2;
                            sv1.n=valtozo.n-valtozo.n/2;
                            printf("Uj ismetles:%d ",sv.n);
                            for(i=0;i<sv.n;i++){
                                    sv.a[i]=valtozo.a[i];
                                    printf(" %d ",sv.a[i]);
                            }
                            int ai=sv1.n-1;
            printf(" *%d* ", sv1.n);
                            for(i=0;i<sv1.n;i++){
                                    sv1.a[i]=valtozo.a[ai];
                                    ai++;
                                    printf(" %d ",sv1.a[i]);
                            }
                            printf("itt a vege\n");
                            write(lefeleir,& sv,sizeof(sv));
                            write(lefeleir,& sv1,sizeof(sv1));
                            divitimp(lefeleolvas, lefeleir, felfeleolvas, felfeleir);
            exit(0);
                    }
                    else{
                            if(valtozo.n==1){
                                    write(felfeleir,& valtozo,sizeof(valtozo));
                                    exit(0);
                            }
                            else{
                                    if(valtozo.n==2);{
                                            if(valtozo.a[0]<valtozo.a[1]){
                                                    write(felfeleir,& valtozo,sizeof(valtozo));
                                            exit(0);
                                            }
                                            else{
                                                    int csere=valtozo.a[1];
                                                    valtozo.a[1]=valtozo.a[0];
                                                    valtozo.a[0]=csere;
                                                    write(felfeleir,& valtozo,sizeof(valtozo));
                                            exit(0);
                                            }
                                    }
                            }
                    }
    }
    while(wait(NULL)>0);
    if(pid==0){
        TOMB sv,sv1,eredmeny;
        read(felfeleolvas,& sv,sizeof(sv));
        read(felfeleolvas,& sv1,sizeof(sv));
        int svi=0,sv1i=0;
        eredmeny.n=sv.n+sv1.n;
        for(i=0;i<sv.n+sv1.n;i++){
            if(svi<sv.n&&sv1i<sv1.n){
                if(sv.a[svi]<sv1.a[sv1i]){
                    eredmeny.a[i]=sv.a[svi];
                    svi++;
                }
                else{
                    eredmeny.a[i]=sv1.a[sv1i];
                    sv1i++;
                }
            }
            else{
                if(svi<sv.n){
                    eredmeny.a[i]=sv.a[svi];
                svi++;  
                }
                else{
                    eredmeny.a[i]=sv1.a[sv1i];
                    sv1i++;
                }
            }
            printf("e=%d sv=%d sv1=%d\n",eredmeny.a[i],sv.a[svi-1],sv1.a[sv1i-1]);

        }
        write(felfeleir,& eredmeny, sizeof(eredmeny));

    }
   }
 }

int main(){
    FILE *f= fopen("input.dat","r");
if(!f){
perror("nincs megfelelo allomany");
exit(1);
}
    FILE *g= fopen("output.dat","w");
int n;
fscanf(f,"%d",&n);
int a[n];
a[0]=n;
int i=0;
printf("%d",n);
for(i;i<n;i++){
fscanf(f,"%d",&a[i]);
}
//printf("%d \n",n);
TOMB betesz1, betesz2;
    betesz1.n=n/2;
    for(i=0;i<betesz1.n;i++){
    betesz1.a[i]=a[i];
printf("%d\n ",a[i]);   
}
    betesz2.n=n-n/2;
    int ai=betesz2.n;
    for(i=0;i<betesz2.n;i++){
    betesz2.a[i]=a[ai];
    ai++;
printf("%d\n ",a[ai-1]);
}
    int pfd[2];
    if (pipe(pfd) < 0){
    perror("pipe hiba");
}
    int pfd1[2];
    if (pipe(pfd1) < 0){
    perror("pipe hiba");
}
    write(pfd[1],& betesz1, sizeof(betesz1));
    write(pfd[1],& betesz2, sizeof(betesz2));

divitimp(pfd[0],pfd[1],pfd1[0],pfd1[1]);
TOMB eredmeny;
read(pfd1[0],& eredmeny,sizeof(eredmeny));
printf("%d\n",eredmeny.n);
for(i=0;i<eredmeny.n;i++){
    fprintf(g,"%d ",eredmeny.a[i]);
}
fclose(f);
fclose(g);
return 0;
}
1 Answers
Related