方阵与fork()的乘法;在C.

时间:2016-12-18 23:21:36

标签: c linux matrix fork matrix-multiplication

我用C语言编写了一个程序,用于1000A和B阶矩阵的乘法。现在我必须添加进程!

现在我必须在乘法中添加4个进程,这将导致数组C。

计算从0到249的1个过程;

2计算从250到499的过程;

3计算从500到749的过程;

4计算从750到999的过程;

乘法工作正常;

我对过程并不了解,问题在于过程的各个部分,我不能做我需要的事情;

请遵循以下代码:

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

int id;

int main(){

    int i;
    int j;
    int row;
    int col;
    int order;

    long T1;
    long T2;
    float delta;

    int process_1;
    int process_2;
    int process_3;
    int process_4;

    printf("Enter the order of the square matrices A and B: ");
    scanf("%d", &order);

    T1 = clock();

    printf("\nThe square matrices A and B, are order matrices %d",order);

    order = order - 1;

    row = order;
    col = order;

    float A[row+1][col+1];
    float B[row+1][col+1];

    for(i = 0; i <= row; i++){

        for(j = 0; j <= col; j++){

            printf("\n\nEnter the value of the array A[%d][%d]: ",i+1,j+1);
            scanf("%f", &A[i][j]);

            printf("\nEnter the value of the array B[%d][%d]: ",i+1,j+1);
            scanf("%f", &B[i][j]);
        }
    }

    printf("\nThe multiplication of matrices A and B:\n\n");

    id = shmget(IPC_PRIVATE, 100, 0600);

    process_1 = fork();
    process_2 = fork();
    process_3 = fork();
    process_4 = fork();

    int *a;
    a = shmat(id,0,0);

    printf("\n\nprocess 1:\n\n");

    if(process_1 == 0){
        int P1 = 0;

        if(P1 <= 249){

            for(i = 0; i <= row; i++) {

                    for(j = 0; j <= col; j++) {

                        float C[row+1][col+1];

                            for(int AUX = 0; AUX <= order; AUX++) {

                                    C[i][j] += A[i][AUX] * B[AUX][j];

                                }
                                printf("%.2f ",C[i][j]);
                    }
                    printf("\n");
            }
        }
        exit(0);
    }

    printf("\n\nprocess 2:\n\n");

    if(process_2 == 0){
        int P2 = 250;

        if(P2 >=250 && P2 <= 499){

            for(i = 0; i <= row; i++) {

                    for(j = 0; j <= col; j++) {

                        float C[row+1][col+1];

                            for(int AUX = 0; AUX <= order; AUX++) {

                                    C[i][j] += A[i][AUX] * B[AUX][j];

                                }
                                printf("%.2f ",C[i][j]);
                    }
                    printf("\n");
            }
        }
        exit(0);
    }

    printf("\n\nprocess 3:\n\n");

    if(process_3 == 0){
        int P3 = 0;

        if(P3 >=500 && P3 <= 749){

            for(i = 0; i <= row; i++) {

                    for(j = 0; j <= col; j++) {

                        float C[row+1][col+1];

                            for(int AUX = 0; AUX <= order; AUX++) {

                                    C[i][j] += A[i][AUX] * B[AUX][j];

                                }
                                printf("%.2f ",C[i][j]);
                    }
                    printf("\n");
            }
        }
        exit(0);
    }

    printf("\n\nprocess 4:\n\n");

    if(process_4 == 0){
        int P4 = 0;

        if(P4 >=750 && P4 <= 999){

            for(i = 0; i <= row; i++) {

                    for(j = 0; j <= col; j++) {

                        float C[row+1][col+1];

                            for(int AUX = 0; AUX <= order; AUX++) {

                                    C[i][j] += A[i][AUX] * B[AUX][j];

                                }
                                printf("%.2f ",C[i][j]);
                    }
                    printf("\n");
            }
        }
        exit(0);
    }

    waitpid(process_1, NULL, 0);
    waitpid(process_2, NULL, 0);
    waitpid(process_3, NULL, 0);
    waitpid(process_4, NULL, 0);

    T2 = clock();
        delta = (float)(T2-T1)/CLOCKS_PER_SEC;

        printf("\n\Time %.5f seconds\n\n",delta);

    return 0;
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我会从为工艺标识符更改类型int到pid_t开始,例如:

pid_t process_1;

然后会改变:

process_1 = fork();
process_2 = fork();
process_3 = fork();
process_4 = fork();

pid_t pid = fork();
if (pid) {
    process_1 = pid;
    pid = fork();
}
if (pid) {
    process_2 = pid;
    pid = fork();
}
if (pid) {
    process_3 = pid;
    pid = fork();
}
if (pid)
    process_4 = pid;

我们的想法是,我们只在父进程中执行fork()并在子进程中跳过分叉。否则你的代码分叉处理为树,每个子进程和父进程在前一个之后调用下一个fork(),然后他们的子进行相同的操作,等等四次。 上面的代码不检查fork()是否返回错误代码(-1)。在理想的世界中,强烈建议。

从fork(2)手册页:

  

返回值

     

成功时,子进程的PID在父进程中返回,   在孩子身上返回0。失败时,返回-1   parent,没有创建子进程,并且正确设置了errno。