【问题标题】:Multiplication of square matrix with fork (); in C方阵与fork()的乘法;在 C 中
【发布时间】:2016-12-18 23:21:36
【问题描述】:

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

现在我必须在乘法中添加 4 个过程,这将产生数组 C。

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

250到499的计算过程;

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

从750计算到999的4个过程;

乘法工作正常;

我不太了解流程,问题出在流程的部分,我不能做我需要的;

按照下面的代码:

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

我该如何解决这个问题?

【问题讨论】:

    标签: c linux matrix fork matrix-multiplication


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多