Parent and child processes terminate randomly (linux, c, fork ())

The challenge was to sort in ascending order the array from input.dat using the fork and divide and conquer algorithm.

So, I wrote code using this, but the parent process ends faster than the child. I have the following line in my code, but parents won't wait for their children.

while(wait(NULL)>0);

      

Small explanation for my code:

Input.dat contains an array with the following form:

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

Example:

6 2 3 1 4 5 6

So I read in an array and I split it into two arrays created in two structs for a pipe apology. After that, I call the divitimp function, explaining the form a bit:

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

After running the divitimp function, I use fork, if it works (no error), I see if it is a child or parent process (the code is the same in the if and in the else), after that (in the if) I read one structure from the pipe. if the array in the structure is no longer than two elements. I write a sorted structure to the writewhilecomingup channel and do not call the divitimp function, if the array is longer than 2, I split the array into two arrays created in two structures and I write them to write to the write channel and call the divitimp function.

After that I want to wait for the child process to finish and I use merge sort to sort the sorted arrays and then I write the results (eredmeny) to the writewhilecomingup channel.

After the divitimp function finishes, I'll go back to the main file and read the solution structure from the readwhilegoingup pipe and write it to output.dat.

The problem is that processes (parent and child) end up randomly.

Original code (very long time sorry, but without 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;
}

      

+3


source to share


1 answer


I think you have confused parent and child processes. fork () returns 0 in the child process, but you have the output (0) s on the else branch, which basically means you are terminating the parents.



Try switching your if statements to pid! = 0.

+5


source







All Articles