我的过程编程老师让我用C编写了一个程序,该程序创建了四个子代,并让它们分别计算一系列数字的第一,第二,第三和第四季度,并为父代提供了所有质数。

我对第一个孩子的四分之一进行了正确的编码,但是当我添加第二个孩子的四分之一时,程序的行为变得不可控制。我和我的老师花了大约2个小时来仔细研究代码,但并未发现问题。

现在的代码是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){

    unsigned long long a=500000,b,c; // not used yet -> d,e,i;
    pid_t pid1;
    unsigned long long fin = 0; //This is used in each child to write if it has finished the prime number calculation.
    unsigned long long fin1 = 0, fin2 = 0; //This is used on the parent to check if a child has finished.
    int primo = 0; //This is used to know if a number is a prime number.
int fd1[2]; //Pipe which communicates the parent with the first child.
    int fd2[2]; //Pipe which communicates the parent with the second child.
//  int fd3[2]; //Not used yet
//  int fd4[4]; //Not used yet
    pipe(fd1); //First child pipe
    pipe(fd2); //Second child pipe
//  pipe(fd3); //Not used yet
//  pipe(fd4); //Not used yet
    pid1 = fork(); //Creating first child
        switch (pid1){
            case -1: //Error
                printf("Error creating child.");
                exit(-1);
            case 0: //First child
                close(fd1[0]); //Input close
                for(b=100;b<(a/4);b++){         //
                    for(i=2;i<b/2;i++){     // These loops check each number from 100 to 125000
                        if(b%i==0){     // and if it is NOT a prime number, it breaks and tries
                            primo=0;    // to check the next number.
                            break;      //
                        }           //
                        primo=1;        //
                    }
                    if(primo==1){               //If it IS a prime number, it's written on the pipe
                        write(fd1[1], &b, sizeof(b));   //and sent to the parent.
                    }
                }
                fin=1; //The child sets it has finished calculating and writes it in the pipe to tell his parent.
                write(fd1[1], &fin, sizeof(fin));
                close(fd1[1]); //Output closing
                break; //First child ends
            default: //Parent

                pid1 = fork(); //Creating second child
                    switch (pid1) {

                        case -1: //error
                            printf("Error");
                            exit(-1);
                        case 0: //Sencond child
                            close(fd2[0]);                      //This behavior is EXACTLY equals to the first child behavior
                            for(c=(a/4);c<(a/2);c++){               //
q                               for(i=2;i<c/2;i++){             //
                                    if(c%i==0){             //
                                        primo=0;            //
                                        break;              //
                                    }                   //
                                primo=1;                    //
                                }                       //
                                if(primo==1){                   //
                                    write(fd2[1], &c, sizeof(c));       //
                                }                       //
                            }                           //
                            fin=1;                          //
                            write(fd2[1], &fin, sizeof(fin));           //
                            close(fd2[1]);                      //
                            break;
                        default: //Parent
                            //HERE WOULD COME THE CODE FOR THIRD AND FOURTH CHILDS.
                            break;



                    } //second child switch close

                //Parent reads answers from childs
                close(fd1[1]); //First child output closing
                close(fd2[1]); //Second child output closing
                for(;;){ //Infinite loop
                    if(fin1==0){ //If first child HAS NOT finished (As it sends a 1 if it does)
                        read(fd1[0], &b, sizeof(b)); //Read the prime number
                        if(b==1){ //If it is a 1, then the child has finished.
                            fin1=1;  //We set the first child has finished
                            close(fd1[0]); //First child input closing
                        }else{
                            printf("%llu es primo\n", b); //Otherwise it is a prime number, then it's printed to console.
                        }
                    }
                    if(fin2==0){                        //Same behavior as with first child
                        read(fd2[0], &c, sizeof(c));
                        if(c==1){
                            fin2=1;
                            close(fd2[0]);
                        }else{
                            printf("%llu es primo\n", c);
                        }
                    }
                    if(fin1==1&&fin2==1){ //If both childs have finished, then we exit.
                        exit(0);
                    }
                }
                break;

        }
    exit(0);
}


它似乎是正确的,但不能正常工作。当第二个孩子完成其数字范围(从125000到249999)的计算时,它将阻止第一个孩子,第一个孩子停止。

然后程序进入无限循环,以读取和打印管道的内容
它看起来像这样:

[first child last calculated number] es primo
250000 es primo
[first child last calculated number] es primo
250000 es primo
[first child last calculated number] es primo
250000 es primo
[first child last calculated number] es primo
250000 es primo


所以。因此,我们问如何将250000写入管道并从父级读取,以及为什么第二个孩子完成阻止第一个孩子。

问候。

最佳答案

在第67行,您的代码突破了第二个switch语句(第45行)。执行从第77行继续。因此子进程2试图关闭(fd2 [1])两次。子进程2正在执行针对父进程的代码。

您可以尝试将第67行替换为

exit(0);

关于c - 当两个子流程都通过管道与父流程通信时,一个子流程将阻塞另一个子流程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26297661/

10-11 23:20
查看更多