简体   繁体   English

父子进程随机结束(linux,c,fork())

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

The task wast to sort in an ascending order the array from the input.dat, using fork and divide and conquer algorithm. 任务是使用fork和divide and conquer算法从input.dat按升序对数组进行排序。

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: 在input.dat中是一个具有以下形式的数组:

(long of the array) (space) (first number) (space) (second number) ... (数组的长度)(空格)(第一个数字)(空格)(第二个数字)......

example: 例:

6 2 3 1 4 5 6 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: 之后我调用了divitimp函数,对表单有一点解释:

(in my code) divitimp(readwhilegoingdown(lefeleolvas),writewhilegoingdown(lefeleir),readwhilecomingup(felfeleolvas),writewhilecomingup(felfeleir)) (在我的代码中)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. divitimp函数启动后我使用fork,如果它工作(没有错误)我看它是子进程还是父进程(if和else中的代码是相同的),之后(在if中)我如果结构中的数组不长于两个元素,则从管道中读出一个结构我将已排序的结构写入writewhilecomingup管道而不回调divitimp函数,如果数组长于2,我将数组拆分为两个在两个结构中创建的数组,我将它们写入writewhilegoingdown管道并回调divitimp函数。

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. 在这些之后,我想等到子进程结束,我使用merge sort对排序的数组进行排序,然后将结果(eredmeny)写入writewhilecomingup管道。

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. divitimp函数完成后,我返回main并从readwhilegoingup管道读取解决方案结构,并将其写入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;
}

I think you confused the parent and child processes. 我认为你混淆了父母和子女的过程。 fork() returns 0 in the child process, but you have exit(0)s on the else branch, which basically means you terminate the parents. fork()在子进程中返回0,但是在else分支上有exit(0),这基本上意味着你终止父进程。

Try switching your if statements to pid != 0. 尝试将if语句切换为pid!= 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM