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

标签 c linux matrix fork matrix-multiplication

我用 C 在 Linux 中编写了一个 1000 阶 A 和 B 矩阵相乘的程序。现在我必须添加进程!

现在我必须在乘法中添加 4 个过程才能得到数组 C。

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

从250计算到499的2个过程;

从500到749的3个计算过程;

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;
}

我该如何解决这个问题?

最佳答案

我将从将进程标识符的类型 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 为一棵树,每个子进程和父进程在前一个之后调用下一个 fork(),然后它们的子进程执行相同的操作,依此类推四次。 上面的代码不检查 fork() 是否返回错误代码 (-1)。在理想世界中,强烈建议这样做。

来自 fork(2) 手册页:

Return Value

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

关于c - 方阵与fork()的乘法;在 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41213950/

相关文章:

c++ - linux bind 不创建套接字

python - 如何在 linux 上的 python 中使用 %cpu 在给定时间内杀死特定进程?

linux - ls -la 和 ls -la > ls-1.txt 的区别

c# - 如何在不知道输入类型的情况下创建填充矩阵的方法

c - 无穷大作为合并排序中的哨兵?

c - 使用 fwrite() 进行 Jpeg 无线传输;需要处理丢失的数据包

c - 如何在障碍处正确同步线程

c - PulseAudio API - 无麦克风信号

arrays - OpenCL 2D 工作组尺寸

c++ - 用 C++ 类替换 Numerical Recipe 的 dmatrix